1

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?

6
  • Your code looks correct. Make sure your IMG parameter is valid. Commented Nov 7, 2017 at 14:27
  • @Icemanid Pretty sure that parameter hasn't changed either. It should be a valid global path to an image. Commented Nov 7, 2017 at 14:29
  • blogs.msdn.microsoft.com/kebab/2014/04/28/… Scroll down to "Synchronous execution". There is how to grab error output (if any). Commented Nov 7, 2017 at 14:30
  • After edit: Did you also check Error-Stream if (ps.Streams.Error.Count > 0) ? Commented Nov 7, 2017 at 14:40
  • have you tried running as admin? Commented Nov 7, 2017 at 15:34

1 Answer 1

1

Well, fixed my own problem (for now at least). For one thing, when I was originally trying different things out to fix it, I changed the line ps.AddCommand() to ps.AddScript() and forgot to change it back when it didn't work. ps.AddCommand() is obviously the correct method to call for this scenario. This (in and of itself) did not fix my problem. HERE I found the solution for a similar problem, so I tried it on mine and it worked.

To summarize, navigate to HKLM\System\CurrentControlSet\Services\FsDepends in the registry. Click on the DWORD value Start and change it from 3 to 0. Restart to apply. Apparently it has something to do with messed up timings in that particular Windows service. I'm not sure if this is a permanent solution, but all my code seems to be in order now, so we'll see.

Sign up to request clarification or add additional context in comments.

1 Comment

@Fildor Completed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.