2014年04月

Why is my powershell output file blank?

Sorry, I'm new to Powershell so I'm obviously missing something fairly fundamental but I can't seem to figure this out.



I've got a powershell script calling a SQL script. I can get the script to run but I would also like to store the output in a log file.



This is my powershell script



Add-PSSnapin SqlServerCmdletSnapin100 
Add-PSSnapin SqlServerProviderSnapin100

Invoke-Sqlcmd -inputfile "testscript.sql" -serverinstance '.\sql2008' | Out-file -filepath "testlog.txt"


And this is my testscript.sql



PRINT 'HELLO WORLD'


At the moment, it creates the testlog.txt file but it is blank.



What am I doing wrong?



Answers

It seems the data you want is in the verbose data stream. In PowerShell 3.0 and above, you can redirect the verbose stream (4) to the stdout stream (1) before it can be sent to Out-File.



Invoke-Sqlcmd -inputfile "testscript.sql" -serverinstance '.\sql2008' -verbose 4>&1 | Out-file -filepath "testlog.txt"


See Get-Help about_redirection for the explanation of different streams.



See also Powershell: Out-File for discussion about redirecting error and verbose streams to Out-File.



Unfortunately, PowerShell 2.0 only supports redirection of stream 0,1,2. You can use a [System.Management.Automation.PowerShell] object to get at the other streams. See the accepted answer to Powershell Invoke-Sqlcmd capture verbose output



Answers

You can save the Verbose (and all the other console output) to a file without polluting your output stream with Start-Transcript





how can i get the number of times a specific word occur in my sql

Please how can I get the number of times, a specific word occur in a table?
I have a table called result and a field called president.



Index - president  
1 - James
2 - John
3 - John
4 - John
5 - James
6 - John


I tried using the following codes to get the number of times John occur in my table.



SELECT COUNT(`president`) FROM `result` WHERE `president`="John"


But, it's writing syntax error.
N.B: What I just need is the number of times John appeared which I expected to be four(4).



Answers

You can try this mate:



SELECT
president, COUNT(index) 'occured'
FROM
result
WHERE
president = 'John';


E: This one is specific for 'John' only.



SELECT
president, COUNT(index) 'occured'
FROM
result
GROUP BY
president;


E: To display the count for each result.president in your database. Cheers!



Answers

You don't need to use COUNT on a column. In your case, you want to get

the number of rows where the president is 'John'.



Use the following syntax:



SELECT COUNT(*) FROM `result` WHERE `president` = "John"


P.S. Don't call your table result. It is kind of incorrect in terms of naming and architecture in general. Call it PresidentsHistory or PresidentsList.



Answers

Syntax COUNT(`president`) is not correct.



SELECT COUNT(*) FROM `result` WHERE `president` = "John"


Answers

you can try the below query



SELECT COUNT(*) FROM `result` WHERE `president` LIKE "John"




Visiting a supermarket with a shopping list, get all items in the fastest way?

Once in a while, my wife sends me to a REAL supermarket, with a shopping list (she writes me an a piece of paper, or texts me a message).



I was wondering is there a fast way to for-fill the list, without iterating the whole super a dozens of times, till I believe I've got everything.



(I found this question very helpful in life: pair-socks-from-a-pile-efficiently, and hoped I can use the community's help in my own situation)




I don't know/remember where each item is located in the supermarket, and the list I'm given is never sorted (for example, vegetables are listed all over the list, and not one after the other). Also, usually I get continuously text messages with more and more items to buy, while I'm still in the super.




My approach is planning a path that would assure me I iterate the whole supermarket, and pick items from the list while walking that path.



Two ways I was thinking about:


foreach item in the super (n) I pass: check if I have such item in my list (m), will give me an O(n*m) complicity, which not so efficient.
"dividing" the shop to rows (p): standing on the beginning of each row I can read the sign or see which sort of items I should find there, than iterate my list, trying to remember all items in list I should find in that row. than walk that row and add those items to my cart, should give me O(p*m) complicity.
but this never really happens: I can't remember more than three of four items from my list I'm expecting to find in that row, and even if I do, I often forget an item and have to use this algorithm again (lets say q times), which comes to: O(q*p*m).


A couple of comments I'd like to add:




when getting to the vegetable section for example, I found myself going over that section many times, cause my wife is a great cook and adds all kinds of vegetables in the list (v). Sure I can't remember all those veggies, and rather not stop next to each vegetable in the market (or should I?), to check if I've got it in my list.
Crossing out each item I put in my cart (or making a new list with all items I still don't have) is very time consuming for me, so I rather just go over the whole list each time I try to find if I still have an item to add to my cart.
Another case is the second you get in your car before going on a family vacation. You remember you forgot to take this that and though, running back to your house, getting some of these items, forgetting others and going back again. Is this a similar case?

Answers

Looking up rearrangement time here, my initial idea of putting in the time to map you usual items would be useless. (they re arrange aisles pretty often).



For any ideas going down that road - not going to work...





Question

3 day pay or quit


If i recieve a 3 day pay or quit.


Do you have to be out in the 3 days or do you get served further?



Answer

Re: 3 day pay or quit


Yes, as the notice states, you have 3 days to pay the rent amount stated in the notice or quit (vacate). The landlord is not required to provide you with any further notice if the rent was not paid.



The landlord's next step after expiration of the 3 day notice is to file and servre you with an unlawful detainer complaint. You have 5 days to file an answer to the complaint and can have a judgment for possession entered against you if you fail to timely repsond to complaint.



NOTICE: No attorney-client or confidential relationship is created through this communication. The information provided is of a general nature only and does not constitute legal advice or a legal opinion and requires that the poster obtain legal advice from an attorney to protect his or her rights.





Answer

Re: 3 day pay or quit


You can get close to a month by fighting an eviction, however, it will appear on your credit history.



If you choose to get out at once, you probably can stay a week after not answering the 3 day notice with cash.







Create an automatic launch-able Broadcast Receiver in Android

Problem:



I am willing to create an application that simply starts as a background process and whenever a new message comes into the device it should log it into a file or simply display a toast message.



I have read a lot of blogs and tried to follow the steps as mentioned. But, I keep on sending messages on my device and nothing displayed not even in device log. I want to run it on devices from Froyo to Lollipop. So, I am not willing to use new Telephony API which supports API 19 and later versions.



Manifest File



<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.abc.test"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >



<receiver android:name=".SMSHandler">

<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>

</application>

</manifest>


Source File



package com.abc.test;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class SMSHandler extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast toast = Toast.makeText(context, "message initiated",
Toast.LENGTH_LONG);
toast.show();

if (intent.getAction()
.equals("android.provider.Telephony.SMS_RECEIVED")) {
toast = Toast.makeText(context, "message received",
Toast.LENGTH_LONG);
toast.show();
}
}

}


Environment:



IDE:




Android Studio


Min SDK Version:




8


Tested On:




ICS Device (Sony Xperia U)
Kit-Kat (MOTO G)

Answers

You need to add an activity, then run that activity, before this BroadcastReceiver will work.



More accurately, something needs to use an explicit Intent before your app will be moved out of the stopped state and allow manifest-registered BroadcastReceivers to work. The simplest way to do that is to have a launcher activity, and run that activity from the launcher.



To learn more, see "Launch controls on stopped applications" in the Android 3.1 release notes, along with this blog post.





↑このページのトップヘ