Powershell ForEach loop with embedded IF statements

Forum: Config Manager
Viewing 1 post (of 1 total)
• #230982

sandeep kumar
Participant

Hi all, I am Trained on SCCM Starting to write powershell scripts (very new) because SCCM tends to respond better to them (both client and server) So with the above stated here is my first script:
<pre class=”lang:default decode:true “>#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$ComputerNamet’Connection Failed’ >> $Failure } Else { if ($SearchStr -eq True)
{
$ProvisioningMode$SystemTaskExcludes
}

}

#Second Check
if ($SearchStr -eq ‘False’) { Write-Output$ComputerNamet’Registry has been changed’ >> $Success The issue in question is the $Online variable. I would like to see if a computer is responsive to ping, if true then proceed to run $ProvisioningMode and $SystemTaskExclude. Then the other issue is querying that key to see if it changed. The issue with that one is $SearchStr = Get-ItemProperty -Path Registry::HKLM\SOFTWARE\Microsoft\CCM\CcmExec | select-object ProvisioningMode returns <pre class=”lang-bsh prettyprint prettyprinted”> <span class="typ">ProvisionMode</span> <span class="pun">-----------------</span> <span class="typ">False</span> And I cant grab just the false data. Like I stated; very new at powershell and writing something that I will use helps me learn SCCM. Edit: What I Have tried is <pre class=”lang:default decode:true “>ForEach ($Name in $Computers) { Test-Connection -BufferSize 2 -Computername$Name.ComputerName -Count 1 -Quiet | Write-Output $Online } if ($Online -eq ‘True’) {Write-Output $Namet’Computer is online’ >> C:\Online.txt} And many variations of the same thing. <pre class=”lang-bsh prettyprint prettyprinted”> <span class="typ">Test</span><span class="pun">-</span><span class="typ">Connection</span> <span class="pun">-</span><span class="typ">BufferSize</span> <span class="lit">2</span> <span class="pun">-</span><span class="typ">Computername</span><span class="pln">$Name</span><span class="pun">.</span><span class="typ">ComputerName</span> <span class="pun">-</span><span class="typ">Count</span> <span class="lit">1</span> <span class="pun">-</span><span class="typ">Quiet</span> 
Returns Data, which is what I want, but I need to input that into an If statement and still retain the $Name for the $StringStr` and log files.

Those of you wondering, this takes the client out of provisioning mode when running an OSD. It fixes the ‘No Self-Signed Certificate’ issue.

Thanks.

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.