You are viewing limited content. For full access, please sign in.

Question

Question

How to run a powershell script on another server from workflow?

asked on December 18, 2018

I could use some help with either c# or .Net as I'm not familiar with either.

0 0

Replies

replied on December 18, 2018

I do it with the following script, but you'll either need to type out the credentials for an account that has the necessary permissions on the server, or have Workflow Service running under a domain account that has permissions on the server.

            // Connection credentials
            string acc = "username";
            string pwd = "password";
            SecureString spwd = new SecureString();

            // Convert password to secure string
            foreach (char X in pwd){
                spwd.AppendChar(X);
            }

            PSCredential credential = new PSCredential(acc,spwd);

            WSManConnectionInfo ci = new WSManConnectionInfo();
            ci.ComputerName = "Remote Computer Name"
            ci.Credential = credential;
            ci.AuthenticationMechanism = AuthenticationMechanism.Default;

            using(Runspace rs = RunspaceFactory.CreateRunspace(ci)){
                rs.Open();

                using(Pipeline p = rs.CreatePipeline()){
                    Command cmd = new Command("Power Shell Command");
                    cmd.Parameters.Add("Parameter Name","Parameter Value");
                    p.Commands.Add(cmd);
                    p.Invoke();

                    if(p.Error.Count > 0){
                        StringBuilder err = new StringBuilder();
                        while(!p.Error.EndOfPipeline){
                            err.AppendLine(p.Error.Read().ToString());
                        }

                        MsgBox(err.ToString());
                    }
                }
            }

I made some modifications to remove the parts that were specific to my process, so this won't run as-is, but it should point you in the right direction. I'd recommend allowing your workflow service account permissions on the target PC rather than typing out the credentials, I only provided that so you could see how it would be done.

 

You'll also need the following namespaces

System.Management.Automation
System.Management.Automation.Runspaces
System.Security

2 0
You are not allowed to follow up in this post.

Sign in to reply to this post.