2014-09-18 6 views
0

Я удаленного подключения к консоли PowerShell с помощью C#:Cast в 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. Что такое «обычный» способ решить эту проблему?

+0

http://msdn.microsoft.com/en-us/library/system.management.automation.powershell%28v=vs.85%29.aspx – Saren

ответ

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. 
} 

 Смежные вопросы

  • Нет связанных вопросов^_^