VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Output for Password Validation

Hi,

I am having issue with the output from the below script. Password validation for VM is working but multiple failed output is getting captured along with the correct password validations in the output.

how can I capture the output and exit the loop, if the password is passed, and also capture the output if all the password fails.

Please help.

$Username = 'root'

$pswds = 'pswd1', 'pswd2', 'pswd3'

ForEach-Object -Process {

   foreach ($pswd in $pswds)

   {

$pass = ConvertTo-SecureString -AsPlainText  $pswd -Force

$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass

Import-Csv -Path '.\CentOS_Info.csv' -UseCulture -PipelineVariable row |

ForEach-Object -Process {

   try

   {

   $session = New-SSHSession $row.IP_Address -Credential $Cred -AcceptKey -ErrorAction Stop

   $result = $session | Select-Object -ExpandProperty Connected

   $output = $((Invoke-SSHCommand -SSHSession $session -Command 'date').output)

   Get-SSHSession | Remove-SSHSession | Out-Null

   }

   catch

   {

   $result = 'False'

   }

   $row | Add-Member -MemberType NoteProperty -Name 'Password_Validation' -Value $result -PassThru | Add-Member -MemberType NoteProperty -Name 'Date' -Value $output -PassThru

}

}

} | Export-Excel -Path $reportlocation

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You do something like this.

The report will now contain rows like

IP_Address         pswd1 pswd2 pswd3

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

srv1.domain.com    False True  False

192.168.1.1        False False True

And the failed servers, for which no password worked, will be displayed.

And which you can also save to a CSV file if required.

$Username = 'root'

$pswds = 'pswd1', 'pswd2', 'pswd3'

$report = @()

$failedServers = @()


Import-Csv -Path '.\CentOS_Info.csv' -UseCulture -PipelineVariable row |

ForEach-Object -Process {

    $passwdFound = $false

    foreach ($pswd in $pswds) {

        $pass = ConvertTo-SecureString -AsPlainText  $pswd -Force

        $Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username, $pass

        try {

            $session = New-SSHSession $row.IP_Address -Credential $Cred -AcceptKey -ErrorAction Stop

            $result = $session | Select-Object -ExpandProperty Connected

            $output = $((Invoke-SSHCommand -SSHSession $session -Command 'date').output)

            Get-SSHSession | Remove-SSHSession | Out-Null

            $passwdFound = $true

        }

        catch {

            $result = 'False'

        }

        $row | Add-Member -MemberType NoteProperty -Name $pswd -Value $result

    }

    $report += $row

    if (-not $passwdFound) {

        $failedServers += $row.IP_Address

    }

}

$report | Export-Excel -Path $reportlocation


Write-Host "Failed servers"

$failedServers


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

View solution in original post

Reply
0 Kudos
12 Replies
LucD
Leadership
Leadership
Jump to solution

You can capture the data you want in the CSV in an array and then export only that array.

Something like this

$Username = 'root'

$pswds = 'pswd1', 'pswd2', 'pswd3'

$report = @()


foreach ($pswd in $pswds) {

    $pass = ConvertTo-SecureString -AsPlainText  $pswd -Force

    $Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username, $pass

    Import-Csv -Path '.\CentOS_Info.csv' -UseCulture -PipelineVariable row |

    ForEach-Object -Process {

        try {

            $session = New-SSHSession $row.IP_Address -Credential $Cred -AcceptKey -ErrorAction Stop

            $result = $session | Select-Object -ExpandProperty Connected

            $output = $((Invoke-SSHCommand -SSHSession $session -Command 'date').output)

            Get-SSHSession | Remove-SSHSession | Out-Null

        }

        catch {

            $result = 'False'

        }

        $report += ($row | Add-Member -MemberType NoteProperty -Name 'Password_Validation' -Value $result -PassThru | Add-Member -MemberType NoteProperty -Name 'Date' -Value $output -PassThru)

    }

}

$report | Export-Excel -Path $reportlocation


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

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

LucD,

The issue is, when we have big list of servers to be validated,

1. how can i identify, which password worked for particular server

2. for failed servers, how can I get the unique server list, since the output would be repeated for multiple failed passwords ?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You do something like this.

The report will now contain rows like

IP_Address         pswd1 pswd2 pswd3

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

srv1.domain.com    False True  False

192.168.1.1        False False True

And the failed servers, for which no password worked, will be displayed.

And which you can also save to a CSV file if required.

$Username = 'root'

$pswds = 'pswd1', 'pswd2', 'pswd3'

$report = @()

$failedServers = @()


Import-Csv -Path '.\CentOS_Info.csv' -UseCulture -PipelineVariable row |

ForEach-Object -Process {

    $passwdFound = $false

    foreach ($pswd in $pswds) {

        $pass = ConvertTo-SecureString -AsPlainText  $pswd -Force

        $Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username, $pass

        try {

            $session = New-SSHSession $row.IP_Address -Credential $Cred -AcceptKey -ErrorAction Stop

            $result = $session | Select-Object -ExpandProperty Connected

            $output = $((Invoke-SSHCommand -SSHSession $session -Command 'date').output)

            Get-SSHSession | Remove-SSHSession | Out-Null

            $passwdFound = $true

        }

        catch {

            $result = 'False'

        }

        $row | Add-Member -MemberType NoteProperty -Name $pswd -Value $result

    }

    $report += $row

    if (-not $passwdFound) {

        $failedServers += $row.IP_Address

    }

}

$report | Export-Excel -Path $reportlocation


Write-Host "Failed servers"

$failedServers


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

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

LucD,

I am getting the below error

ConvertFrom-Csv : Cannot validate argument on parameter 'InputObject'. The argument is null. Provide a valid value for the argument, and then try running the command again.

At D:\4.ps1:37 char:30

+ ConvertFrom-Csv -InputObject $servers -UseCulture -PipelineVariable r ...

+                              ~~~~~~~~

    + CategoryInfo          : InvalidData: (:) [ConvertFrom-Csv], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.ConvertFromCsvCommand

Failed servers

My Input csv has below - .\CentOS_Info.csv

"Folder","VM","IP_Address","OS"

"Test","Test02","192.168.1.2","CentOS 7 (64-bit)"

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

My bad, I left a line that I used for testing in there.
I updated the script above.


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

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Perfect...Thank you very much Smiley Happy

Reply
0 Kudos
vmk2014
Expert
Expert
Jump to solution

LucD,

Does the script will work for  bulk servers in different domain. It will be around 1000 to 5000 Servers ?

Thanks

V

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Not sure what you mean by different domains?

Are these different SSO domains?

Then you need to connect to all the vCenters.

Since this is interacting with the Guest OS, there is no direct connection with the SSO domain.

Provided you are connected to the correct vCenters.


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

Reply
0 Kudos
vmk2014
Expert
Expert
Jump to solution

Yes. LucD. We have multiple domain like Domain A/B/C and before patching or performing any maintenance activity, we need to valid the our credential manually which takes lot of time. Will it work for bulk servers ?

Thanks

V

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Are you talking about Windows AD domains?

This script was for a Linux box where we used SSH to test the password.

This will not work like this for Windows servers.


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

vmk2014
Expert
Expert
Jump to solution

Yes, LucD. It's my bad. I was talking about Windows domain

Thanks

V

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Then this script will not work unless you have SSH active and running on all these stations.

But a simple Invoke-VMscript with a dummy command should tell if the password is correct or not.

Would that work?

This is an example of what I mean, see Re: Help with Try/Catch block to Invoke querys on VMs with Multiple Passwords


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

Reply
0 Kudos