VMware Cloud Community
jvm2016
Hot Shot
Hot Shot

script_structure _powercli

Hi Luc,

i am checking to how many hosts a given root password is working and connecting .

though it is working by comparing $allesxi and $co(conncted esxi).

can yu modify it so that it specidically print name in catch block .

$esxi=Get-Content C:\users\jvmishra\desktop\firstnameinfqdn.txt | Sort-Object -Unique

$esxi.Count

$cred=Get-Credential

$domain=".test.com"

$allesxi = @()

foreach($e in $esxi)

{

$v=$e+$domain

$allesxi += $v

try

{

connect-viserver -server $v -Credential $cred|Out-Null -ErrorAction stop

}

catch

{

}

}

$co=$global:DefaultVIServers

write-host "folowing hosts are not connected with given root password " -ForegroundColor Gray

0 Kudos
8 Replies
LucD
Leadership
Leadership

You could do something like this

$esxi=Get-Content C:\users\jvmishra\desktop\firstnameinfqdn.txt | Sort-Object -Unique

$esxi.Count


$cred=Get-Credential

$domain = 'test.com'


$okesxi = @()

$failesxi = @()


foreach($e in $esxi)

{

    $v=$e,$domain -join '.'

    try

    {

        Connect-VIServer -server $v -Credential $cred -ErrorAction stop | Out-Null

        Write-Host "Connect ok for $e"

        $okesxi += $v

    }

    catch

    {

        Write-Host "Connect failed for $e"

        $failesxi += $v

    }

}


Write-Host "folowing hosts are not connected with given root password " -ForegroundColor Gray

$failesxi


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

0 Kudos
jvm2016
Hot Shot
Hot Shot

actually i want to see exact error meesage in catch block as some hosts not connecting as they are in non responding state .

throwing exception in catch block.

0 Kudos
LucD
Leadership
Leadership

You can create a catch block per type of exception

$esxi=Get-Content C:\users\jvmishra\desktop\firstnameinfqdn.txt | Sort-Object -Unique

$esxi.Count


$cred=Get-Credential

$domain=".test.com"


$okesxi = @()

$failesxi = @()


foreach($e in $esxi)

{

    $v=$e,$domain -join '.'

    try

    {

        Connect-VIServer -server $v -Credential $cred -ErrorAction stop | Out-Null

        Write-Host "Connect ok for $e"

        $okesxi += $v

    }

    catch [VMware.VimAutomation.ViCore.Types.V1.ErrorHandling.InvalidLogin]

    {

        Write-Host "Invalid credentials for $e"

        $failesxi += $v

    }

    catch{

        Write-Host "Could not connect to $e"

        $failesxi += $v

    }

}



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

0 Kudos
jvm2016
Hot Shot
Hot Shot

thanks luc .if i dont write anything in catch block as per my initial post will it not write the corresponding error (whatever) in that block??

0 Kudos
LucD
Leadership
Leadership

No ,due to the -ErrorAction Stop, the code goes into one of the catch-blocks.

No message is printed automatically in there.


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

0 Kudos
jvm2016
Hot Shot
Hot Shot

well my undestaing from previousthreads is if erroraction is stop then only it will throw the exception in catch block .

considering again the below piece of code i expect esxi name and corresponding error

try

{

connect-viserver -server $v -Credential $cred|Out-Null -ErrorAction stop

}

catch

{

$V

}

0 Kudos
LucD
Leadership
Leadership

No, you will not get the error.

You had the ErrorAction on the Out-Null cmdlet, not on the Connect-VIServer.

Your code never came in the catch block, you got the regular exception message


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

0 Kudos
jvm2016
Hot Shot
Hot Shot

i am going to check this one more time tomorrow ..

0 Kudos