1

I`m trying to convert this code which works in Powershell ISE

PWord = ConvertTo-SecureString "P@ssw0rd???" -AsPlainText -Force

$User = "testing\administrator"
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord

Restart-Computer -ComputerName 10.0.50.235 -Credential $Credential -Force

Below is what I have in a console app written in .net core 3.1 but gives a WinRm error.

PowerShell ps = PowerShell.Create();
ps.AddCommand("Restart-Computer");

SecureString secureString = new NetworkCredential("administrator", "P@ssw0rd???", "testing").SecurePassword;
PSCredential psc = new PSCredential($"testing\\administrator", secureString);
ps.AddParameter("Credential", psc);
ps.AddParameter("ComputerName", ip);
ps.AddParameter("Force");
var results = ps.Invoke();

When I run this the error is thrown

Failed to restart the computer 10.0.50.235 with the following error message: The WinRM client cannot process the request. Default authentication may be used with an IP address under the following conditions: the transport is HTTPS or the destination is in the TrustedHosts list, and explicit credentials are provided. Use winrm.cmd to configure TrustedHosts. Note that computers in the TrustedHosts list might not be authenticated. For more information on how to set TrustedHosts run the following command: winrm help config.

I'm confused about what I'm doing wrong as it works fine in Powershell ISE but not in .net.

I've also tried the following but i still get the same error

SecureString secureString = new SecureString();
foreach (char c in rebootOptions.Password)
{
    secureString.AppendChar(c);
}

Anyone know what I'm doing wrong?

2
  • you're connecting to powershell as a remote host, thus it's not allowing you... check this on how to run as administrator ... however, I'm not sure this works on .net core Commented Jan 19, 2020 at 4:01
  • Are you sure you are doing the same? ISE uses Powershell for Windows, .net core usually uses Powershell Core. The trusted host config you have in ps for Windows might be different. Commented Jan 19, 2020 at 9:39

0

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.