-
September 8th, 2016, 06:29 PM
#1
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);
}
}
-
September 8th, 2016, 10:02 PM
#2
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?
-
September 8th, 2016, 11:20 PM
#3
Re: Run Powershell commands inside Winform C# with another users crfedential
Originally Posted by Arjay
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,
-
September 9th, 2016, 02:10 PM
#4
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.
-
September 11th, 2016, 08:38 AM
#5
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/
-
September 11th, 2016, 11:26 AM
#6
Re: Run Powershell commands inside Winform C# with another users crfedential
Originally Posted by hspc
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.
-
October 11th, 2016, 02:28 AM
#7
Re: Run Powershell commands inside Winform C# with another users crfedential
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|