Powershell ForEach loop with embedded IF statements -


starting write powershell scripts (very new) because sccm tends respond better them (both client , server) above stated here first script:

#changes 'provisioningmode' key in registry false $provisiongmode = new-itemproperty -path registry::hklm\software\microsoft\ccm\ccmexec -name provisioningmode -value false -force #clears or 'nulls' systemtaskexcludes key in 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 } 

the issue in question $online variable. see if computer responsive ping, if true proceed run $provisioningmode , $systemtaskexclude. other issue querying key see if changed. issue 1 $searchstr = get-itemproperty -path registry::hklm\software\microsoft\ccm\ccmexec | select-object provisioningmode returns

 provisionmode  -----------------  false 

and cant grab false data. stated; new @ powershell , writing use helps me learn.

edit: have tried is

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 online' >> c:\online.txt} 

and many variations of same thing.

 test-connection -buffersize 2 -computername $name.computername -count 1 -quiet  

returns data, want, need input if statement , still retain $name $stringstr , log files.

those of wondering, takes client out of provisioning mode when running osd. fixes 'no self-signed certificate' issue.

even though string representations of boolean values in powershell true , false, correct way compare againt such value $true , $false variables.

furthermore, assign result of test-connection $online =:

$online = test-connection -buffersize 2 -computername $name.computername -count 1 -quiet  if($online -eq $true){     # machine responds ping, stuff! } 

but comparison unnecessary. if $online equals $frue or $false, can use on own inside if statement:

if($online){     # machine responds ping, stuff! } 

i assume $provisionmode, $systemtaskexcludes , $searchstr statements want execute on remote machine, not on sccm server itself.

to so, need connect machine , instruct execute *-itemproperty statements.

# enclosing statements in {} creates scriptblock - piece of code can invoked later! $provisionmode = {      #changes 'provisioningmode' key in registry false     new-itemproperty  -path registry::hklm\software\microsoft\ccm\ccmexec -name provisioningmode -value false -force  }  $systemtaskexludes = {      #clears or 'nulls' systemtaskexcludes key in 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)         {             # call operator (&) invokes scriptblock             invoke-command -computername $computer.name -scriptblock $provisionmode             invoke-command -computername $computer.name -scriptblock $systemtaskexludes         }         else # searchstr must $false, or non-existing         {             "$computername`t'registry has been changed'" | out-file -filepath $logfilepath -append         }     } } 

for simplicity, i've used invoke-command -computername parameter, in real world situation, set pssession new-pssession, , reuse connection invoke-command -session


Comments