How to execute powershell from c# as administrator on Windows 10

0

I'm trying to execute a powershell script from c# on Windows 10 and getting an Access Denied error. Here is my code so far:

            var powerShellCommand = $"Invoke-Command -ScriptBlock {{ pdqdeploy Deploy -Package \"{packageName}\" -Targets \"{machineName}\" }}";
            var passwordSecureStringAdmin = new SecureString();
            var passwordAdmin = "daadminpw";
            for (int i = 0; i < passwordAdmin.Length; i++)
            {
                passwordSecureStringAdmin.AppendChar(passwordAdmin[i]);
            }

            var credential = new PSCredential("monkeytown.com\\monkey.king", passwordSecureStringAdmin);
            var connectionInfo = new WSManConnectionInfo({ Credential = credential};

            using (var runSpace = RunspaceFactory.CreateRunspace(connectionInfo))
            {
                runSpace.Open();
                using (var ps = PowerShell.Create())
                {
                    ps.Runspace = runSpace;
                    ps.AddScript("Enable-PSRemoting -SkipNetworkProfileCheck -Force");
                    ps.AddScript("Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted");
                    ps.AddScript(powerShellCommand);
                    var PSOutput = ps.Invoke();

                    if (ps.HadErrors)
                    {
                        throw ps.Streams.Error.ElementAt(0).Exception;
                    }
                }
                runSpace.Close();
            }

For some background, I had some previous errors that I worked through to get here:

ERROR MESSAGE: Connecting to remote server localhost failed with the following error message : The client cannot connect to the destination specified in the request. Verify that the service on the destination is running and is accepting requests. Consult the logs and documentation for the WS-Management service running on the destination, most commonly IIS or WinRM. If the destination is the WinRM service, run the following command on the destination to analyze and configure the WinRM service: "winrm quickconfig". For more information, see the about_Remote_Troubleshooting Help topic.

So I ran winrm quickconfig manually in PS and got another error:

WinRM is not set up to receive requests on this machine. The following changes must be made:

Start the WinRM service.
Set the WinRM service type to delayed auto start.

Make these changes [y/n]? y

WinRM has been updated to receive requests.

WinRM service type changed successfully.
WinRM service started.
WSManFault
Message = The client cannot connect to the destination specified in the request. Verify that the service on the destination is running and is accepting requests. Consult the logs and documentation for the WS-Management service running on the destination, most commonly IIS or WinRM. If the destination is the WinRM service, run the following command on the destination to analyze and configure the WinRM service: "winrm quickconfig".

Error number: -2144108526 0x80338012
The client cannot connect to the destination specified in the request. Verify that the service on the destination is running and is accepting requests. Consult the logs and documentation for the WS-Management service running on the destination, most commonly IIS or WinRM. If the destination is the WinRM service, run the following command on the destination to analyze and configure the WinRM service: "winrm quickconfig".

So then I ran:

Enable-PSRemoting -SkipNetworkProfileCheck -Force

on my machine and that got me to where I am now with the Access Denied problem.

c#
powershell
windows-10
asked on Stack Overflow Jul 16, 2019 by richard • edited Jul 17, 2019 by richard

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0