VMware Cloud Community
vin01
Expert
Expert
Jump to solution

Test login credentials are correct using invoke-VM

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"

    }

Regards Vineeth.K
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

View solution in original post

0 Kudos
3 Replies
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
vin01
Expert
Expert
Jump to solution

Perfect LucD:smileyhappy: It worked.

One last step need to export the output in .csv

with $vmname, $output & IP address of $vm

Regards Vineeth.K
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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