0

I am running the below code but the restart is not working. My intention was to run restart command parallelly on all remote machines at once.

$YourFile = gc "machinelst.txt"
$username = "user1"
$password = "pass1"
$secpw = ConvertTo-SecureString $password -AsPlainText -Force
$cred  = New-Object Management.Automation.PSCredential ($username, $secpw)

foreach ($computer in $YourFile)
{
    Invoke-Command -ComputerName $computer -credential $cred -ErrorAction Stop -ScriptBlock { Restart-Computer -ComputerName $computer -Force } -AsJob
     
} 

enter image description here

7
  • remove -ErrorAction Stop for read error Commented Dec 31, 2020 at 13:16
  • Thanks for your reply, i have remove the -ErrorAction Stop and run the script again.It is not restarting Commented Dec 31, 2020 at 13:21
  • what is your errors text Commented Dec 31, 2020 at 13:22
  • Id Name PSJobTypeName State HasMoreData Location Command -- ---- ------------- ----- ----------- -------- ------- 76 Job76 RemoteJob Failed False RemoteHost1 Restart-Computer -Com... 78 Job78 RemoteJob Failed False RemoteHost2 Restart-Computer -Com... 80 Job80 RemoteJob Failed False RemoteHost3 Restart-Computer -Com... This is the output Commented Dec 31, 2020 at 13:24
  • remove -AsJob and give me your error Commented Dec 31, 2020 at 13:25

3 Answers 3

1

That looks like its the output from Get-Job - could you try Receive-Job $id (Receive-Job 80).

Should give you the actual exception.

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

Comments

1

This likely runs in parallel just like invoke-command does with an array of computernames:

restart-computer computer01,computer02,computer03,computer04,computer05

Or this. It takes a couple minutes for the winrm service to come back, but they all seem to reboot at the same time.

$c = get-credential
$list = 1..10 | % tostring computer00
restart-computer $list -wait -protocol wsman -cr $c

Comments

0

try this (you will can add -asjob if it's work) :

$username = "yourdomain\user1"
$password = ConvertTo-SecureString "pass1" -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password

get-content "machinelst.txt" | %{
Restart-Computer -ComputerName $_ -Authentication default -Credential $cred

}

if you want use job, you can do it :

$listjob=@()

get-content "machinelst.txt" | %{
$listjob+=Restart-Computer -ComputerName $_ -Authentication default -Credential $cred -AsJob
}

$listjob | Wait-Job -Timeout 30

$listjob | %{

    if ($_.State -eq 'Failed' )
    {
      Receive-Job -Job $_ -Keep
    }
   
}

4 Comments

ConvertTo-SecureString : Input string was not in a correct format. At restart.ps1:14 char:26 $password = "pass1" | ConvertTo-SecureString
try with this change
ok it seems like working now. One thing i want to know, this script will execute the restart parallely on all remote servers at once? or will it run sequential?.
You can use the "-asjob" command to initiate parallel restarts, but restarting either takes time depending on the remote server. There may be gaps of a few seconds I think.

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.