-
Notifications
You must be signed in to change notification settings - Fork 1
/
rename-portgroup.pl
executable file
·78 lines (63 loc) · 2.12 KB
/
rename-portgroup.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/perl -w
# --
# rename-portgroup.pl - Rename Portgroup Name in Distributed Switch
# https://github.com/aleex42/VMware-Automation
# --
# Copyright (C) 2013 Alexander Krogltoh, E-Mail: git <at > krogloth.de
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (GPL). If you
# did not receive this file, see http://www.gnu.org/licenses/gpl.txt.
# --
use strict;
use warnings;
use VMware::VIRuntime;
use VMware::VILib;
my %opts = (
'dvswitch' => {
type => "=s",
help => "Name of dvSwitch",
required => 1,
},
'vlan' => {
type => "=i",
help => "VLAN-ID",
required => 1,
},
'new_name' => {
type => "=s",
help => "New Name for VLAN",
required => 1,
},
);
Opts::add_options(%opts);
Opts::parse();
Opts::validate();
Util::connect();
my $dvswitch = Opts::get_option('dvswitch');
my $vlan = Opts::get_option('vlan');
my $newname = Opts::get_option('new_name');
my $dvSwitches = Vim::find_entity_views(view_type => 'DistributedVirtualSwitch', filter => {'name' => $dvswitch});
foreach my $dvs (@$dvSwitches) {
foreach my $dvs (@$dvSwitches) {
if(defined($dvs->portgroup)) {
my $dvPortgroups = $dvs->portgroup;
foreach my $dvpg (@$dvPortgroups) {
my $dvpgView = Vim::get_view(mo_ref => $dvpg);
my $vlan_id = $dvpgView->{'config'}->{'defaultPortConfig'}->{'vlan'}->{'vlanId'};
if($vlan_id eq $vlan){
my $configVersion = $dvpgView->{'config'}->{'configVersion'};
my $spec = DVPortgroupConfigSpec->new(name => $newname, configVersion => $configVersion);
$dvpgView->ReconfigureDVPortgroup_Task(spec => $spec);
if($@){
print "Error: " . $@ . "\n";
} else {
print $dvpgView->{'name'} . " => " . $newname . "\n";
}
last;
}
}
}
}
}
Util::disconnect();