VMware Cloud Community
R3van
Contributor
Contributor

Setting a delay in powercli script for tagging hosts

Scenario is:  Tagging multiple hosts from my flat switch. I will lose connectivity to hosts until I plug in my startech device and assign the tag to it.  when the script runs it pretty much just stalls after the 1st host. I have tried removing the discconect-viserver.   /Any thoughts? see script below.   Looking for something that acts like nohup sleep command.   Also start-sleep is not what i need. 

$file_hosts = "c:\ps\ip.csv"

#Login Variables

$auth_usr = "root"

$auth_pwd = "Youwillneverknow"

#variables - Management network

$pg_ManageNet = "Management Network"

#variables - CVM network

$Host_CVM_VLAN = 3

$New_NamePortGroupCVM = "CVM_MGNT"

$pg_CVM = "VM Network"

   

  Import-csv $file_hosts | ForEach-Object {

    $host_ntnx = $_.hostntnx 

   

    Connect-VIServer -Server $host_ntnx -User $auth_usr -Password $auth_pwd -Force

    Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -WarningAction SilentlyContinue -Confirm: $false

    Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $false -Confirm: $false

    Write-Host "******************************************************************"

    Write-Host "******************************************************************"

    Write-Host "connected to" $host_ntnx

    Write-Host "******************************************************************"

    Write-Host "******************************************************************"

    Write-Host "Configuring CVM Portgroup for" $host_ntnx

    #Setting vSwitch0 Variable

    $VSwitch0 = Get-VirtualSwitch -VMHost $host_ntnx -Name "vSwitch0"

    #rename/tag CVM port group

    Write-Host "rename/tagging cvm portgroup"

    $VSwitch0 = Get-VirtualSwitch -VMHost $host_ntnx -Name "vSwitch0"

    $CVM_PortGroup_Config = Get-VirtualPortGroup -VMHost $host_ntnx -VirtualSwitch $VSwitch0 -Name $pg_CVM | Set-VirtualPortGroup -Name $New_NamePortGroupCVM -VlanId $Host_CVM_VLAN

   

    if($?)

{

Write-Host "******************************************************************"

Write-Host "******************************************************************"

  Write-Host "command succeeded"

Write-Host "******************************************************************"

Write-Host "******************************************************************"

  

}

else

{

Write-Host "******************************************************************"

Write-Host "******************************************************************"

   Write-Host "command failed"

Write-Host "******************************************************************"

Write-Host "******************************************************************"

}

    Write-Host "******************************************************************"

    Write-Host "******************************************************************"

    Write-Host "Configuring Host Portgroup for" $host_ntnx

    #Tag Management Network

    Write-Host "Tagging mgmt network"

    $TAGnetwork = Get-VirtualPortGroup -VMHost $host_ntnx -VirtualSwitch $VSwitch0 -Name $pg_ManageNet | Set-VirtualPortGroup -VlanId $Host_CVM_VLAN

   

        Write-Host "******************************************************************"

    Write-Host "******************************************************************"

    Write-Host "Finished DNS Config" $host_ntnx

    Write-Host "******************************************************************"

    Write-Host "******************************************************************"

    Write-Host "Disconnecting from" $host_ntnx

    Disconnect-VIServer -Server $host_ntnx -Confirm:$false

 

    }

Tags (3)
0 Kudos
3 Replies
LucD
Leadership
Leadership

Can't you wait in a loop and check with the Test-NetConnection cmdlet?


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

0 Kudos
R3van
Contributor
Contributor

I am not sure how to 'wait' a loop.  So this is part of a larger script.  Part of my job is installing software configuring IPs, etc.  Pretty much prepping to put onto the customer network.  If i tag I lose connectivity which is meant to happen.  I just need the script to keep running.  But what happens is it tags the first host and gets stuck bc it can't communicate back and forth any more.

0 Kudos
LucD
Leadership
Leadership

With the Test-NetConnection you can verify that a host is reachable (consider it an improved 'ping').
As long as the host is not replying, you stay in the loop.

Effectively waiting for the host to be reachable again, before the script continues.

In this snippet I use Test-Connection, which is even simpler

$hostName = 'Host-to-be-tested'

while(-not (Test-Connection -ComputerName $hostName -Quiet)){

    sleep 5

}

# Continue with the other lines in the script


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

0 Kudos