This is not new for most here. I went through almost all posts from here and blogs. It doesn't error out.
I am not getting any errors. If I run PowerShell locally it works [expected to take screen shot], when running from C# it is not taking Screenshots.
Installed all the required NuGet packages.
using System.Management.Automation;
I am trying to run .ps1 from c# but not working. Powershell takes a screenshot and stores it in a provided path.
Locally I am running .ps1 as below and it takes a screenshot. But from c# it doesn't.
C:\source\Hooks\TestScreenCapture.ps1 -ScreenShotPath C:\source\Hooks\testy.png
This function takes a screenshot when runs from selenium.
I have written a c# code to run it.
Script located at:
var psScript = Path.Combine(AppDomain.CurrentDomain.BaseDirectory.Replace("bin\\Debug\\netcoreapp3.1", "Hooks"), "TestScreenCapture.ps1");
1st Approach:
var startInfo = new ProcessStartInfo()
{
FileName = "powershell.exe",
Arguments = $"-NoProfile -ExecutionPolicy unrestricted -file \"{psScript}\" -ScreenShotPath \"{screenShotFullPath}\"",
UseShellExecute = false
};
Process.Start(startInfo);
2nd Approach
var powerShell = PowerShell.Create();
powerShell
.AddScript($"\"{psScript}\"", false)
.AddParameter("ScreenShotFullPath", screenShotFullPath)
.Invoke();
3rd Approach
var powerShell = PowerShell.Create();
powerShell
.AddScript(@"& {psScript} -ScreenShotFullPath {screenShotFullPath}")
.Invoke();
I tried running PowerShell code from my C# just to check if c# is working or not and it works.
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");
Issues seem PowerShell to take a screenshot from c# is not working.