Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有嵌入式IF语句的Powershell ForEach循环_Powershell_If Statement_Foreach - Fatal编程技术网

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

带有嵌入式IF语句的Powershell ForEach循环,powershell,if-statement,foreach,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

开始编写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,则继续运行
$provisiongmode
$SystemTaskExclude
。 然后,另一个问题是查询该键以查看它是否更改。这个问题是
$SearchStr=Get ItemProperty-Path Registry::HKLM\SOFTWARE\Microsoft\CCM\CcmExec | select object provisiongmode
返回

 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 
返回我想要的数据,但我需要将其输入到
If
语句中,并且仍然保留
$StringStr
和日志文件的
$Name


您可能想知道,这会使客户端在运行OSD时脱离配置模式。它修复了“无自签名证书”问题

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

此外,使用
=
测试连接的结果分配给
$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命令
-ComputerName
参数,但在实际情况下,我会使用
New PSSession
设置PSSession,对于与
Invoke Command-Session

的连接,即使PowerShell中布尔值的字符串表示形式是
True
False
,对此类值进行比较的正确方法是使用
$True
$False
变量

此外,使用
=
测试连接的结果分配给
$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命令
-ComputerName
参数,但在实际情况下,我会使用
New PSSession
设置PSSession,对于与
Invoke Command-Session

的连接,即使PowerShell中布尔值的字符串表示形式是
True
False
,对此类值进行比较的正确方法是使用
$True
$False
变量

此外,使用
=
测试连接的结果分配给
$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命令
-ComputerName
参数,但在实际情况下,我会使用
New PSSession
设置PSSession,对于与
Invoke Command-Session

的连接,即使PowerShell中布尔值的字符串表示形式是
True
False
,对此类值进行比较的正确方法是使用
$True
$False
变量

此外,使用
=
测试连接的结果分配给
$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

阅读“嵌入式”的描述-taghaha,嗯,它被认为是“嵌入if语句”。我的错,奥拉夫。读一下“嵌入”的描述——塔哈哈,嗯,它应该是“嵌入如果”