Can some suggest me to complete this script.
1. There is a user with name 'admin' on all the windows machines (6k +)
2. In some of the VM's the admin user is configured with a particular password (ex: password@123)
3. With help of the script I need to just test whether connection established with passed credentials or not.
4. If connection established then I need VM Name.
something like this
$vms=Get-VM | Where-Object {$_.PowerState -eq "PoweredOn" -and $_.ExtensionData.Config.GuestFullName -like "*Microsoft*" }
forEach($vm in $vms){
try{
$out = Invoke-VMScript -VM $vm -GuestUser "admin" -GuestPassword "password@123"
}
catch [VMware.VimAutomation.ViCore.Types.V1.ErrorHandling.InvalidGuestLogin]{
$out = "Invalid logon"
}
catch{
$out = "Login Sucessfully"
}
Write-Output "VM: $($vm.Name): $out"
}
Try like this
$vms=Get-VM | Where-Object {$_.PowerState -eq "PoweredOn" -and $_.ExtensionData.Config.GuestFullName -like "*Microsoft*" }
forEach($vm in $vms){
try{
Invoke-VMScript -VM $vm -GuestUser "admin" -GuestPassword "password@123" -ErrorAction Stop -ScriptText "" > $null
$out = "Login Sucessfully"
}
catch [VMware.VimAutomation.ViCore.Types.V1.ErrorHandling.InvalidGuestLogin]{
$out = "Invalid guest logon"
}
catch{
$out = "Other error"
}
Write-Output "VM: $($vm.Name): $out"
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try like this
$vms=Get-VM | Where-Object {$_.PowerState -eq "PoweredOn" -and $_.ExtensionData.Config.GuestFullName -like "*Microsoft*" }
forEach($vm in $vms){
try{
Invoke-VMScript -VM $vm -GuestUser "admin" -GuestPassword "password@123" -ErrorAction Stop -ScriptText "" > $null
$out = "Login Sucessfully"
}
catch [VMware.VimAutomation.ViCore.Types.V1.ErrorHandling.InvalidGuestLogin]{
$out = "Invalid guest logon"
}
catch{
$out = "Other error"
}
Write-Output "VM: $($vm.Name): $out"
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Perfect LucD:smileyhappy: It worked.
One last step need to export the output in .csv
with $vmname, $output & IP address of $vm
Try like this
$vms=Get-VM | Where-Object {$_.PowerState -eq "PoweredOn" -and $_.ExtensionData.Config.GuestFullName -like "*Microsoft*" }
$report = forEach($vm in $vms){
try{
Invoke-VMScript -VM $vm -GuestUser "admin" -GuestPassword "password@123" -ErrorAction Stop -ScriptText "" > $null
$out = "Login Sucessfully"
}
catch [VMware.VimAutomation.ViCore.Types.V1.ErrorHandling.InvalidGuestLogin]{
$out = "Invalid guest logon"
}
catch{
$out = "Other error"
}
$vm | Select Name,
@{N='Output';E={$out}},
@{N='IP';E={(Get-VMGuest -VM $_ | Select -ExpandProperty IPAddress) -join '|'}}
}
$report | Export-Csv report.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference