Click here to Skip to main content
15,941,690 members
Home / Discussions / C#
   

C#

 
QuestionHow can I run multiple powershell commands in a C#/powershell runspace? Pin
turbosupramk318-Jul-16 4:02
turbosupramk318-Jul-16 4:02 
QuestionRe: How can I run multiple powershell commands in a C#/powershell runspace? Pin
Richard MacCutchan18-Jul-16 4:22
mveRichard MacCutchan18-Jul-16 4:22 
AnswerRe: How can I run multiple powershell commands in a C#/powershell runspace? Pin
turbosupramk318-Jul-16 4:35
turbosupramk318-Jul-16 4:35 
GeneralRe: How can I run multiple powershell commands in a C#/powershell runspace? Pin
Richard MacCutchan18-Jul-16 4:51
mveRichard MacCutchan18-Jul-16 4:51 
GeneralRe: How can I run multiple powershell commands in a C#/powershell runspace? Pin
turbosupramk318-Jul-16 4:58
turbosupramk318-Jul-16 4:58 
GeneralRe: How can I run multiple powershell commands in a C#/powershell runspace? Pin
Richard MacCutchan18-Jul-16 6:15
mveRichard MacCutchan18-Jul-16 6:15 
GeneralRe: How can I run multiple powershell commands in a C#/powershell runspace? Pin
turbosupramk318-Jul-16 7:49
turbosupramk318-Jul-16 7:49 
GeneralRe: How can I run multiple powershell commands in a C#/powershell runspace? Pin
turbosupramk319-Jul-16 4:10
turbosupramk319-Jul-16 4:10 
Here is what I ended up doing, 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


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);



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();
        }

QuestionClose Form Popup before opening MessageBox.Show and the form use delegate ? Pin
Member 245846717-Jul-16 22:47
Member 245846717-Jul-16 22:47 
AnswerRe: Close Form Popup before opening MessageBox.Show and the form use delegate ? Pin
OriginalGriff17-Jul-16 22:51
mveOriginalGriff17-Jul-16 22:51 
Questionc# parser questions. Pin
elfenliedtopfan517-Jul-16 13:13
elfenliedtopfan517-Jul-16 13:13 
AnswerRe: c# parser questions. Pin
OriginalGriff17-Jul-16 21:42
mveOriginalGriff17-Jul-16 21:42 
GeneralRe: c# parser questions. Pin
elfenliedtopfan518-Jul-16 3:31
elfenliedtopfan518-Jul-16 3:31 
GeneralRe: c# parser questions. Pin
OriginalGriff18-Jul-16 4:11
mveOriginalGriff18-Jul-16 4:11 
GeneralRe: c# parser questions. Pin
elfenliedtopfan518-Jul-16 7:24
elfenliedtopfan518-Jul-16 7:24 
GeneralRe: c# parser questions. Pin
OriginalGriff18-Jul-16 8:02
mveOriginalGriff18-Jul-16 8:02 
GeneralRe: c# parser questions. Pin
elfenliedtopfan518-Jul-16 9:28
elfenliedtopfan518-Jul-16 9:28 
GeneralRe: c# parser questions. Pin
OriginalGriff18-Jul-16 9:33
mveOriginalGriff18-Jul-16 9:33 
GeneralRe: c# parser questions. Pin
elfenliedtopfan518-Jul-16 10:31
elfenliedtopfan518-Jul-16 10:31 
QuestionContextMenu on a MenuItem in ContextMenu possible in WPF? Pin
Imagiv16-Jul-16 4:08
Imagiv16-Jul-16 4:08 
QuestionRe: ContextMenu on a MenuItem in ContextMenu possible in WPF? Pin
User 1106097916-Jul-16 7:53
User 1106097916-Jul-16 7:53 
AnswerRe: ContextMenu on a MenuItem in ContextMenu possible in WPF? Pin
Imagiv18-Jul-16 1:19
Imagiv18-Jul-16 1:19 
GeneralRe: ContextMenu on a MenuItem in ContextMenu possible in WPF? Pin
User 1106097918-Jul-16 1:28
User 1106097918-Jul-16 1:28 
GeneralRe: ContextMenu on a MenuItem in ContextMenu possible in WPF? Pin
Imagiv18-Jul-16 2:02
Imagiv18-Jul-16 2:02 
GeneralRe: ContextMenu on a MenuItem in ContextMenu possible in WPF? Pin
Pete O'Hanlon18-Jul-16 2:18
mvePete O'Hanlon18-Jul-16 2:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.