简体   繁体   中英

Invoke-command on remote computer with reboot

I'm building a script which does the following things

  1. Connect from the Hyper-V to the newly created VM template using Powershell remote
  2. Change IP
  3. Change computername
  4. Reboot for computer name to be effective
  5. Install AD role
  6. Create domain

Most steps seem to work, I only get stuck on step 4.

I use the following line.

Invoke-Command -FilePath C:\Support\createdomain.ps1 -VMName TEMPLATE-DC

Which asks for the credentials of the VM, and it goes on and do the stuff. Until I get to the reboot.

Restart-Computer -Wait -For PowerShell -Timeout 300 -Delay 2

I also tried to just have the following, this restarts the VM but also stops the Powershell session and does not run the Start Sleep

Restart-Computer -Force
Start-Sleep -s 300

So my question is: how can I run a powershell script on a remote machine, in this case a VM. And do a reboot and wait until it is rebooted to resume the script?

This is the error I get

The background process reported an error with the following message: "The Hyper-V socket target process has ended.".
+ CategoryInfo          : OperationStopped: (TEMPLATE-DC:String) [], PSRemotingTransportException
+ FullyQualifiedErrorId : JobFailure
+ PSComputerName        : TEMPLATE-DC

I would recommend breaking that script up into three pieces.

  1. Update System name and IP address
    • wait until the system is accessible via loop. Make sure vm is accessible before proceeding
  2. Reconnect after the Agent is accessible and Reboot.
    • invoke-command to reboot the system.
    • wait for the system is accessilbe again. Once the VM is accessible,
  3. Install Role and configure domain.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM