3

I have a script file sample.ps1 which has the following command :

copy-item C:\source -destination C:\destination.

Instead of hard coding the value for source and destination I want to pass it as arguments to the script.

copy-item $source -destination $destination.

I want to call this script from a stand alone client and pass the source and destination as parameters. I have the following program to execute the script file :

            string shellUri = "http://schemas.microsoft.com/powershell/Microsoft.PowerShell";
            string userName = "MachineName\\Administrator";
            string password = "Password";
            SecureString securePassword = new SecureString();

            foreach (char c in password)
            {
                securePassword.AppendChar(c);
            }
            PSCredential credential = new PSCredential(userName, securePassword);
            WSManConnectionInfo connectionInfo = new WSManConnectionInfo(false, "machinename", 5985, "/wsman", shellUri, credential);
            using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
            {

                runspace.Open();
        String file = "C:\\scripts\\Sample.ps1";
                Pipeline pipeline = runspace.CreatePipeline();

                pipeline.Commands.AddScript(System.IO.File.ReadAllText(file));
        }

I want to pass the parameters to the script Sample.ps1 through the C# program. How is that possible?

2 Answers 2

2

Here are the answers to all of your questions:

Sign up to request clarification or add additional context in comments.

Comments

0

Instead of reading the file and using Commands.AddScript to inject its contents as text, I'd rather use the Commands.Add method to add a command that would call the Sample.ps1 script directly, as a file:

pipeline.Commands.Add( "C:\\dirWithNoSpaces\\scripts\\Sample.ps1 -param1 value1");

but be careful about escaping.. I've just read that if there any spaces in the path, it has to be quoted:

pipeline.Commands.Add( "&\"C:\\dir with spaces\\scripts\\Sample.ps1\" -param1 value1");

However, if you'd like to keep it as you have it now, you might look for some ways to add 'parameters' in Pipeline or Runspace objects. For example, I've just found this: Runspace.InitialSessionState - you inject some initial variables to the runtime environment, so you could use it to pass values to inlined-scripts. However, the values will already be in-variables, not in the ARGV or such.

2 Comments

FYI: I have not tried it. I'm basing on this question. It should work, but still I just guess.
I tried the above method without parameter, but I am getting the following error : 'Unhandled Exception: System.Management.Automation.RemoteException: The term 'C:\scripts\Sample.ps1' is not recognized as the name of a cmdlet, function, scrip t file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.