Author Posts

July 8, 2017 at 9:42 pm

Wrote a script which will loop through multiple server and store the output in a file. But, the script is giving me an error.

Get-Content .\server.txt |
get-childitem "c:\test\*" -recurse | SELECT-STRING -pattern "$11t$dpo3"
Select path, pattern, Linenumber |
Export-Csv C:\Users\admin\Desktop\Test\Search_String\string_match_results.csv -NoType

Select-String : Cannot bind argument to parameter 'Pattern' because it is an empty string.
At C:\Users\admin\Desktop\Test\Search_String\Final.ps1:5 char:68
+ get-childitem "c:\opscripts\*" -recurse | SELECT-STRING -pattern "$11t$dpo3"
+ ~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Select-String], ParameterBindingValidationExceptio
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.Powe

Can someone please guide me on this.


July 9, 2017 at 12:13 am

The error message is actually pretty clear. The "pattern" you provide for your Select-String is empty. Why? Because in Powershell variables start with a $ sign. So Powershell "thinks" you want to provide the two variables "$11t" and "$dpo3". And they are – I assume – empty because you did not declare them, right?

What do you actually mean with "$11t$dpo3"? Should it be taken literal? Then you could enclose it in single quotes and use the parameter -SimpleMatch. If it should be a regular expression I think it does not make sense this way. Because the $ sign represents the end of the line or the end of the string and it does not make sense to have two of them in one short string.

July 9, 2017 at 3:00 am

Thanks. -SimpleMatch worked.