VMware Cloud Community
hargma
Contributor
Contributor
Jump to solution

Refresh Host Network Information using PowerCLI

Hi all,

We currently use a scheduled script written using the perl API, which regularly refreshes the host network configuration, to pick up any new VLANS that may of been added.

foreach my $entity_view (@$entity_views) {
        my $host_views = Vim::find_entity_views(view_type => 'HostSystem',begin_entity => $entity_view);
        foreach (@$host_views) {
                my $netMgr = Vim::get_view(mo_ref => $_->configManager->networkSystem);
                $netMgr->RefreshNetworkSystem();
                sleep 120;

Can anyone advise how I can achieve the same using the Powershell API.

Many Thanks.

Reply
0 Kudos
1 Solution

Accepted Solutions
mattboren
Expert
Expert
Jump to solution

Hello, hargma-

You should be able to achieve this with:

$viewHosts = Get-View -ViewType HostSystem -Property Name,ConfigManager
foreach ($viewHost in $viewHosts) {
    $viewNetMgr = Get-View $viewHost.ConfigManager.NetworkSystem
    $viewNetMgr.RefreshNetworkSystem()
    Start-Sleep -Seconds 120 
} ## end foreach

Or, a bit more compactly:

Get-View -ViewType HostSystem -Property Name,ConfigManager | %{
    (Get-View $_.ConfigManager.NetworkSystem).RefreshNetworkSystem()
    Start-Sleep -Seconds 120 
} ## end foreach

Enjoy

Message was edited by: mattboren (corrected formatting that was off and comment in code)

View solution in original post

Reply
0 Kudos
3 Replies
mattboren
Expert
Expert
Jump to solution

Hello, hargma-

You should be able to achieve this with:

$viewHosts = Get-View -ViewType HostSystem -Property Name,ConfigManager
foreach ($viewHost in $viewHosts) {
    $viewNetMgr = Get-View $viewHost.ConfigManager.NetworkSystem
    $viewNetMgr.RefreshNetworkSystem()
    Start-Sleep -Seconds 120 
} ## end foreach

Or, a bit more compactly:

Get-View -ViewType HostSystem -Property Name,ConfigManager | %{
    (Get-View $_.ConfigManager.NetworkSystem).RefreshNetworkSystem()
    Start-Sleep -Seconds 120 
} ## end foreach

Enjoy

Message was edited by: mattboren (corrected formatting that was off and comment in code)

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

I have a strong feeling that the Start-Sleep should be out of the loop:

While ($true) {
  Get-View -ViewType HostSystem -Property Name,ConfigManager | `
  ForEach-Object {
    (Get-View $_.Configmanager.NetworkSystem).RefreshNetworkSystem()
  }
  Start-Sleep -Seconds 120
}


Regards, Robert

Message was edited by: RvdNieuwendijk

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
mattboren
Expert
Expert
Jump to solution

Hey, RvdNieuwendijk-

Depending on the way that the script is to be used, I would agree.

The Perl that hargma posted had the "sleep" piece inside of the foreach loop.  From that and the statement that the script is scheduled, I took it that he has a cron job that runs the script at regular intervals, and that the "sleep" was in the loop so as to stagger the hosts' RefreshNetworkSystem() calls.

But, if the script is something that is kicked off once, and the intent is to have it always running, and initiating a RefreshNetworkSystem() call on all hosts at the same time every 120 seconds, then, yes, your version would definitely be the way (the While loop that never stops, and the 120 sec. pause between each Foreach-Object).

Thanks for adding the other angle.

Reply
0 Kudos