0

I have a windows service (C#) running as Local System. I want to be able to read my database and run PowerShell commands and scripts. I am able to run most scripts but my test machine is hanging on this one :

NET USE Z: /Delete /y
NET USE Z: \\TEST2\ProgramData

I can run these commands on the computer and it all works but when I try to run these commands from within my Windows Service it hands on the line which runs the script.

    private static bool RunPSCommand(string command, out string output)
    {
        // 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(command);

        // 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
        try
        {
            StringBuilder stringBuilder = new StringBuilder();

            Collection<PSObject> results = pipeline.Invoke();
            if (pipeline.HadErrors)
            {
                var errors = pipeline.Error.ReadToEnd();
                foreach (object error in errors)
                {
                    stringBuilder.AppendLine(error.ToString());
                }
            }

            // close the runspace
            runspace.Close();

            // convert the script result into a single string        
            foreach (PSObject obj in results)
            {
                stringBuilder.AppendLine(obj.ToString());
            }

            output = stringBuilder.ToString();
            return true;
        }
        catch (CommandNotFoundException e)
        {
            output = e.Message;
            return false;
        }

        catch (Exception e)
        {
            output = e.Message;
            return false;
        }
    }

I am not sure why this is so difficult. I have been wracking my head on this one for three days and trying every option from net use to DOM objects

NET USE Z: /Delete /y
(New-Object -Com WScript.Network).MapNetworkDrive("z:" , "\\test2\programdata")
  • There's a lot of discussion about what can and can't be done, and some creative approaches to the issue of mapped drives in a service here: https://stackoverflow.com/questions/182750/map-a-network-drive-to-be-used-by-a-service – WaitingForGuacamole Feb 24 '21 at 20:11
  • I dont want to make a mapped drive FOR the service to use, I want the service to MAKE the drive mapping for another process to use. – Chris Dunlop Feb 24 '21 at 20:23
  • I think the problem is essentially the same - whether it uses the drive itself or creates for others. LOCALSYSTEM isn't a network credential, it's local only, so when you try to do that, there is nothing that `NET USE` can, well, use! And I'm speculating a little here, but the hang could be the result of `NET USE` trying to prompt the user with no existing interactive session. Have you tried `NET USE Z: \\TEST2\ProgramData /username:myuser mypassword` ? – WaitingForGuacamole Feb 24 '21 at 20:35
  • I tried net use z: \\test2\programdata /u:dev1\administrator password in powershell and it did not work. I tried net use z: \\test2\programdata /u:test2\test password in powershell and it did not work. These two computers are part of a workgroup, not a network so I tried specifying the computer\user account to see if that would work. I then got rid of the username and passwords from my tests in powershell and it worked, but when I went back to trying to do it through the service it did not work. – Chris Dunlop Feb 24 '21 at 21:50
  • Well, because you're in a workgroup and not a domain I'm less in my element, and your experience is taking me down stranger paths... Two things to try. First, and easiest, create a service account that isn't `LOCALSYSTEM`. Grant it the privileges it needs, including to the remote share. Log in as that user, and verify that you can do a `NET USE`. If that all works, configure the service to use that service account. Second, and you will not like this - go through the hassle of configuring it as an interactive service so you can possibly see why it hangs. Could be on input. – WaitingForGuacamole Feb 24 '21 at 22:01
  • Now I am completely baffled. I had the service log in as the administrator to test something. it worked. Then I switched back to localsystem. Now, all of a sudden, I am getting mapped network drives but the are saying DISCONNECTED and have the red X through the drive ICON. But they work????? – Chris Dunlop Feb 24 '21 at 22:12
  • Have you tried using forward slashes instead of back splashes when declaring the network location with net use? Backslashes can be a real pain because they are escape characters. – general-gouda Feb 25 '21 at 02:31
  • Drives show as disconnected because you connected to a network share with a network credential. LocalSystem is not a network credential and so cannot access what you mapped with another account. – WaitingForGuacamole Feb 25 '21 at 03:48

1 Answers1

0

I was attempting to do this from a service but switched to an application running on a user session. It works this way.