0

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.

2
  • pass your creds in another way Commented Oct 13, 2022 at 21:53
  • 1
    add -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))) Commented Oct 13, 2022 at 22:12

1 Answer 1

2

Instead of adding a text script to the pipeline, pass it properly as a Command with parameters. You should also add -Confirm $false.

You also need to find a way to get credentials without using Get-Credential. You could just prompt the user and create a PSCredential from that.

var creds = new PSCredential("GLMC\Admin", AskForSecurePassword());

pipeline.Commands
  .AddCommand("Restart-Computer")
  .AddParameter("ComputerName", pcname)
  .AddParameter("Credential", creds)
  .AddParameter("Force")
  .AddParameter("Confirm", false);

Collection<PSObject> results = pipeline.Invoke();

I suggest you find a better way of passing credentials. Ideally you would require the whole application to be launched with admin rights, then you wouldn't need to pass the credentials separately, just rely on what it has already.

Sign up to request clarification or add additional context in comments.

Comments

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.