This is a powershell script that help you to update the Network Adapter for the virtual machine in ESXi.
You can save this moveNet.ps1 and you will need to login to the ESXi server or your Vcenter before you can run this script.
The script basically help you to update the NetworkAdapter attribute of the Virtual machine using the Set-NetworkAdapter function.
In order to ensure the virtual machine get a new ip address after the network is changed, we put in some check to determine if the machine is powered on. If that's online, we will do ipconfig /release before updating the networkadapter and do an ipconfig /renew after that.
To use this script, in the PowerCli windows, after you connect to your ESXi or VCenter, you should run
./moveNet.ps1 -vmname VM123 -newnetwork VLAN1 -administrator Administrator -password password
Please refer to the detail and modify that to suit your environment!!
----start of moveNet.ps1-----
Param($vmname, $newnetwork, $administrator, $password)
$VM = Get-VM -Name $vmname
if ($vm.powerstate -eq "PoweredOff") {
Get-VM -Name $vmname | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName $newnetwork -confirm:$false
}
if ($vm.powerstate -eq "PoweredOn") {
Invoke-VMScript "ipconfig /release" -vm $VM -GuestUser $administrator -GuestPassword $password -ScriptType "bat"
Get-VM -Name $vmname | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName $newnetwork -confirm:$false
Invoke-VMScript "ipconfig /renew" -vm $VM -GuestUser $administrator -GuestPassword $password -ScriptType "bat"}
-----end of script-----
This blog is to record down all the automation script I created to automate some manual task I have to do from day-to-day.
Friday, June 28, 2013
Script to force install all windows update or patches
I would like to post the script I used to update the base image for my VDI project for Windows XP, Vista or Windows.
Prerequisite:
1. You need to have Windows XP SP3 or above and installed at least the Windows update agent 7.4.7600.226
http://support.microsoft.com/kb/946928
2. You need to create the following files
Update.bat simply ensure the Windows update service is enabled before running wua.vbs.
Here's the detail of the script. Please make sure you put these two scripts in the same directory before you run it.
This is actually a vbscript from Microsoft which use the Windows update agent to get the update.
Set updateSession = CreateObject("Microsoft.Update.Session")
updateSession.ClientApplicationID = "MSDN Sample Script"
Set updateSearcher = updateSession.CreateUpdateSearcher()
WScript.Echo "Searching for updates..." & vbCRLF
Set searchResult = _
updateSearcher.Search("IsInstalled=0 and Type='Software' and IsHidden=0")
WScript.Echo "List of applicable items on the machine:"
For I = 0 To searchResult.Updates.Count-1
Set update = searchResult.Updates.Item(I)
WScript.Echo I + 1 & "> " & update.Title
Next
If searchResult.Updates.Count = 0 Then
WScript.Echo "There are no applicable updates."
WScript.Quit
End If
WScript.Echo vbCRLF & "Creating collection of updates to download:"
Set updatesToDownload = CreateObject("Microsoft.Update.UpdateColl")
For I = 0 to searchResult.Updates.Count-1
Set update = searchResult.Updates.Item(I)
addThisUpdate = false
If update.InstallationBehavior.CanRequestUserInput = true Then
WScript.Echo I + 1 & "> skipping: " & update.Title & _
" because it requires user input"
Else
If update.EulaAccepted = false Then
WScript.Echo I + 1 & "> note: " & update.Title & _
" has a license agreement that must be accepted:"
WScript.Echo update.EulaText
WScript.Echo "Do you accept this license agreement? (Y/N)"
update.AcceptEula()
addThisUpdate = true
Else
addThisUpdate = true
End If
End If
If addThisUpdate = true Then
WScript.Echo I + 1 & "> adding: " & update.Title
updatesToDownload.Add(update)
End If
Next
If updatesToDownload.Count = 0 Then
WScript.Echo "All applicable updates were skipped."
WScript.Quit
End If
WScript.Echo vbCRLF & "Downloading updates..."
Set downloader = updateSession.CreateUpdateDownloader()
downloader.Updates = updatesToDownload
downloader.Download()
Set updatesToInstall = CreateObject("Microsoft.Update.UpdateColl")
rebootMayBeRequired = false
WScript.Echo vbCRLF & "Successfully downloaded updates:"
For I = 0 To searchResult.Updates.Count-1
set update = searchResult.Updates.Item(I)
If update.IsDownloaded = true Then
WScript.Echo I + 1 & "> " & update.Title
updatesToInstall.Add(update)
If update.InstallationBehavior.RebootBehavior > 0 Then
rebootMayBeRequired = true
End If
End If
Next
If updatesToInstall.Count = 0 Then
WScript.Echo "No updates were successfully downloaded."
WScript.Quit
End If
If rebootMayBeRequired = true Then
WScript.Echo vbCRLF & "These updates may require a reboot."
End If
WScript.Echo vbCRLF & "Would you like to install updates now? (Y/N)"
strInput = "y"
WScript.Echo
If (strInput = "Y" or strInput = "y") Then
WScript.Echo "Installing updates..."
Set installer = updateSession.CreateUpdateInstaller()
installer.Updates = updatesToInstall
Set installationResult = installer.Install()
'Output results of install
WScript.Echo "Installation Result: " & _
installationResult.ResultCode
WScript.Echo "Reboot Required: " & _
installationResult.RebootRequired & vbCRLF
WScript.Echo "Listing of updates installed " & _
"and individual installation results:"
For I = 0 to updatesToInstall.Count - 1
WScript.Echo I + 1 & "> " & _
updatesToInstall.Item(i).Title & _
": " & installationResult.GetUpdateResult(i).ResultCode
Next
End If
sc start wuauserv
cscript wua.vbs
Prerequisite:
1. You need to have Windows XP SP3 or above and installed at least the Windows update agent 7.4.7600.226
http://support.microsoft.com/kb/946928
2. You need to create the following files
- wua.vbs
- update.bat
Update.bat simply ensure the Windows update service is enabled before running wua.vbs.
Here's the detail of the script. Please make sure you put these two scripts in the same directory before you run it.
a. wua.vbs
This is actually a vbscript from Microsoft which use the Windows update agent to get the update.
Set updateSession = CreateObject("Microsoft.Update.Session")
updateSession.ClientApplicationID = "MSDN Sample Script"
Set updateSearcher = updateSession.CreateUpdateSearcher()
WScript.Echo "Searching for updates..." & vbCRLF
Set searchResult = _
updateSearcher.Search("IsInstalled=0 and Type='Software' and IsHidden=0")
WScript.Echo "List of applicable items on the machine:"
For I = 0 To searchResult.Updates.Count-1
Set update = searchResult.Updates.Item(I)
WScript.Echo I + 1 & "> " & update.Title
Next
If searchResult.Updates.Count = 0 Then
WScript.Echo "There are no applicable updates."
WScript.Quit
End If
WScript.Echo vbCRLF & "Creating collection of updates to download:"
Set updatesToDownload = CreateObject("Microsoft.Update.UpdateColl")
For I = 0 to searchResult.Updates.Count-1
Set update = searchResult.Updates.Item(I)
addThisUpdate = false
If update.InstallationBehavior.CanRequestUserInput = true Then
WScript.Echo I + 1 & "> skipping: " & update.Title & _
" because it requires user input"
Else
If update.EulaAccepted = false Then
WScript.Echo I + 1 & "> note: " & update.Title & _
" has a license agreement that must be accepted:"
WScript.Echo update.EulaText
WScript.Echo "Do you accept this license agreement? (Y/N)"
update.AcceptEula()
addThisUpdate = true
Else
addThisUpdate = true
End If
End If
If addThisUpdate = true Then
WScript.Echo I + 1 & "> adding: " & update.Title
updatesToDownload.Add(update)
End If
Next
If updatesToDownload.Count = 0 Then
WScript.Echo "All applicable updates were skipped."
WScript.Quit
End If
WScript.Echo vbCRLF & "Downloading updates..."
Set downloader = updateSession.CreateUpdateDownloader()
downloader.Updates = updatesToDownload
downloader.Download()
Set updatesToInstall = CreateObject("Microsoft.Update.UpdateColl")
rebootMayBeRequired = false
WScript.Echo vbCRLF & "Successfully downloaded updates:"
For I = 0 To searchResult.Updates.Count-1
set update = searchResult.Updates.Item(I)
If update.IsDownloaded = true Then
WScript.Echo I + 1 & "> " & update.Title
updatesToInstall.Add(update)
If update.InstallationBehavior.RebootBehavior > 0 Then
rebootMayBeRequired = true
End If
End If
Next
If updatesToInstall.Count = 0 Then
WScript.Echo "No updates were successfully downloaded."
WScript.Quit
End If
If rebootMayBeRequired = true Then
WScript.Echo vbCRLF & "These updates may require a reboot."
End If
WScript.Echo vbCRLF & "Would you like to install updates now? (Y/N)"
strInput = "y"
WScript.Echo
If (strInput = "Y" or strInput = "y") Then
WScript.Echo "Installing updates..."
Set installer = updateSession.CreateUpdateInstaller()
installer.Updates = updatesToInstall
Set installationResult = installer.Install()
'Output results of install
WScript.Echo "Installation Result: " & _
installationResult.ResultCode
WScript.Echo "Reboot Required: " & _
installationResult.RebootRequired & vbCRLF
WScript.Echo "Listing of updates installed " & _
"and individual installation results:"
For I = 0 to updatesToInstall.Count - 1
WScript.Echo I + 1 & "> " & _
updatesToInstall.Item(i).Title & _
": " & installationResult.GetUpdateResult(i).ResultCode
Next
End If
b. update.bat
sc config wuauserv start= demandsc start wuauserv
cscript wua.vbs
Subscribe to:
Posts (Atom)