dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Run Powershell commands inside Winform C# with another users crfedential

  1. #1
    Join Date
    Sep 2016
    Posts
    2

    Run Powershell commands inside Winform C# with another users crfedential

    When I click the view button it executes the Powershell code successfully and displays the results in 3 different textboxes. This is working as should.

    But I need to run this with another user credential and this should happen when I click the command button.

    We are not going to get access to that remote computer with our current user and password.. I need a workaround for this.

    1. I do not want a prompt for entering the user name and password.
    2. I want this to be transparent.

    I have looked and try solutions from other sites but these where for running in Powershell and not in Visual Studio C# Winforms.

    Any assistance is appreciated.


    Code:
    {
                try
                {
                   
                    //string Test = textBoxScript.Text;
                    txtTaskNotResponding.Clear();
    
                    //Returns all the Process running
                    txtTaskList.Text = RunScript("Get-Process -ComputerName pcname | Where-Object {$_.responding -eq $True} | Format-Table -AutoSize ProcessName, Responding, CPU");
    
                    //Returns all Process that are not running
                    txtTaskNotResponding.Text = RunScript("Get-Process -ComputerName  pcname | Where-Object {$_.responding -eq $false} | Format-Table -AutoSize ProcessName, Responding, CPU | Sort-Object ProcessName -Descending");
    
                    //Returns the Eventlog in application log with errors
                    txtEventLogError.Text = RunScript("Get-WinEvent -FilterHashtable @{logname= 'Windows PowerShell'} -MaxEvents 100 -ComputerName pcname | Where-Object LevelDisplayName -Like 'Information' | Sort-Object TIMECREATED -Descending");
    
                }
                catch (Exception error)
                {
                    txtTaskList.Text += String.Format("\r\nError in script : {0}\r\n", error.Message);
                    txtTaskNotResponding.Text += String.Format("\r\nError in script : {0}\r\n", error.Message);
                    txtEventLogError.Text += String.Format("\r\nError in script : {0}\r\n", error.Message);
                    
                }
            }

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,171

    Re: Run Powershell commands inside Winform C# with another users crfedential

    Where is RunScript defined? Is it part of a power shell library or something you've coded?

  3. #3
    Join Date
    Sep 2016
    Posts
    2

    Re: Run Powershell commands inside Winform C# with another users crfedential

    Quote Originally Posted by Arjay View Post
    Where is RunScript defined? Is it part of a power shell library or something you've coded?
    Its defined here:

    Code:
     private string RunScript(string scriptText)
            {
                // create Powershell runspace
                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);
    
                // 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 = pipeline.Invoke();
    
                // 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 stringBuilder.ToString();
             
            }

    Regards,

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,171

    Re: Run Powershell commands inside Winform C# with another users crfedential

    I spent a bit of time trying looking for the answer and was unable to find a solution that is able to pass the identity of the current user as credentials to powershell.

  5. #5
    Join Date
    Apr 2002
    Location
    Egypt
    Posts
    2,210

    Re: Run Powershell commands inside Winform C# with another users crfedential

    Hello
    I tried this on my local laptop, it works:
    Code:
    var con = new WSManConnectionInfo();
    con.Credential = new PSCredential("user", "pass".ToSecureString());
    Runspace runspace = RunspaceFactory.CreateRunspace(con);
    runspace.Open();
    Pipeline pipeline = runspace.CreatePipeline();
    pipeline.Commands.Add("dir");
    pipeline.Commands.Add("Out-String");
    var results = pipeline.Invoke();
    runspace.Close();
    to connect to a remote machine you need to specify the machine to the WSManConnectionInfo
    also make sure you enable PowerShell remoting : http://www.howtogeek.com/117192/how-...ote-computers/





    Hesham A. Amin
    My blog , Articles


    <a rel=https://twitter.com/HeshamAmin" border="0" /> @HeshamAmin

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,171

    Re: Run Powershell commands inside Winform C# with another users crfedential

    Quote Originally Posted by hspc View Post
    Hello
    I tried this on my local laptop, it works:
    Code:
    var con = new WSManConnectionInfo();
    con.Credential = new PSCredential("user", "pass".ToSecureString());
    Runspace runspace = RunspaceFactory.CreateRunspace(con);
    runspace.Open();
    Pipeline pipeline = runspace.CreatePipeline();
    pipeline.Commands.Add("dir");
    pipeline.Commands.Add("Out-String");
    var results = pipeline.Invoke();
    runspace.Close();
    to connect to a remote machine you need to specify the machine to the WSManConnectionInfo
    also make sure you enable PowerShell remoting : http://www.howtogeek.com/117192/how-...ote-computers/





    The OP wants to use the identity of the current user without having to explicitly pass in a user name and password.

  7. #7
    Join Date
    Oct 2016
    Posts
    1

    Re: Run Powershell commands inside Winform C# with another users crfedential

    Good

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)