【问题标题】:Opening a powershell with credentials in memory使用内存中的凭据打开 powershell
【发布时间】:2017-02-16 18:54:00
【问题描述】:

我想像这样从 C# 代码打开 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 时将其设置在变量中。我怎样才能做到这一点?

目前的方法有两种,

通过 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 对象也应该有效。但是,硬编码凭据是一种不好的做法。

标签: c# powershell


【解决方案1】:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-02
    • 2019-04-03
    相关资源
    最近更新 更多