VMware Cloud Community
jvm2016
Hot Shot
Hot Shot
Jump to solution

Repair-Esxiconnection_powercli

Hi Luc,

could you check the following code and put some condition in orange line.

function Repair-Esxiconnection

{

  [cmdletbinding()]

    param (

        [parameter(mandatory = $true,

            valuefrompipeline = $true,

            valuefrompipelinebypropertyname = $true)]

        [string[]]$esxi

       

       

       

    )

    $cred=Get-Credential

    $e=get-vmhost $esxi

    Connect-VIServer -server $e -Credential $cred

   

     $vmk_management=(Get-VMHostNetworkAdapter -VMKernel -VMHost $e|?{$_.managementtrafficenabled -eq "$true"}).ip

     $ping=ping $vmk_management

     if($ping -e "")

     {

     $e|get-vmhostservice|?{$_.key -eq "vpxa"}|Restart-VMHostService -WhatIf

     }

     }

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

I did mean Test-NetConnection.

That cmdlet comes with the NetTCPIP module that is standard on Windows 10 and Windows Server 2016 afaik.

You can use the Test-Connection cmdlet, but then you would need to add the -Quiet switch and just test what the cmdlet returns for $true or $false.

  if (Test-Connection $vmk_management -Quiet) {

   $e|Get-VMHostService | Where-Object {$_.key -eq "vpxa"} | Restart-VMHostService -WhatIf

  }

And yes, if you want to restart when the test succeeds, you have to remove the -NOT


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

View solution in original post

0 Kudos
14 Replies
LucD
Leadership
Leadership
Jump to solution

Why don't you use the Test-NetConnection cmdlet?

That has a Boolean property indicating success or failure.

Something like this

function Repair-Esxiconnection

{

  [cmdletbinding()]

   param (

   [parameter(mandatory = $true,

   valuefrompipeline = $true,

   valuefrompipelinebypropertyname = $true)]

   [string[]]$esxi

   )


   $cred = Get-Credential

   $e = Get-VMHost $esxi

   Connect-VIServer -server $e -Credential $cred

   $vmk_management = (Get-VMHostNetworkAdapter -VMKernel -VMHost $e | Where-Object{$_.managementtrafficenabled -eq "$true"}).ip

   if(-not (Test-NetConnection $vmk_management).PingSucceeded)

   {

   $e|Get-VMHostService | Where-Object{$_.key -eq "vpxa"} | Restart-VMHostService -WhatIf

   }

}


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

0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

do yu mean test-connection as i dont find test-netconnection

also i want to restart vpxa when ping succededs so do i need to remove -not.

if(-not (Test-NetConnection $vmk_management).PingSucceeded)

   {

   $e|Get-VMHostService | Where-Object{$_.key -eq "vpxa"} | Restart-VMHostService -WhatIf

   }

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I did mean Test-NetConnection.

That cmdlet comes with the NetTCPIP module that is standard on Windows 10 and Windows Server 2016 afaik.

You can use the Test-Connection cmdlet, but then you would need to add the -Quiet switch and just test what the cmdlet returns for $true or $false.

  if (Test-Connection $vmk_management -Quiet) {

   $e|Get-VMHostService | Where-Object {$_.key -eq "vpxa"} | Restart-VMHostService -WhatIf

  }

And yes, if you want to restart when the test succeeds, you have to remove the -NOT


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

0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

thanks Luc,

it is working .

directly connecting to esxi using powercli can be considered similar operation as ssh'in to esxi .??

however we cant restart hostd only vpxa.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Not really, a SSH telnet session and Connect-VIServer to an ESXi node, use different ports (and require different FW rules).
Also with PowerCLI the SSH service doesn't need to be running.

On the other hand, you can't do all commands available through an SSH session.

For the hostd service you would need a SSH session, and run the /etc/init.d/hostd restart command.
But be aware that this would also kill your SSH session.


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

0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

thanks .so iam limited to vpxa start only through powercli and right now i have following . would you like to add use of plink.exe to start hostsd also .

function Repair-Esxiconnection

{

  [cmdletbinding()]

    param (

        [parameter(mandatory = $true,

            valuefrompipeline = $true,

            valuefrompipelinebypropertyname = $true)]

        [string]$esxi

       

       

       

    )

    $cred=Get-Credential

    $e=get-vmhost $esxi

    Connect-VIServer -server $e -Credential $cred

   

     $vmk_management=(Get-VMHostNetworkAdapter -VMKernel -VMHost $e|?{$_.managementtrafficenabled -eq "$true"}).ip

    

     if((Test-Connection $vmk_management -Quiet) -eq $true )

     {

     write-host "ping successds to " $vmk_management -BackgroundColor DarkGreen

     write-host "starting vpxa on " $e.name

     $e|get-vmhostservice|?{$_.key -eq "vpxa"}|Restart-VMHostService -WhatIf

     }

     }

plink.exe

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I would suggest to use the Posh-SSH module instead of plink.exe.

See Use Posh-SSH instead of PuTTY


Can you install that module?


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

0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

i have file in downlods

pastedImage_0.png

is it kind of similar operations as importing .psm1 module.??

do i need to make dir in "C:\Program Files\WindowsPowerShell\Modules"  and

use import-module .

i see its extension is different .

0 Kudos
LucD
Leadership
Leadership
Jump to solution

If you have a PC with an Internet connection you can do Install-Module -Name Posh-SSH.


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

0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

no internet .

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Do you have any PC with Internet connection? Perhaps at home?
On that one you can do Save-Module -Name Posh-SSH and then copy the files to the no-Internet PC.

But how did you get the nupkg file without Internet?


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

0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

well that file nupkg was dowloaded on different machine with net connection .i thought of moving it to desrired mchine (with no Internet)

and then import-module the way we did for .psm1 module.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The better way to do it is with Save-Module.
See the Offline Install of PowerCLI to a Computer Without an Internet Connection in the Welcome PowerCLI to the PowerShell Gallery – Install Process Updates post.


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

0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

Thanks I m going to check this.

0 Kudos