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
Monday, August 27, 2012
Automated script to check the services in Automatic and Manual Mode
This week I am working on some new requirement from our Active Directory Architect to provide all the services required to start as "Automatic" and "Manual" for all my VMWare VDI infrastructure servers.
It is very painful to go to each server one by one to do this, so I written a simple Powershell script to check.
You will need to prepare for a text file (Servers.txt) which list all your servers you want to check. (One hostname in each line). This Servers.txt file should be in the same location as your service.ps1 script.
---start of service.ps1 script----------------------
$compArray = get-content .\Servers.txt
foreach($strComputer in $compArray)
{
echo $strComputer
echo --------------------------------------------------------------------------------------------------
Get-WmiObject Win32_Service -ComputerName $strComputer | Where {$_.StartMode -like "*Auto*"}| select-object DisplayName,Name,StartMode,StartName
echo --------------------------------------------------------------------------------------------------
Get-WmiObject Win32_Service -ComputerName $strComputer | Where {$_.StartMode -like "*Manual*"}| select-object DisplayName,Name,StartMode,StartName
echo --------------------------------------------------------------------------------------------------
}
---end of service.ps1 script----------------------
To run that script, you just need to login with an account with Local Administrator right for all servers needed and run the following command in the Powershell
You will then see all services in results.txt.
It is very painful to go to each server one by one to do this, so I written a simple Powershell script to check.
You will need to prepare for a text file (Servers.txt) which list all your servers you want to check. (One hostname in each line). This Servers.txt file should be in the same location as your service.ps1 script.
---start of service.ps1 script----------------------
$compArray = get-content .\Servers.txt
foreach($strComputer in $compArray)
{
echo $strComputer
echo --------------------------------------------------------------------------------------------------
Get-WmiObject Win32_Service -ComputerName $strComputer | Where {$_.StartMode -like "*Auto*"}| select-object DisplayName,Name,StartMode,StartName
echo --------------------------------------------------------------------------------------------------
Get-WmiObject Win32_Service -ComputerName $strComputer | Where {$_.StartMode -like "*Manual*"}| select-object DisplayName,Name,StartMode,StartName
echo --------------------------------------------------------------------------------------------------
}
---end of service.ps1 script----------------------
To run that script, you just need to login with an account with Local Administrator right for all servers needed and run the following command in the Powershell
.\services.ps1 | out-file results.txt
Wednesday, February 23, 2011
How to monitor free space of a group of desktop
If you have some special group of desktop shared by a team, you may have the similar issue as me that the hard disk space used up quickly simply because of the temp file (like the ost file of outlook).
I have written a script to monitor the freespace of a few machines...
First of all, I create a freespace.bat file like this
----start of freespace.bat-----
@echo off
for /f "usebackq delims== tokens=2" %%x in (`wmic /node:%1 logicaldisk where "DeviceID='C:'" get FreeSpace /format:value`) do (set tx1234=%%x)
for /f "usebackq delims== tokens=2" %%x in (`wmic /node:%1 logicaldisk where "DeviceID='C:'" get Size /format:value`) do (set txsi=%%x)
echo %1;%tx1234%;%txsi%
----end of freespace.bat-----
This script basically will poll the PC that you specified provide the freespace and the disksize of c-drive.
The usage is like this
freespace.bat <hostname>
If you have a list of machines, then, you can simply put their hostname into a text file, e.g. deskfree.txt and then use the following batch file to check their freespace and echo the results into a text file.
----start of mon.bat-------
@echo off
del /q deskfree.txt
for /f %%y in (desk.txt) Do freespace %%y >> deskfree.txt
------end of mon.bat--------
I have written a script to monitor the freespace of a few machines...
First of all, I create a freespace.bat file like this
----start of freespace.bat-----
@echo off
for /f "usebackq delims== tokens=2" %%x in (`wmic /node:%1 logicaldisk where "DeviceID='C:'" get FreeSpace /format:value`) do (set tx1234=%%x)
for /f "usebackq delims== tokens=2" %%x in (`wmic /node:%1 logicaldisk where "DeviceID='C:'" get Size /format:value`) do (set txsi=%%x)
echo %1;%tx1234%;%txsi%
----end of freespace.bat-----
This script basically will poll the PC that you specified provide the freespace and the disksize of c-drive.
The usage is like this
freespace.bat <hostname>
If you have a list of machines, then, you can simply put their hostname into a text file, e.g. deskfree.txt and then use the following batch file to check their freespace and echo the results into a text file.
----start of mon.bat-------
@echo off
del /q deskfree.txt
for /f %%y in (desk.txt) Do freespace %%y >> deskfree.txt
------end of mon.bat--------
Wednesday, February 9, 2011
How to install apps from Ovi-store into your Satio Phone without hacking phone
If you are the poor Satio users like me who do not have access to Ovi-store and you want to use the latest firmware (or you have to) in order to have the full WXGA video, here's the steps you could follow to install Nokia apps while continue to get the latest update from Sony Ericsson.
1) Download and extract SISContents
2) Install Firefox in your PC and install the Add-on "User Agent Switcher"
3) Setup a new user Agent which allows you to pre-tend to be a Nokia phone. You can use the string for Nokia 5800 as below
Mozilla/5.0 (SymbianOS/9.4; U; Series60/5.0 Nokia5800d-1/10.0.008; Profile/MIDP-2.1 Configuration/CLDC-1.1 ) AppleWebKit/413 (KHTML, like Gecko) Safari/413
4) Next, goto the Ovi-store to download all the apps you want.
5)Then Open the siscontents file extsis.exe the file you downloaded in first step..
6) then select the sis file which you have downloaded from ovi-store
7) Now You Delete All The Signatures Present in the sis File
8) Now Select Sign Package And Follow The Following Steps
9) Now a new window will pop out to sign the sis file. You need to have a certificate and key file first for your phone. if you have not done it please read my previous post of Get You s60v5 certificate and key form OPDA. If you have the certificate and key files please continue.
10) Now After this close this window and save your new hacked sis file.
Oh.....in some case, you cannot straightly do this as the file you download from OVI-store are .dm file!!!
Some of the Ovi Store apps downloaded to PC are in DM file format. So, how to install the DM files from Ovi Store on Nokia phone?
You need to open and edit the DM file with a file editor that can deal with binary file format (i.e. hex content not ASCII). My favorite is Notepad++ (powerful open-source editor for Windows), as shown in this “silent” screencast:
- Open the DM file with Notepad++
- Move the cursor from top-left position to the beginning of “z” character and hit DELETE key – that will delete top 4 lines up to the blank space in front of “z” character, as shown in the YouTube video (above).
- Save and close the edited file.
- Remove the
.dmfile extension from file name – if the 2nd last suffix is SIS, then the converted file is in SIS file format, otherwise it is SISX file format. - Then, you can use the same method above to sign the application with your own certificate!!!
Enjoy...
Tuesday, February 1, 2011
How to check what kind of hotfix are installed in a PC
To check what kind hotfix has been installed, we could use a tool Pstools available from Microsoft.
http://technet.microsoft.com/en-us/sysinternals/bb897550
The command line is very straightforward, you simply do that by
psinfo -h
And then you will see something like this
P:\Desktop\pstools>psinfo -h
PsInfo v1.75 - Local and remote system information viewer
Copyright (C) 2001-2007 Mark Russinovich
Sysinternals - www.sysinternals.com
System information for \\x12354
Uptime: 0 days 21 hours 16 minutes 53 seconds
Kernel version: Microsoft Windows XP, Multiprocessor Free
Product type: Professional
Product version: 5.1
Service pack: 3
Kernel build number: 2600
Registered organization: Fei
Registered owner: Fei User
Install date: 8/6/2010, 8:05:42 PM
Activation status: Error reading status
IE version: 7.0000
System root: C:\WINDOWS
Processors: 2
Processor speed: 2.7 GHz
Processor type: Intel(R) Core(TM)2 Duo CPU E7400 @
Physical memory: 3292 MB
Video driver: Intel(R) 4 Series Internal Chipset
Installed HotFix
10/6/2010 Microsoft Internationalized Domain Names Mitigation APIs
10/6/2010 Microsoft National Language Support Downlevel APIs
11/11/2010 Security Update for Windows Media Player (KB2378111)
8/7/2010 Security Update for Windows Media Player (KB954155)
8/7/2010 Security Update for Windows Media Player (KB968816)
8/7/2010 Security Update for Windows Media Player (KB973540)
1/10/2010 Security Update for Windows Media Player (KB975558)
26/7/2010 Security Update for Windows Media Player (KB978695)
10/6/2010 Security Update for Windows Media Player 10 (KB917734)
8/7/2010 Security Update for Windows XP (KB941569)
8/7/2010 Security Update for Windows Internet Explorer 7 (KB974455)
8/6/2010 Windows XP Service Pack 3
4/9/2010 Security Update for Windows XP (KB2079403)
4/9/2010 Security Update for Windows XP (KB2115168)
1/10/2010 Security Update for Windows XP (KB2121546)
4/9/2010 Security Update for Windows XP (KB2160329)
29/7/2010 Security Update for Windows XP (KB2229593)
1/10/2010 Security Update for Windows XP (KB2259922)
11/11/2010 Security Update for Windows XP (KB2279986)
12/8/2010 Security Update for Windows XP (KB2286198)
......
If you are going to feed this information to an automation script so that you can consolidate all inforamtion by yourself, you can use the script like this.
del checkhotfix.txt /q
del 0123894985.txt /q
psinfo -h > 0123894985.txt
for /f "skip=18 tokens=1*" %%i in (0123894985.txt) do @echo %%i %%j >> checkhotfix.txt
This script will skip the first 18 lines of the information so you can easily import them into an access or SQL database for further analysis.
http://technet.microsoft.com/en-us/sysinternals/bb897550
The command line is very straightforward, you simply do that by
psinfo -h
And then you will see something like this
P:\Desktop\pstools>psinfo -h
PsInfo v1.75 - Local and remote system information viewer
Copyright (C) 2001-2007 Mark Russinovich
Sysinternals - www.sysinternals.com
System information for \\x12354
Uptime: 0 days 21 hours 16 minutes 53 seconds
Kernel version: Microsoft Windows XP, Multiprocessor Free
Product type: Professional
Product version: 5.1
Service pack: 3
Kernel build number: 2600
Registered organization: Fei
Registered owner: Fei User
Install date: 8/6/2010, 8:05:42 PM
Activation status: Error reading status
IE version: 7.0000
System root: C:\WINDOWS
Processors: 2
Processor speed: 2.7 GHz
Processor type: Intel(R) Core(TM)2 Duo CPU E7400 @
Physical memory: 3292 MB
Video driver: Intel(R) 4 Series Internal Chipset
Installed HotFix
10/6/2010 Microsoft Internationalized Domain Names Mitigation APIs
10/6/2010 Microsoft National Language Support Downlevel APIs
11/11/2010 Security Update for Windows Media Player (KB2378111)
8/7/2010 Security Update for Windows Media Player (KB954155)
8/7/2010 Security Update for Windows Media Player (KB968816)
8/7/2010 Security Update for Windows Media Player (KB973540)
1/10/2010 Security Update for Windows Media Player (KB975558)
26/7/2010 Security Update for Windows Media Player (KB978695)
10/6/2010 Security Update for Windows Media Player 10 (KB917734)
8/7/2010 Security Update for Windows XP (KB941569)
8/7/2010 Security Update for Windows Internet Explorer 7 (KB974455)
8/6/2010 Windows XP Service Pack 3
4/9/2010 Security Update for Windows XP (KB2079403)
4/9/2010 Security Update for Windows XP (KB2115168)
1/10/2010 Security Update for Windows XP (KB2121546)
4/9/2010 Security Update for Windows XP (KB2160329)
29/7/2010 Security Update for Windows XP (KB2229593)
1/10/2010 Security Update for Windows XP (KB2259922)
11/11/2010 Security Update for Windows XP (KB2279986)
12/8/2010 Security Update for Windows XP (KB2286198)
......
If you are going to feed this information to an automation script so that you can consolidate all inforamtion by yourself, you can use the script like this.
del checkhotfix.txt /q
del 0123894985.txt /q
psinfo -h > 0123894985.txt
for /f "skip=18 tokens=1*" %%i in (0123894985.txt) do @echo %%i %%j >> checkhotfix.txt
This script will skip the first 18 lines of the information so you can easily import them into an access or SQL database for further analysis.
How to extract all email distribution list from Active Directory
This is a script that I used to extract all distribution list from Active Directory. You can use a tool called csvde.exe from Microsoft to do this job easily.
csvde -f c:\temp\yourdl.csv -p subtree -l cn,mail,displayName,managedBy -r "(|(&(objectCategory=Group)(objectClass=Group)(|(groupType=8)(groupType=4)(groupType=2)))(objectCategory=ms-Exch-Dynamic-Distribution-List)(objectClass=msExchDynamicDistributionList))" -j c:\temp -s <your domain>
You should replace <your domain> with your company's domain information
csvde -f c:\temp\yourdl.csv -p subtree -l cn,mail,displayName,managedBy -r "(|(&(objectCategory=Group)(objectClass=Group)(|(groupType=8)(groupType=4)(groupType=2)))(objectCategory=ms-Exch-Dynamic-Distribution-List)(objectClass=msExchDynamicDistributionList))" -j c:\temp -s <your domain>
You should replace <your domain> with your company's domain information
Subscribe to:
Posts (Atom)








