Tuesday

Running PowerShell script from C#



private void testRunToolStripMenuItem_Click(object sender, EventArgs e)
{
List<string> ls = new List<string>();
RunPowershellScript(@"c:\Program Files\Microsoft Transporter Tools\PassCh.ps1",ls);
//log("script complete");

}

private static void RunPowershellScript(string scriptFile, List<string> parameters)
{
// Validate parameters
if (string.IsNullOrEmpty(scriptFile)) { throw new ArgumentNullException("scriptFile"); }
if (parameters == null) { throw new ArgumentNullException("parameters"); }

RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
PSSnapInException ex;
runspaceConfiguration.AddPSSnapIn("Microsoft.Exchange.Transporter",out ex);

using (Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration))
{
runspace.Open();
RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
scriptInvoker.Invoke("Set-ExecutionPolicy Unrestricted");
Pipeline pipeline = runspace.CreatePipeline();
Command scriptCommand = new Command(scriptFile);
Collection<CommandParameter> commandParameters = new Collection<CommandParameter>();
foreach (string scriptParameter in parameters)
{
CommandParameter commandParm = new CommandParameter(null, scriptParameter);
commandParameters.Add(commandParm);
scriptCommand.Parameters.Add(commandParm);
}
pipeline.Commands.Add(scriptCommand);
Collection<PSObject> psObjects;
psObjects = pipeline.Invoke();
}
}

more...

No comments:

imagemagic add text to image

rem different types of text annotations on existing images rem cyan yellow orange gold rem -gravity SouthWest rem draw text and anno...