このようにC#コードからpowershellを開きたいとします。しかし、私はそれが出てくると、PowerShellに資格を設定したい。メモリ内に資格情報を持つpowershellを開く
Process myProcess = new Process();
try
{
myProcess.StartInfo.UseShellExecute = true;
myProcess.StartInfo.FileName = @"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe";
myProcess.StartInfo.CreateNoWindow = false;
myProcess.Start();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
PowerShellは、私がリモートコンピュータに接続するには、Enter-PSSessionコマンドを実行したいが、私はそこにユーザー名とパスワードの入力を要求したくない実行されている基本的に後。
私はこれができることを知っています。
PS C:\> $C = Get-Credential
PS C:\> Enter-PsSession -ComputerName "Server01" -Credential $C
私の代わりに私がPowerShellを起動する際に変数に設定したい、資格情報をユーザーに依頼する必要はありません。どうすればこれを達成できますか?
現在のアプローチは、折り目2で、
はC#
string cmd = "\"" + mypwd+ "\"" + @" | ConvertTo-SecureString -AsPlainText -Force | convertfrom-securestring | out-file output";
PowerShellInstance.AddScript(cmd);
Collection<PSObject> PSOutput = PowerShellInstance.Invoke();
を経由してパスワードをダンプPowerShellで
$pwd = Get-Content .\output | ConvertTo-SecureString
$localcred = New-Object -typename System.Management.Automation.PSCredential -argumentlist "myusername",$pwd
Enter-PSSession -ComputerName 192.168.2.51 -Credential $localcred
それを読んで滑らかな印象方法はありますか?
Aaと私はあなたのためにPowerShellでICredentialオブジェクトを(私はSharepointのためのすべての時間をそれを行う)を使用することができます知っているこれまでのようにPSCredentialオブジェクトNetworkCredentialオブジェクトを使用することも有効です。ただし、資格情報をハードコードするのは悪い習慣です。 – bluuf