Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to execute PowerShell script or cmdlets from C# code?
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Management.Automation;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace PowerShellScriptExecutor
{
class Program
{
static void Main(string[] args)
{
using (PowerShell PowerShellInst = PowerShell.Create())
{
string criteria = "system*";
PowerShellInst.AddScript("Get-Service -DisplayName " + criteria);
Collection<PSObject> PSOutput = PowerShellInst.Invoke();
foreach (PSObject obj in PSOutput)
{
if (obj != null)
{
Console.Write(obj.Properties["Status"].Value.ToString() + " - ");
Console.WriteLine(obj.Properties["DisplayName"].Value.ToString());
}
}
Console.WriteLine("Done");
Console.Read();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Management.Automation;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace PowerShellScriptExecutor
{
class Program
{
static void Main(string[] args)
{
//Execute PS1(PowerShell script) file
using (PowerShell PowerShellInst = PowerShell.Create())
{
string path = System.IO.Path.GetDirectoryName(@"C:\Temp\") + "\\Get-EventLog.ps1";
if (!string.IsNullOrEmpty(path))
PowerShellInst.AddScript(System.IO.File.ReadAllText(path));
Collection<PSObject> PSOutput = PowerShellInst.Invoke();
foreach (PSObject obj in PSOutput)
{
if (obj != null)
{
Console.Write(obj.Properties["EntryType"].Value.ToString() + " - ");
Console.Write(obj.Properties["Source"].Value.ToString() + " - ");
Console.WriteLine(obj.Properties["Message"].Value.ToString() + " - ");
}
}
Console.WriteLine("Done");
Console.Read();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Management.Automation;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace PowerShellScriptExecutor
{
class Program
{
static void Main(string[] args)
{
//execute powershell cmdlets or scripts using command arguments as process
ProcessStartInfo processInfo = new ProcessStartInfo();
processInfo.FileName = @"powershell.exe";
//execute powershell script using script file
//processInfo.Arguments = @"& {c:\temp\Get-EventLog.ps1}";
//execute powershell command
processInfo.Arguments = @"& {Get-EventLog -LogName Application -Newest 10 -EntryType Information | Select EntryType, Message}";
processInfo.RedirectStandardError = true;
processInfo.RedirectStandardOutput = true;
processInfo.UseShellExecute = false;
processInfo.CreateNoWindow = true;
//start powershell process using process start info
Process process = new Process();
process.StartInfo = processInfo;
process.Start();
Console.WriteLine("Output - {0}", process.StandardOutput.ReadToEnd());
Console.WriteLine("Errors - {0}", process.StandardError.ReadToEnd());
Console.Read();
}
}
}
@miteshsureja

This comment has been minimized.

Copy link
Owner Author

@miteshsureja miteshsureja commented Jun 2, 2018

image

@miteshsureja

This comment has been minimized.

Copy link
Owner Author

@miteshsureja miteshsureja commented Jun 2, 2018

image
image

@miteshsureja

This comment has been minimized.

Copy link
Owner Author

@miteshsureja miteshsureja commented Jun 2, 2018

image

@jack-debug

This comment has been minimized.

Copy link

@jack-debug jack-debug commented Jul 16, 2020

thank you, you beautiful indian man

@casrxs

This comment has been minimized.

Copy link

@casrxs casrxs commented Nov 12, 2020

Hi, I used every method described above, it works fine on my local machine but when I create task scheduler on production server PowerShell script does not get executed.

Any help would be appreciated.

Thanks
Rupinder

@pramodRayanna

This comment has been minimized.

Copy link

@pramodRayanna pramodRayanna commented Dec 2, 2020

Hi, i am having the same issue. i am invoking powershell script on a button click. its working fine on my local machine. but when the code is deployed to Dev environment, its not working. is this an issue with IIS User vs local user? please help.

Thanks in advance,
Pramod.

@shunyh

This comment has been minimized.

Copy link

@shunyh shunyh commented Feb 8, 2021

Same issue here, anyone can help with this? It works fine on dev machine, but it will not execute the PS after deploying to the Windows server.

@shunyh

This comment has been minimized.

Copy link

@shunyh shunyh commented Feb 19, 2021

Same issue here, anyone can help with this? It works fine on dev machine, but it will not execute the PS after deploying to the Windows server.

I resolved this issue.
Set those properties:

RedirectStandardError = true; error = process.StandardError.ReadToEnd();

write the error to a .txt file, it is the important info for troubleshooting.

The error for me is logon server failed due to missing the domain before the username. Hope it helps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment