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?