Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

How to run a power shell scipt in a remote pc

$
0
0

Hi All,

Below is my code to execute powershell scripts wih aguments using C#.
Now i want to run scripts in a remote pc. In the method i will be passing remoteserver and credentials
public static IList<string> ExecutePowerShellScript(string scriptname, string args,string remote server,string username,string password

Can any one tell me how to execute script in a server which i passing using the credentials that im passing in the method
Can write the code any send me.plzzz

Thanks in advance

public static IList<string> ExecutePowerShellScript(string scriptname, string args)
        {
            List<string> list = new List<string>();

            Runspace runspace = RunspaceFactory.CreateRunspace();
            runspace.Open();
            Pipeline pipeline = runspace.CreatePipeline();
            Command cmd = new Command(scriptname + " " + args, true);
            //Command cmd = new Command(scriptname);
            //cmd.Parameters.Add(args);

            pipeline.Commands.Add(cmd);

            Collection<PSObject> results;
            results = pipeline.Invoke();

            foreach (PSObject obj in results)
            {
                list.Add(obj.ToString());
            }

            return list.ToArray();
        }


adityadugyala


Viewing all articles
Browse latest Browse all 21975

Trending Articles