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
Something like this?
$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
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
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.
Try like this
$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
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
Something like this?
$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
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.
I placed parenthesis around the Value of the 1st Add-Member.
That should work now
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you very much. That worked perfectly
You are simply superb as always.