powershell - Is return code required for Invoke-Command -


powershell script

  1. enables psremoting on remote computer
  2. executes setup.exe on remote computer
  3. disables psremoting on remote computer

how can sure disabling psremoting after remote computer able execute setup.exe?

am disabling psremoting before remote computer able execute setup.exe?

$password = get-content d:\script\cred.txt | convertto-securestring $credentials = new-object -typename system.management.automation.pscredential -argumentlist "administrator",$password $j = "remote_computer"  $comp = "\\"+$j  $exe = "setup.exe" [string]$cmd = "cmd /c 'c:\share\$exe'" [scriptblock]$sb = [scriptblock]::create($cmd)    $bstr = [system.runtime.interopservices.marshal]::securestringtobstr($password) $str =  [system.runtime.interopservices.marshal]::ptrtostringbstr($bstr) [system.runtime.interopservices.marshal]::zerofreebstr($bstr)    $enable_command = "d:\pstools\psexec.exe $comp -u administrator -p $str -accepteula powershell.exe c:\share\ps_enable.ps1"  invoke-expression $enable_command   try{     invoke-command -computername $j -credential $credentials -scriptblock $sb  } catch [system.exception]{     continue }   $disable_command = "d:\pstools\psexec.exe $comp -u administrator -p $str -accepteula powershell.exe c:\share\ps_disable.ps1"  invoke-expression $disable_command 

easy enough, use asjob switch invoke-command , assign variable. use wait-job know job completed before move on disabling psremoting.

try{     $setupjob = invoke-command -computername $j -credential $credentials -scriptblock $sb -asjob  } catch [system.exception]{     continue }  $setupjob|wait-job  $disable_command = "d:\pstools\psexec.exe $comp -u administrator -p $str -accepteula powershell.exe c:\share\ps_disable.ps1"  invoke-expression $disable_command 

Comments

Popular posts from this blog

c# - HttpResponseMessage System.InvalidOperationException -

sql - Postgresql error: "failed to find conversion function from unknown to text" -