I am trying to run the invoke-command cmdlet using C #, but I cannot figure out the correct syntax. I just want to run this simple command:
invoke-command -ComputerName mycomp.mylab.com -ScriptBlock {"get-childitem C:\windows"}
In C # code, I did the following:
InitialSessionState initial = InitialSessionState.CreateDefault(); Runspace runspace = RunspaceFactory.CreateRunspace(initial); runspace.Open(); PowerShell ps = PowerShell.Create(); ps.Runspace = runspace; ps.AddCommand("invoke-command"); ps.AddParameter("ComputerName", "mycomp.mylab.com"); ps.AddParameter("ScriptBlock", "get-childitem C:\\windows"); foreach (PSObject obj in ps.Invoke()) {
When I run this, I get an exception:
Cannot bind parameter 'ScriptBlock'. Cannot convert the "get-childitem C:\windows" value of type "System.String" to type "System.Management.Automation.ScriptBlock".
I assume that I need to use the ScriptBlock type here, but don't know how to do it. This is a simple example to get started, in the real case of use, a larger script block will be involved with several commands in it, so any help on how to do this will be highly appreciated.
thanks
source share