I'm trying to create a powershell script inside c# that will allow my company to paste a computer name in the field, click restart, and then force a restart on a remote computer.
Here's what I have:
private void button1_Click(object sender, EventArgs e) {
string pcName = textBox1.Text;
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
runspace.SessionStateProxy.SetVariable("Computer", pcName);
var script = string.Format("Restart-Computer -ComputerName " + pcName + " -Credential Get-Credential GLMC\\Admin -Force");
pipeline.Commands.AddScript(script);
Collection<PSObject> results = pipeline.Invoke();
runspace.Close();
StringBuilder sB = new StringBuilder();
foreach (PSObject pSObject in results)
sB.AppendLine(pSObject.ToString());
textBox2.Text = sB.ToString();
}
Taken from this powershell script (that works):
$Computer = Read-Host 'Enter computer name' -Verbose
$Creds = Get-Credential GLMC\Admin
Write-Host -ForegroundColor Yellow "Starting process..."
Restart-Computer -ComputerName $Computer -Credential $Creds -Force
$End= Read-Host 'Finished, press enter to continue' -Verbose
I keep getting an error in the credentials part that says A command that prompts the user failed because the host program or the command type does not support user interaction.
-Confirm:$false; take-Credential (Get-Credential GLMC\\Admin)into bracers (this will not help); use-Credential ([System.Management.Automation.PSCredential] ("username", ($password | ConvertTo-SecureString -AsPlainText -Force)))