Im trying to write a very brief powershell script that runs a few commands, pipes their output to a text file, and then does a search against a keyword.
I cant figure out what to change however for this line:
wmic service get name, startname | out-File "$pwd\admin\wmic.txt"
WMIC.exe : Invalid GET Expression.
At \\test.ps1:7 char:5
+ wmic <<<< service get name startname | out-File "$pwd\admin\wmic.txt"
+ CategoryInfo : NotSpecified: (Invalid GET Expression.:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
I believe the error is generated from the ',' as powershell uses the comma to create an array. Any thoughts or suggestions?
Thanks
Commas are array separators in PowerShell. You should either escape them or encase the parameter in double quotes and then write some logic to split your string that contains commas. E.g. Your cmdlet call should be: Get-Weather "Shrewsbury,MA?
Subexpression operator $( ) For a single result, returns a scalar. For multiple results, returns an array. Use this when you want to use an expression within another expression. For example, to embed the results of command in a string expression. PowerShell Copy.
The “$_” is said to be the pipeline variable in PowerShell. The “$_” variable is an alias to PowerShell's automatic variable named “$PSItem“. It has multiple use cases such as filtering an item or referring to any specific object.
wmic service get name"," startname| out-File "$pwd\admin\wmic.txt"
This is easy to resolve comma, no change at all.
Beginning with PowerShell 3.0 there is the new escaping operator --%
available (see magic parameter in the release notes):
wmic --% service get name, startname | out-File "$pwd\admin\wmic.txt"
This operator is known as the stop-parsing operator, and it instructs PowerShell to stop interpreting input from that point forward as PowerShell commands or expressions (see docs here).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With