ganapa2000
Enthusiast
Enthusiast

Unable to capture the output to the correct file

Jump to solution

Hi,

I am unable to validate and capture the output in the correct output file.

When validating, I am getting the screen output twice for VMs as below

DNS successfully updated on VM1

DNS successfully updated on VM1

Please help!!!

Script

$WPassword = "Password1", "Password2"

$ScriptBlock = @'

ipconfig /flushdns

ipconfig /registerdns

'@

$report = @()

$reportNotFound = @()

Import-Csv -Path ".\VM_Info.csv" -UseCulture -PipelineVariable row | where {($_.OS -match "Windows")} |

ForEach-Object -Process {

    $found = $false

    foreach ($pswd in $WPassword) {

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

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

    $sInvoke = @{

        VM              = $_.Name

        GuestCredential = $Creds

        ScriptTYpe      = 'powershell'

        ScriptText      = $ScriptBlock

        ErrorAction = 'Ignore'

    }

    try{

        Invoke-VMScript @sInvoke | out-null

        "DNS successfully updated on $($row.Name)"

        $report += $($row.Name)

       }

    catch{

        Throw "DNS Failed to update on $($row.Name)"

        $reportNotFound += $($row.Name)

    }

}  

    $result

    if(-not $found){

        $reportNotFound += $row

    }

}

$report | ft -auto

$report | Export-Csv -Path ".\DNS_Info.csv" -NoTypeInformation -UseCulture

$reportNotFound | ft -auto

$reportNotFound | Export-Csv -Path ".\No_DNS_Info.csv" -NoTypeInformation -UseCulture

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

You are trying to export a string instead of an object with properties through Export-Csv.

Try like this

$WPassword = "Password1", "Password2"

$ScriptBlock = @'

ipconfig /flushdns

ipconfig /registerdns

'@


$report = @()

$reportNotFound = @()

Import-Csv -Path ".\VM_Info.csv" -UseCulture -PipelineVariable row | where {($_.OS -match "Windows")} |

ForEach-Object -Process {

    $found = $false

    foreach ($pswd in $WPassword) {

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

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

    $sInvoke = @{

        VM              = $row.Name

        GuestCredential = $Creds

        ScriptTYpe      = 'powershell'

        ScriptText      = $ScriptBlock

        ErrorAction = 'Stop'

    }

    try{

        Invoke-VMScript @sInvoke | out-null

        "DNS successfully updated on $($row.Name)"

        $report += $row

        $found = $true

       }

    catch{

    }

}

    $result

    if(-not $found){

        $reportNotFound += $row

    }

}

$report | ft -auto

$report | Export-Csv -Path ".\DNS_Info.csv" -NoTypeInformation -UseCulture

$reportNotFound | ft -auto

$reportNotFound | Export-Csv -Path ".\No_DNS_Info.csv" -NoTypeInformation -UseCulture


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

View solution in original post

0 Kudos
6 Replies
LucD
Leadership
Leadership

Since you use ErrorAction 'Ignore' all Invoke-VMScript calls will proceed through the Try block.

And since you have 2 possible passwords, the same VM will appear twice in the report.


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

0 Kudos
ganapa2000
Enthusiast
Enthusiast

LucD,

After changing the ErrorAction to Stop, it is stopping the script after it find the error

DNS successfully updated on DB3

DNS Failed to update on DB3

At D:\myreports\DR_Get_VM_Info\2020\shared.ps1:62 char:9

+         Throw "DNS Failed to update on $($row.Name)"

+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : OperationStopped: (DNS Failed to update on MyDB3:String) [], RuntimeException

    + FullyQualifiedErrorId : DNS Failed to update on DB3

0 Kudos
LucD
Leadership
Leadership

That is because you use Throw in the catch block.

Throw stops the script


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

0 Kudos
ganapa2000
Enthusiast
Enthusiast

LucD,

After removing the throw in the script, it progresses further but is there a way to get the screen output only when the password is valid

also another issue is Output is not getting captured in the output file as I am seeing only

"Length"

"5"

"5"

"6"

"8"

"6"

"6"

"5"

"6"

"5"

"6"

"10"

"5"

"5"

0 Kudos
LucD
Leadership
Leadership

You are trying to export a string instead of an object with properties through Export-Csv.

Try like this

$WPassword = "Password1", "Password2"

$ScriptBlock = @'

ipconfig /flushdns

ipconfig /registerdns

'@


$report = @()

$reportNotFound = @()

Import-Csv -Path ".\VM_Info.csv" -UseCulture -PipelineVariable row | where {($_.OS -match "Windows")} |

ForEach-Object -Process {

    $found = $false

    foreach ($pswd in $WPassword) {

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

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

    $sInvoke = @{

        VM              = $row.Name

        GuestCredential = $Creds

        ScriptTYpe      = 'powershell'

        ScriptText      = $ScriptBlock

        ErrorAction = 'Stop'

    }

    try{

        Invoke-VMScript @sInvoke | out-null

        "DNS successfully updated on $($row.Name)"

        $report += $row

        $found = $true

       }

    catch{

    }

}

    $result

    if(-not $found){

        $reportNotFound += $row

    }

}

$report | ft -auto

$report | Export-Csv -Path ".\DNS_Info.csv" -NoTypeInformation -UseCulture

$reportNotFound | ft -auto

$reportNotFound | Export-Csv -Path ".\No_DNS_Info.csv" -NoTypeInformation -UseCulture


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

View solution in original post

0 Kudos
ganapa2000
Enthusiast
Enthusiast

Thank you LucD. That worked perfectly Smiley Happy

0 Kudos