VMware Cloud Community
ErikKemper
Contributor
Contributor

check list of users and password against hosts

Hi,

can anyone help me with a script that checks a list of users/passwords against a set of hosts in a vcenter platform?

There are four vcenters with a bunch of hosts of which there is a mix of accounts and passwords which is not documented.

It would be nice to have a script that can test this from a file which has the known account and passwords.

And that it would output this as  "host, account, password" which work and a sepater output which shows the host that fail on all?

Anyone have such a script of would like to help me with it?

Thanks!

Erik

8 Replies
LucD
Leadership
Leadership

You could do something like the following.

Note that the script assumes the following:

  • You are connected to all vCenters
  • The account info is present in a CSV file with the following layout

"Name","Password"

"user1","pswd1"

"user2","pswd2"

$report = @()

$users = Import-Csv users.csv -UseCulture


foreach($esx in Get-VMHost){

    foreach($user in $users){

        Try{

            $connection = Connect-VIServer -Server $esx.Name -User $user.Name -Password $user.Password

            $report += New-Object PSObject -Property @{

                Host = $esx.Name

                Account = $user.Name

                Password = $user.Password

            }

            Disconnect-VIServer -Server $Name -Confirm:$false

        }

        Catch{

            Write-Output "Host: $($esxName) failed for user $($user)"

        }

    }

}

$report


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

ErikKemper
Contributor
Contributor

Thanks LucD,

I will test it today and let you know!

Cheers

Erik

0 Kudos
ErikKemper
Contributor
Contributor

Hi,

works like a charme, but one thing is missing... :smileylaugh:

I cannot (easily) see in the megalarge output which password worked for a host?

How can I add in the script an extra output file file which shows the successful connect (host, account, password?

Cheers

Erik

0 Kudos
LucD
Leadership
Leadership

The report should show 3 columns: Host,Account and Password.

Don't you see those columns ?

You can export the content of the $report variable to a CSV file if needed.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

ErikKemper
Contributor
Contributor

Hi,

Yes, i see the columns, but in ACCOUNT, HOST, PASSWORD format, and in my case six times, which is the amount of test accounts in the users.csv.

So it shows all the hosts with all the test account/passwords.... Smiley Wink

Still fiddleling with it, but if you have an idea.....?

Cheers

Erik

0 Kudos
LucD
Leadership
Leadership

I think I might have found the flaw in the logic.

Try with this version

$report = @()

$users = Import-Csv users.csv -UseCulture

foreach($esx in Get-VMHost){

    foreach($user in $users){

        Try{

            $connection = Connect-VIServer -Server $esx.Name -User $user.Name -Password $user.Password -ErrorAction Stop

            if($connection){

                $report += New-Object PSObject -Property @{

                    Host = $esx.Name

                    Account = $user.Name

                    Password = $user.Password

                }

                Disconnect-VIServer -Server $Name -Confirm:$false

            }

        }

        Catch{

            Write-Output "Host: $($esxName) failed for user $($user)"

        }

    }

}

$report


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

ErikKemper
Contributor
Contributor

Ok,

will keep you posted....!

Thanks

Erik

0 Kudos
ErikKemper
Contributor
Contributor

Hi LucD,

works like a charm!

I edited it a bit so it dumps 2 outputs to seperate files for the eye.

Thanks again!

Cheers
Erik

Below my script (with credits to LucD)

---------------------------------------------------------------------------------------

$hosts = @(

#     "test-site01.local.com"

    "test-site01.local.com",

    "test-site02.local.com",

    "test-site03.local.com"

);

$vcenteruser = "DC01\vcenteradmin"

$vcenterpassword = "Pass0wrd"

$report = @()

$users = Import-Csv users.csv -UseCulture

# Connect

Connect-VIServer -Server $hosts -User $vcenteruser -Password $vcenterpassword

foreach($esx in Get-VMHost){

    foreach($user in $users){

        Try{

            $connection = Connect-VIServer -Server $esx.Name -User $user.Name -Password $user.Password -ErrorAction Stop

            if($connection){

                $report += New-Object PSObject -Property @{

                    Host = $esx.Name

                    Account = $user.Name

                    Password = $user.Password

                }

                Disconnect-VIServer -Server $Name -Confirm:$false

            }

        }

        Catch{

            Write-Output "Host: $($esx.Name) failed for user $($user)" | Out-File ".\HostReport-failed-passwords.txt" -Width 120 -Append

        }

    }

}

$report | Out-File ".\HostReport-working_passwords.txt" -Width 120 -Append

---------------------

0 Kudos