0

i am trying to write a script to check if a string is within a file, if it is then carry on and check the next one and continue until all are checked, if it finds one that is not present then the script will terminate. Am i able to use a select-string statement with a boolean outcome (-quiet switch) within an if statement. The pinglist.txt file contains a list of servers and the other _pinglist.ini contains a large list of servers.I am new to this so please excuse my poor scripting. Thanks

$Original_File = 'C:\Jobs\Powershell\Pinger\Other_PingList.ini'

$PingList= 'C:\Jobs\Powershell\pinglist.txt'

#

Foreach ($item in $pingList)

{If (Select-String $Original_File -pattern $item -quiet) {

Write-Host "Servers Present In File - Carrying on"

}}
Else {

   Write-Host "Servers Not Present In File - Terminating"

}
alroc
  • 27,574
  • 6
  • 51
  • 97
Mach1
  • 13
  • 1
  • 1
  • 5

2 Answers2

3

give this a try

$Original_File = gc 'C:\Jobs\Powershell\Pinger\Other_PingList.ini'
$PingList= gc 'C:\Jobs\Powershell\pinglist.txt'    

Foreach ($item in $pingList)
{
    If ($Original_File -match $item ) 
    {
        Write-Host "Servers $item Present In File - Carrying on"
    }
    Else
    {
        Write-Host "Servers $item Not Present In File - Terminating"
    }
}
CB.
  • 58,865
  • 9
  • 159
  • 159
  • Hi, thanks for the response, i tried that and received the following error, Bad argument to operator '-match': parsing "C:\Jobs\Powershell\pinglist.txt" - Unrecognized escape sequence \J.. At line:11 char:30 + If ($Original_File -match <<<< $item ) + CategoryInfo : InvalidOperation: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : BadOperatorArgument – Mach1 Sep 20 '13 at 13:37
  • 2
    @Mach1 look @ the `gc` (alias for get-content cmdlet) in the variable declaration... I think you missed it! Copy&Paste all my code... – CB. Sep 20 '13 at 13:41
  • 2
    @Mach1 Make sure you mark an answer as "Accepted" if it resolved your question. – tnw Sep 20 '13 at 13:56
2

Am i able to use a select-string statement with a boolean outcome (-quiet switch) within an if statement

Yes. From the documentation:

-Quiet

Returns a Boolean value (true or false), instead of a MatchInfo object. The value is "true" if the pattern is found; otherwise, the value is "false".

Source

Are you having an issue with your current script? What prompted this question to begin with?

tnw
  • 13,521
  • 15
  • 70
  • 111
  • Hi, yes, it was throwing an error out. parsing "C:\Jobs\Powershell\pinglist.txt" - Unrecognized escape sequence \J. At line:9 char:19 + {If (Select-String <<<< $Original_File -pattern $item -quiet) { + CategoryInfo : NotSpecified: (:) [Select-String], ArgumentException + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.SelectStringCommand – Mach1 Sep 20 '13 at 13:43
  • @Mach1 Could be that your `Foreach` block doesn't include your `Else`. You should move the extra bracket on the line after `Write-Host` to the far side of the `Else`. That way, both the `If` and `Else` are contained within the `Foreach` – tnw Sep 20 '13 at 13:50
  • 1
    @Mach1 and @C.B. had a good suggestion as well, you need to use `$Original_File = Get-Content "...."`, – tnw Sep 20 '13 at 13:53