I have a C# WPF application running on Windows 10 from which I call Powershell commands. Typically, the code runs successfully and everything's fine, but now it does not.
The Powershell commands run without any error, but do not do anything. I have not changed any of the code pertaining to these commands.
The method I call is below:
using (var ps = PowerShell.Create()) //Mounts iso image
{
ps.AddScript("Mount-DiskImage");
ps.AddParameter("ImagePath", IMG);
string outp = "";
Collection<PSObject> PSOutput = ps.Invoke();
using (StringWriter sw = new StringWriter())
{
foreach (PSObject invoke in PSOutput)
sw.WriteLine(invoke.ToString());
outp = sw.ToString();
}
if (ps.Streams.Error.Count > 0)
{
outp += Environment.NewLine + string.Format("{0} errors: ", ps.Streams.Error.Count);
foreach (ErrorRecord err in ps.Streams.Error)
outp += Environment.NewLine + err.ToString();
MessageBox.Show(outp);
}
}
Edit: Changed code to return error if one is raised. No error was shown when testing.
Edit 2: Added additional error check. This returns an error, but I can't tell what it is.
Edit 3: Changed code to see error output and it said I failed to provide the ImagePathparameter (which I did not). I checked the IMG parameter before I passed it to the method, and it is indeed a full path. What's wrong here?
IMGparameter is valid.if (ps.Streams.Error.Count > 0)?