This is the blog of DJ (@gkrew) and Brad, two guys who are just engineers on a messaging team supporting Microsoft Exchange Server. DJ is an IT Pro with experience as an Enterprise System Administrator in Microsoft Windows and Unix. Brad is a self-taught coder who develops code to assist with Exchange Administration using C# and PowerShell. There will be posts about Exchange, PowerShell, Active Directory, C# and scripts that you can use in your job as well as anything IT related.
Tuesday, July 19, 2016
How to run multiple PowerShell commands in c# (including pre-environment commands like set-adserversettings -viewentireforest:$true)
This allows me to create a script in a string with multiple commands, it processes 1 command at a time and then creates a runspace in c# and runs all of the powershell commands. The amount of commands that you could put inline in the string is endless.
<pre lang="C#">string scriptText = @"$pw = convertto-securestring -AsPlainText -Force -String '<password>'; $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist '<domain>\<username>', $pw; $session = new-pssession -ConfigurationName Microsoft.Exchange -ConnectionUri '<server url>/powershell' -credential $cred; import-pssession $session; Set-ExecutionPolicy bypass -confirm:$false -force; $pic = ([System.IO.File]::ReadAllBytes('" + <picture file name> + "')); set-userphoto -identity <username> -picturedata $pic -domaincontroller '<dc fqdn>' -confirm:$false;";
runExchangeShellScript(scriptText);</pre>
<pre lang="C#">private string runExchangeShellScript(string scriptText)
{
Runspace runspace = RunspaceFactory.CreateRunspace();
// open it
runspace.Open();
// create a pipeline and feed it the script text
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(scriptText);
RunspaceInvoke runSpaceInvoker = new RunspaceInvoke(runspace);
runSpaceInvoker.Invoke("Set-ExecutionPolicy Unrestricted");
// add an extra command to transform the script output objects into nicely formatted strings
// remove this line to get the actual objects that the script returns. For example, the script
// "Get-Process" returns a collection of System.Diagnostics.Process instances.
//pipeline.Commands.Add("Out-String");
// execute the script
Collection<PSObject> results = null;
try
{
results = pipeline.Invoke();
}
catch (Exception ex)
{
MessageBox.Show(ex.InnerException + " - " + ex.Message + " - " + ex.Source + " - " + ex.StackTrace + " - " + ex.TargetSite + " - " + ex.Data);
return "";
}
// close the runspace
runspace.Close();
// convert the script result into a single string
StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject obj in results)
{
stringBuilder.AppendLine(obj.ToString());
}
// return the results of the script that has
// now been converted to text
return stringBuilder.ToString();
}</pre>
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment