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"

}
4

2 回答 2

3

试试这个

$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"
    }
}
于 2013-09-20T13:32:51.837 回答
2

我是否能够在 if 语句中使用带有布尔结果(-quiet 开关)的选择字符串语句

是的。从文档中:

-安静的

返回一个布尔值(true 或 false),而不是 MatchInfo 对象。如果找到模式,则值为“true”;否则,该值为“假”。

资源

您当前的脚本有问题吗?是什么促使这个问题开始的?

于 2013-09-20T13:34:01.707 回答