0

我正在使用 C# 远程连接到 PowerShell 控制台:

using (Runspace remoteRunspace = RunspaceFactory.CreateRunspace(setUpConnection()))
{
    remoteRunspace.Open();
    using (PowerShell powershell = PowerShell.Create())
    {
        powershell.Runspace = remoteRunspace;

        DateTime dateTime = GetTime(powershell);  // <------ How to implement?
    }
    remoteRunspace.Close();
}

我想调用Get-Date PowerShell 命令并以某种方式PSObject转换为DateTime. 解决此问题的“通常”方法是什么?

4

1 回答 1

0

使用PSObject.BaseObject属性:

using (Runspace remoteRunspace = RunspaceFactory.CreateRunspace(setUpConnection()))
{
    remoteRunspace.Open();
    using (PowerShell powershell = PowerShell.Create())
    {
        powershell.Runspace = remoteRunspace;

        DateTime dateTime = (DateTime)powershell.Invoke().Single().BaseObject;
    }
    // No need to close runspace; you are disposing it.
}
于 2014-09-18T14:06:08.690 回答