usingSystem.Management.Automation;usingSystem.Management.Automation.Runspaces;internalvoidrunPowershellRemotely(stringlocation,stringscriptToBeRun){stringuserName=ConfigurationManager.AppSettings["RemoteMachineLogonUser"];stringpassword=ConfigurationManager.AppSettings["RemoteMachineUserPassword"];varsecurestring=newSecureString();foreach(Charcinpassword){securestring.AppendChar(c);}PSCredentialcreds=newPSCredential(userName,securestring);// Remove logging if not neededlog.Info(String.Format("\tPOWERSHEL : Running Powershell {0} at location {1}",scriptToBeRun,location));WSManConnectionInfoconnectionInfo=newWSManConnectionInfo();connectionInfo.ComputerName=ConfigurationManager.AppSettings["RemoteMachine"];connectionInfo.Credential=creds;Runspacerunspace=RunspaceFactory.CreateRunspace(connectionInfo);runspace.Open();using(PowerShellps=PowerShell.Create()){ps.Runspace=runspace;ps.AddScript(@"cd "+location);ps.AddScript(scriptToBeRun);try{varresults=ps.Invoke();log.Info("\tPOWERSHEL : Results from Powershell Script is ---------------------------");foreach(varxinresults){log.Info(x.ToString());}log.Info("\tPOWERSHEL : End of results--------------------------------- ---------------------------");}catch(Exceptione){log.Error("\tPOWERSHEL : Exception from running Powershell Script is"+e.ToString());}}runspace.Close();}