This is the final script.I need few more corrections if possible.
1. I used try/catch method to try multiple passwords for login but here I can not get correct write output after succeeding the login attempt with correct password but tools installation is going as expected.
2.Can we avoid invoke cmd twice for disabling UAC and installing vmtools and sending them in one cmd.
Still any modifications required to improve faster execution process in the script please correct me.
Write-Host 'Fetching Resource Pools Info'
$allresourcepools = 'SEZ08'
$respExpression = "^$($allresourcepools -join '$|^')$"
foreach($resourcepool in (Get-View -ViewType ResourcePool -Filter @{'Name'=$respExpression})){
Write-Host "Fetching VMs in $resourcepool"
$vmsinresourcepool = Get-View -Id $resourcepool.Vm
$vmtoolsoutdate = $vmsinresourcepool|? {$_.guest.ToolsRunningStatus -eq "guestToolsRunning" -and $_.guest.toolsversionstatus -eq "guestToolsNeedUpgrade"}
$vmexpression= "^$($vmtoolsoutdate.name -join '$|^')$"
$installerArgs = '/S /v "/qn REBOOT=R ADDLOCAL=ALL REMOVE=Hgfs,WYSE"'
$vmtoolsupdate=Get-View -ViewType VirtualMachine -Filter @{'Name'=$vmexpression}
if($vmtoolsupdate.Guest.GuestFullName -contains "*Windows*"){
%{($vmtoolsupdate).UpgradeTools_Task($installerArgs)}
}
else{
%{($vmtoolsupdate).UpgradeTools_Task($null)}
}
Start-sleep -s 300
$tasklist=Get-Task |? {$_.Name -eq "UpgradeTools_Task" -and $_.State -eq "Error"}
$installfailedvms=Get-View -Id $tasklist.ObjectId -Property Name | select -ExpandProperty Name
$installfailedwin=Get-VM $installfailedvms |?{$_.ExtensionData.Guest.GuestFullName -like "*windows*"}
foreach($batchinstallvms in $installfailedwin){
if($batchinstallvms.ExtensionData.Runtime.ToolsInstallerMounted -eq $false){
Mount-Tools -VM $batchinstallvms
}
else{
}
$code = "wmic logicaldisk where drivetype=5 get deviceid, volumename, description"
$disableUAC= "C:\Windows\System32\cmd.exe /k %windir%\System32\reg.exe ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v EnableLUA /t REG_DWORD /d 0 /f"
$found = $false
try{
$result1 = Invoke-VMScript -VM $batchinstallvms -GuestUser "admin" -GuestPassword "admin@1" -ScriptType Bat -ScriptText $code | Select -ExpandProperty ScriptOutPut
$result1 | where{$_ -match '(?<Drive>\w:)\s*VMware Tools'} | %{
$drive = $Matches['Drive']
}
$path="$($drive)\setup.exe /S /v ""/qn REBOOT=R ADDLOCAL=ALL REMOVE=Hgfs"""
Invoke-VMScript -VM $batchinstallvms -GuestUser "admin" -GuestPassword "admin@1" -ScriptType Bat -ScriptText $disableUAC | Select -ExpandProperty ScriptOutPut
$resultout1=Invoke-VMScript -VM $batchinstallvms -GuestUser "admin" -GuestPassword "admin@1" -ScriptType Bat -ScriptText $path | Select -ExpandProperty ScriptOutPut
$found = $true
}
catch [VMware.VimAutomation.ViCore.Types.V1.ErrorHandling.InvalidGuestLogin]{
$result1 = "Invalid logon"
} catch{
$result1 = "any other output"
}
if(!$found){
try{
$result2 = Invoke-VMScript -VM $batchinstallvms -GuestUser "admin" -GuestPassword "admin@2" -ScriptType Bat -ScriptText $code | Select -ExpandProperty ScriptOutPut
$result2 | where{$_ -match '(?<Drive>\w:)\s*VMware Tools'} | %{
$drive = $Matches['Drive']
}
$path="$($drive)\setup.exe /S /v ""/qn REBOOT=R ADDLOCAL=ALL REMOVE=Hgfs"""
Invoke-VMScript -VM $batchinstallvms -GuestUser "admin" -GuestPassword "admin@1" -ScriptType Bat -ScriptText $disableUAC | Select -ExpandProperty ScriptOutPut
$resultout2=Invoke-VMScript -VM $batchinstallvms -GuestUser "admin" -GuestPassword "admin@2" -ScriptType Bat -ScriptText $path | Select -ExpandProperty ScriptOutPut
$found = $true
}
catch [VMware.VimAutomation.ViCore.Types.V1.ErrorHandling.InvalidGuestLogin]{
$result2 = "Invalid logon"
} catch{
$result2 = "any other output"
}
if(!$found){
try{
$result3 = Invoke-VMScript -VM $batchinstallvms -GuestUser "admin" -GuestPassword "admin@2" -ScriptType Bat -ScriptText $code | Select -ExpandProperty ScriptOutPut
$result3 | where{$_ -match '(?<Drive>\w:)\s*VMware Tools'} | %{
$drive = $Matches['Drive']
}
$path="$($drive)\setup.exe /S /v ""/qn REBOOT=R ADDLOCAL=ALL REMOVE=Hgfs"""
Invoke-VMScript -VM $batchinstallvms -GuestUser "admin" -GuestPassword "admin@1" -ScriptType Bat -ScriptText $disableUAC | Select -ExpandProperty ScriptOutPut
$resultout3=Invoke-VMScript -VM $batchinstallvms -GuestUser "admin" -GuestPassword "admin@2" -ScriptType Bat -ScriptText $path | Select -ExpandProperty ScriptOutPut
$found = $true
}
catch [VMware.VimAutomation.ViCore.Types.V1.ErrorHandling.InvalidGuestLogin]{
$result3 = "Invalid logon"
} catch{
$result3 = "any other output"
}
Write-Output "VM: $($batchinstallvms.Name): $found"
Write-Output "VM: $($batchinstallvms.Name): $result1"
Write-Output "VM: $($batchinstallvms.Name): $result2"
Write-Output "VM: $($batchinstallvms.Name): $result3"
}
}
}
}