Automation

 View Only
  • 1.  Need help in validating Passwords

    Posted Jul 16, 2020 01:50 PM

    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



  • 2.  RE: Need help in validating Passwords

    Posted Jul 16, 2020 02:05 PM

    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?



  • 3.  RE: Need help in validating Passwords

    Posted Jul 16, 2020 02:10 PM

    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.



  • 4.  RE: Need help in validating Passwords

    Posted Jul 16, 2020 02:24 PM

    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



  • 5.  RE: Need help in validating Passwords

    Posted Jul 16, 2020 02:58 PM

    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



  • 6.  RE: Need help in validating Passwords
    Best Answer

    Posted Jul 16, 2020 04:07 PM

    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



  • 7.  RE: Need help in validating Passwords

    Posted Jul 16, 2020 04:48 PM

    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.



  • 8.  RE: Need help in validating Passwords

    Posted Jul 16, 2020 05:22 PM

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

    That should work now



  • 9.  RE: Need help in validating Passwords

    Posted Jul 16, 2020 05:56 PM

    Thank you very much. That worked perfectly :smileyhappy:

    You are simply superb as always.