私は、リモートマシン上で実行されるPowerShellスクリプトから終了コードを返そうとしています。しかし、ExitCode
にチェックすると乱数があります。私のPowerShellスクリプトが間違った終了コードを返すのはなぜですか?
私は間違っていますか?さらに、テキスト全体を返すことは可能ですか?
私のスクリプト
$proc = Start-Process -Filepath "$PSExec" -ArgumentList "\\$server -h -u $user -p $pass -d PowerShell $command" -PassThru -Wait
$proc.ExitCode
リモートスクリプト
New-Item "c:\temp\1.txt" -type file -force
exit 123
UPDATE
$secureString = ConvertTo-SecureString $password -Force -AsPlainText #$password includes password in clear text
$cred = New-Object System.Management.Automation.PSCredential($usrName, $secureString)
$sess = New-PSSession -ComputerName $serverName -Credential $cred
$command = "`"C:\temp\1.ps1`""
$result = Invoke-Command -Session $sess -ScriptBlock {
Start-Process -Filepath "$PSExec" -ArgumentList "\\$server -h -u $usrName -p $password -d PowerShell $command" -PassThru -Wait
}
@whenは(ゲット-資格を)'それは、ユーザー名とパスワードを入力するポップアップウィンドウが開きます。スクリプトでユーザー名とパスワードを渡す方法は? – theateist
'New-PSSession'に' Logon failure'例外がスローされます。'PSExec'がサーバーにアクセスするのに成功したので、ユーザー名とパスワードが正しいことが分かりました。私は、コードを使って投稿に更新を加えました。ありがとう – theateist
私のパスワードで何かができますか?私のパスワードは '$ password =" $ mypass! "です。ここには表示されていませんが、 "$"記号の前に "$"記号をエスケープするには ""文字を使用します。それが正しいか? – theateist