具有嵌入式IF语句的Powershell ForEach循环

时间:2015-07-20 20:39:09

标签: powershell if-statement foreach

开始编写powershell脚本(非常新),因为SCCM往往对它们(客户端和服务器)做出更好的响应 所以上面提到的是我的第一个脚本:

#Changes the 'ProvisioningMode' Key in the registry to False
$ProvisiongMode = New-ItemProperty -Path Registry::HKLM\SOFTWARE\Microsoft\CCM\CcmExec -Name ProvisioningMode -Value False -Force
#Clears or 'nulls' the SystemTaskExcludes key in the registry
$SystemTaskExludes = New-ItemProperty -Path Registry::HKLM\SOFTRWARE\Microsoft\CCM\CcmExec -Name SystemTaskExcludes - Value "" - Force
#----------------------------------------------------------------------------------------------
$Success = "C:\Path\to.log"
$Failure = "C:\Path\to.log"
$Computers = Import-Csv "C:\Path\to.csv"
$SearchStr = Get-ItemProperty -Path Registry::HKLM\SOFTWARE\Microsoft\CCM\CcmExec | select-object ProvisioningMode
$Online = Test-Conntection -Computername $ComputerName -Count 1 -Quiet

ForEach ($ComputerName in $Computers)
if ($Online -eq 'False')
{
    Write-Output $ComputerName`t'Connection Failed'  >> $Failure
}
Else
{
    if ($SearchStr -eq True)
     {
        $ProvisioningMode
        $SystemTaskExcludes 
     }

}

#Second Check
if ($SearchStr -eq 'False')
{
    Write-Output $ComputerName`t'Registry has been changed' >> $Success
}

有问题的是$Online变量。我想看看计算机是否对ping有响应,如果为true,则继续运行$ProvisioningMode$SystemTaskExclude。 然后另一个问题是查询该密钥以查看它是否发生了变化。这个问题是$SearchStr = Get-ItemProperty -Path Registry::HKLM\SOFTWARE\Microsoft\CCM\CcmExec | select-object ProvisioningMode返回

 ProvisionMode
 -----------------
 False

我只能抓住false数据。 就像我说的那样;非常新的powershell和写一些我将使用的东西可以帮助我学习。

编辑:我尝试过的是

ForEach ($Name in $Computers) 
{
Test-Connection -BufferSize 2 -Computername $Name.ComputerName -Count 1 -Quiet | Write-Output $Online
}

if ($Online -eq 'True') {Write-Output $Name`t'Computer is online' >> C:\Online.txt}

同一件事的许多变化。

 Test-Connection -BufferSize 2 -Computername $Name.ComputerName -Count 1 -Quiet 

返回Data,这是我想要的,但是我需要将它输入到If语句中,并且仍然保留$Name$StringStr和日志文件。

有些人想知道,这会在运行OSD时使客户端退出配置模式。它修复了“没有自签名证书”的问题。问题。

1 个答案:

答案 0 :(得分:3)

尽管PowerShell中布尔值的字符串表示形式为TrueFalse,但正确比较此类值的正确方法是使用$true$false变量

此外,使用Test-Connection$Online的结果分配给=

$Online = Test-Connection -BufferSize 2 -Computername $Name.ComputerName -Count 1 -Quiet

if($Online -eq $true){
    # Machine responds to ping, do stuff!
}

但这种比较实际上是不必要的。如果$Online已经等于$frue$false,您可以在if语句中单独使用它:

if($Online){
    # Machine responds to ping, do stuff!
}

我认为$ProvisionMode$SystemTaskExcludes$SearchStr都是您要在远程计算机上执行的语句,而不是SCCM服务器本身。

为此,您需要连接到计算机并指示它执行* -ItemProperty语句。

# Enclosing statements in {} creates a ScriptBlock - a piece of code that can be invoked later!
$ProvisionMode = { 
    #Changes the 'ProvisioningMode' Key in the registry to False
    New-ItemProperty  -Path Registry::HKLM\SOFTWARE\Microsoft\CCM\CcmExec -Name ProvisioningMode -Value False -Force 
}

$SystemTaskExludes = { 
    #Clears or 'nulls' the SystemTaskExcludes key in the registry
    New-ItemProperty -Path Registry::HKLM\SOFTRWARE\Microsoft\CCM\CcmExec -Name SystemTaskExcludes - Value "" - Force 
}

$SearchStr = { 
    Get-ItemProperty -Path Registry::HKLM\SOFTWARE\Microsoft\CCM\CcmExec | Select-Object -ExpandProperty ProvisioningMode
}

#----------------------------------------------------------------------------------------------
$LogFilePath = "C:\Path\to.log"

$Computers = Import-Csv "C:\Path\to.csv"


foreach($Computer in $Computers){

    $Online = Test-Connection -Computername $Computer.Name -Count 1 -Quiet

    if(-not $Online)
    {
        "$ComputerName`t'Connection Failed'" | Out-File -FilePath $LogFilePath -Append
    }
    else
    {
        $SearchResult = Invoke-Command -ComputerName $Computer.Name -ScriptBlock $SearchStr
        if ($SearchResult)
        {
            # The call operator (&) invokes the scriptblock
            Invoke-Command -ComputerName $Computer.Name -ScriptBlock $ProvisionMode
            Invoke-Command -ComputerName $Computer.Name -ScriptBlock $SystemTaskExludes
        }
        else # SearchStr must be $false, or non-existing
        {
            "$ComputerName`t'Registry has been changed'" | Out-File -FilePath $LogFilePath -Append
        }
    }
}

为简单起见,我已将Invoke-Command-ComputerName参数一起使用,但在实际情况下,我会设置一个带New-PSSession的PSSession,并将其重用于与Invoke-Command -Session

的连接