VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Need help in validating Passwords

Hi,

I am unable to validate and execute the below scripts, When I have multiple passwords

Please help

$reportlocation1 = ".\Clients_Mgmt.csv"

$WPassword = "Password1", "Password2", "Password3"

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

$Creds = New-Object System.Management.Automation.PSCredential ("admin", $pass)

$code = @'

"### Output ###"

(Get-Date)

'@

$report = @()

Import-Csv -Path $reportlocation1 -UseCulture -PipelineVariable row |

ForEach-Object -Process {

   foreach ($pswd in $pass)

   {

    $sInvoke = @{

        VM              = $_.Name

        GuestCredential = $Creds

        ScriptTYpe      = 'powershell'

        ScriptText      = $code

    }

    $result = Invoke-VMScript @sInvoke

    $dummy, $out1 = $result.ScriptOutput -split '### Output ###'

    $out1 = $out1.TrimStart("`n`r")

    $report += $row | Add-Member -MemberType NoteProperty -Name 'DateTime' -Value ([DateTime]::Parse($out1)) -PassThru

    }

    }

$report | ft -auto

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Something like this?

$reportlocation1 = ".\Clients_Mgmt.csv"

$WPassword = "Password1", "Password2", "Password3"

$user = 'admin'


$code = @'

"### Output ###"

(Get-Date)

'@


$report = @()

$notFound = @()


Import-Csv -Path $reportlocation1 -UseCulture -PipelineVariable row |

ForEach-Object -Process {

    $found = $false

    $pswdNr = 1

    foreach ($pswd in $WPassword) {

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

        $Creds = New-Object System.Management.Automation.PSCredential ($user, $pass)


        $sInvoke = @{

            VM              = $row.Name

            GuestCredential = $Creds

            ScriptTYpe      = 'powershell'

            ScriptText      = $code

            ErrorAction = 'Stop'

        }

        try {

            $result = Invoke-VMScript @sInvoke

            $dummy, $out1 = $result.ScriptOutput -split '### Output ###'

            $out1 = $out1.TrimStart("`n`r")

            $report += $row | Add-Member -MemberType NoteProperty -Name 'DateTime' -Value ([DateTime]::Parse($out1)) -PassThru |

                Add-Member -MemberType NoteProperty -Name 'Password' -Value $pswdNr -PassThru

            $found = $true

            break

        }

        catch {

        }

        $pswdNr++

    }

    if(-not $found){

        $notFound += $row

    }

}


$report | Format-Table -AutoSize


$notFound | Format-Table -AutoSize


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

View solution in original post

Reply
0 Kudos
8 Replies
LucD
Leadership
Leadership
Jump to solution

I'm not sure what you are trying to do.
Finding a working password?

Or getting that DateTime property, independent which password was used?

And what when none of the passwords work?


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

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

LucD,

I want to get the time from the Guest VM using the working Password.

If non of the passwords work, I want to capture the VM Name in the different file.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try like this

$reportlocation1 = ".\Clients_Mgmt.csv"

$WPassword = "Password1", "Password2", "Password3"

$user = 'admin'


$code = @'

"### Output ###"

(Get-Date)

'@


$report = @()

$notFound = @()


Import-Csv -Path $reportlocation1 -UseCulture -PipelineVariable row |

ForEach-Object -Process {

    $found = $false

    foreach ($pswd in $WPassword) {

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

        $Creds = New-Object System.Management.Automation.PSCredential ($user, $pass)


        $sInvoke = @{

            VM              = $row.Name

            GuestCredential = $Creds

            ScriptTYpe      = 'powershell'

            ScriptText      = $code

            ErrorAction = 'Stop'

        }

        try {

            $result = Invoke-VMScript @sInvoke

            $dummy, $out1 = $result.ScriptOutput -split '### Output ###'

            $out1 = $out1.TrimStart("`n`r")

            $report += $row | Add-Member -MemberType NoteProperty -Name 'DateTime' -Value [DateTime]::Parse($out1) -PassThru

            $found = $true

            break

        }

        catch {

        }

    }

    if(-not $found){

        $notFound += $row

    }

}


$report | Format-Table -AutoSize


$notFound | Format-Table -AutoSize


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

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

LucD,

That worked. But I one last thing, How can I add Password Column added to the output to get which Password Worked ?

Like Password1 or Password2

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Something like this?

$reportlocation1 = ".\Clients_Mgmt.csv"

$WPassword = "Password1", "Password2", "Password3"

$user = 'admin'


$code = @'

"### Output ###"

(Get-Date)

'@


$report = @()

$notFound = @()


Import-Csv -Path $reportlocation1 -UseCulture -PipelineVariable row |

ForEach-Object -Process {

    $found = $false

    $pswdNr = 1

    foreach ($pswd in $WPassword) {

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

        $Creds = New-Object System.Management.Automation.PSCredential ($user, $pass)


        $sInvoke = @{

            VM              = $row.Name

            GuestCredential = $Creds

            ScriptTYpe      = 'powershell'

            ScriptText      = $code

            ErrorAction = 'Stop'

        }

        try {

            $result = Invoke-VMScript @sInvoke

            $dummy, $out1 = $result.ScriptOutput -split '### Output ###'

            $out1 = $out1.TrimStart("`n`r")

            $report += $row | Add-Member -MemberType NoteProperty -Name 'DateTime' -Value ([DateTime]::Parse($out1)) -PassThru |

                Add-Member -MemberType NoteProperty -Name 'Password' -Value $pswdNr -PassThru

            $found = $true

            break

        }

        catch {

        }

        $pswdNr++

    }

    if(-not $found){

        $notFound += $row

    }

}


$report | Format-Table -AutoSize


$notFound | Format-Table -AutoSize


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

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

LucD,

I am unable to get the Time output and Password details in the output.

I just see, what ever I have in the input file as output.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I placed parenthesis around the Value of the 1st Add-Member.

That should work now


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

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Thank you very much. That worked perfectly Smiley Happy

You are simply superb as always.

Reply
0 Kudos