Saturday, October 31, 2015

Automation Script to Restart Windows Virtual Machines (VMs) with PowerShell

Note: we must have current user in administrator

Follow below procedure to add user to admin group in VM
Computer --> Manage --> Local Users and Groups --> (User($username variable user in below ps script) must be added here, if not add)
VMs.txt – notepad contains all VMs (computer names) to be restarted in each line
Ps file contains below code

''*----------------------------------------- Power shell script as below ----------------------------------
$username = 'admin user of computer'
$password = 'admin user password'
$secstr = New-Object -TypeName System.Security.SecureString
$password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr
restart-computer (get-content "Note pad which contain(.txt) suggested to maintain in Shared location") -credential $cred -force

Above file will be executed by below vbs file

''*---------------------------------------- VB script File as below ------------------------------------------
Dim var
var = msgbox("Are you sure you want to restart all EXECUTION VMs",4,"Restart Vms by Powershell")
If var = 6 Then
   Dim objShell
   Set objShell = WScript.CreateObject("WScript.Shell")
   objShell.Run "powershell -file ""power shell script path (.ps1) suggested to maintain in shared location"""
   Set objshell = nothing
   Msgbox "Selected NO to Restart all execution VMs", 64, "Input By User"
End If

"A good threat is worth a thousand tests" - Boris Beizer

No comments:

There was an error in this gadget