0

Powershell スクリプト

  1. リモート コンピューターで PSRemoting を有効にします
  2. リモート コンピューターで setup.exe を実行します。
  3. リモート コンピューターで PSRemoting を無効にします

リモート コンピューターが setup.exe を実行できるようになった後、PSRemoting を無効にしていることを確認するにはどうすればよいですか?

リモート コンピューターが setup.exe を実行できるようになる前に、PSRemoting を無効にしていますか?

$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
4

1 に答える 1

1

Invoke-Command に AsJob スイッチを使用し、それを変数に割り当てるのは簡単です。次に、Wait-Job を使用して、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
于 2014-09-10T18:31:00.977 に答える