Error when running powershell command remotely

I want to run Powershell command on remote machine. This is method that I am using (localhost:131 is because I use tunnel to remote machine's port 5985):

 public string RunRemotePowerShellCommand(string command) { System.Security.SecureString password = new System.Security.SecureString(); foreach (char c in _password.ToCharArray()) { password.AppendChar(c); } string schema = ""; WSManConnectionInfo connectionInfo = new WSManConnectionInfo(false, "localhost", 131, "/wsman", schema, new PSCredential(_domain + @"\" + _userName, password)); using (Runspace remoteRunspace = RunspaceFactory.CreateRunspace(connectionInfo)) { remoteRunspace.Open(); using (PowerShell powershell = PowerShell.Create()) { powershell.Runspace = remoteRunspace; powershell.AddCommand(command); powershell.Invoke(); Collection<PSObject> results = powershell.Invoke(); // convert the script result into a single string StringBuilder stringBuilder = new StringBuilder(); foreach (PSObject obj in results) { stringBuilder.AppendLine(obj.ToString()); } return stringBuilder.ToString(); } } } 

I'm trying to run following command:

D:\FolderName\scriptName.ps1 -action editbinding -component "comp1","comp2","comp3","comp4" 

Like this:

RunRemotePowerShellCommand(@"D:\FolderName\scriptName.ps1 -action editbinding -component ""comp1"",""comp2"",""comp3"",""comp4"""); 

but I get:

Error: System.Management.Automation.RemoteException: The term 'D:\FolderName\scriptName.ps1 -action editbinding -component "comp1","comp2","comp3","comp4"' is not recognized as a name of cmdlet, function, script file, or operable program. Check the spelling of the name, or if the path is included, verify that the path is correct and try again. 

The method works fine with simple commands, and the command that I want to run is ok when I run it on remote machine.

Thanks in advance.

Regards, Dusan

2 Answers

You need to use the powershell.AddParameter() method to add the parameters for your command. The AddCommand() call should name just the command: cmdlet name, function name, path to script, etc. From the docs:

PowerShell ps = PowerShell.Create(); ps.AddCommand("Get-Process"); ps.AddArgument("wmi*"); ps.AddCommand("Sort-Object"); ps.AddParameter("descending"); ps.AddArgument("id"); 
1

I has a similar requirement.

My solution was to create a powershell function in C# code and use it over the powershell remote session like.

using System; using System.Management.Automation; namespace PowerShellTest { class Program { static void Main(string[] args) { string func = @"function Test { Write-Host 'hello' };"; PowerShell ps = PowerShell.Create(); ps.AddScript(func); ps.Invoke(); ps.AddCommand("Test"); ps.Invoke(); Console.WriteLine("Successfully executed function"); Console.ReadLine(); } } } 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like