Skip to content

Commit 68ba470

Browse files
committed
update cmdlet Copy-VNVIRole for easier and more flexible use
1 parent 5ddf2f9 commit 68ba470

File tree

3 files changed

+40
-21
lines changed

3 files changed

+40
-21
lines changed

ChangeLog.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44

55
- \[improvement] updated function `Get-VNVMByAddress`:
66
- greatly increased speed by using `VMware.Vim.SearchIndex` for by-IP searches (does support wildcard, though -- by wildcard still uses slower mechanism); example speed increase: went from 13s to about 0.25s in a vCenter with about 7,500 VMs -- woo-hoo!
7-
- added new parameters `-GuestHostname` and `-UUID` to support for searching by guest DNS name or VM BIOS UUID (these also use a method of `VMware.Vim.SearchIndex`, so the searches are super fast)
8-
7+
- added new parameters `-GuestHostname` and `-UUID` to support for searching by guest DNS name or VM BIOS UUID, respectively (these also use a method of `VMware.Vim.SearchIndex`, so the searches are super fast)
8+
- \[improvement] updated function `Copy-VNVIRole`:
9+
- takes new parameter, `-SourceRole`, for passing VIRole object itself as value, and this parameter accepts value from pipeline, for more natural use of cmdlet
10+
- deduces source vCenter server from `-SourceRole` value, simplifying use of cmdlet (no need to specify `-SourceVCName` parameter when providing the source VIRole object)
11+
- `-DestinationVCName` parameter now optional, further simplifying use of cmdlet; if parameter not specified, destination vCenter will be the same as the source vCenter
12+
913

1014
#### v1.1, released 20 Dec 2016
1115

ToDo.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
- support for accepting VMHost ID from pipeline (by property), so that the following works tip top:
77
`Get-Cluster myCluster | Get-VMHost | Get-VNVMHostBrokenUplink`
88
- update function `Get-VNVMByVirtualPortGroup` to take a standard- or distributed virtual portgroup as a parameter (and, from pipeline)
9-
- update function `Copy-VNVIRole` to take SourceVIRole as VIRole object, and to accept it from pipeline
109
- update function `Get-VNUplinkNicForVM` to give more meaningful VDSwitch identification (currently returns something to the effect of `DvsPortset-1` for the VDSwitch name)
1110
- ?add function for changing VM boot order
1211
- investigate using a PropertyCollector to more quickly retrieve VM network adapter MAC addresses for function `Find-VNVMWithDuplicateMACAddress`

vNugglets.Utility/vNuggletsUtilityMod_functions.ps1

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,49 +1064,65 @@ function Copy-VNVIRole {
10641064
This assumes that connections to source/destination vCenter(s) are already established. If role of given name already exists in destination vCenter, this attempt will stop.
10651065
10661066
.Example
1067-
Copy-VNVIRole -SourceRoleName SysAdm -DestinationRoleName SysAdm_copyTest -SourceVCName vcenter.dom.com -DestinationVCName othervcenter.dom.com
1068-
Copy the VIRole "SysAdm" from the given source vCenter to a new VIRole named "SysAdm_copyTest" in the given destination vCenter
1067+
Get-VIRole -Server vcenter.dom.com -Name SysAdm | Copy-VNVIRole -DestinationVCName othervcenter.dom.com
1068+
Copy the VIRole "SysAdm" from the given source vCenter (taken from pipeline) to a new VIRole of the same name as the source VIRole and in the given destination vCenter
10691069
10701070
.Example
1071-
Copy-VNVIRole -SourceRoleName MyTestRole0 -DestinationRoleName SomeRole_copyTest -SourceVCName vcenter.dom.com -DestinationVCName vcenter.dom.com
1072-
Copy the given VIRole from the given source vCenter to a new VIRole named "SysAdm_copyTest" in the _same_ vCenter
1071+
Get-VIRole -Server vcenter.dom.com -Name TestRole0 | Copy-VNVIRole -DestinationRoleName TestRole0_copy
1072+
Make a copy the VIRole with the new role name in the source vCenter
1073+
1074+
.Example
1075+
Copy-VNVIRole -SourceRoleName SysAdm -DestinationRoleName SysAdm_copyTest -SourceVCName vcenter.dom.com -DestinationVCName othervcenter.dom.com
1076+
Copy the VIRole "SysAdm" from the given source vCenter to a new VIRole named "SysAdm_copyTest" in the given destination vCenter
10731077
10741078
.Link
10751079
http://vNugglets.com
10761080
10771081
.Outputs
1078-
VMware.VimAutomation.Types.PermissionManagement.Role if role is created/updated, String in Warning stream and nothing in standard out otherwise
1082+
VMware.VimAutomation.Types.PermissionManagement.Role if role is created/updated, String in Warning stream and/or Error object, and nothing in standard out otherwise
10791083
#>
10801084
[CmdletBinding(SupportsShouldProcess=$true)]
10811085
[OutputType([VMware.VimAutomation.Types.PermissionManagement.Role])]
10821086
param(
1087+
## Source VIRole object to copy
1088+
[parameter(Mandatory=$true, ValueFromPipeline=$true, ParameterSetName="SourceVIRoleByObj")][VMware.VimAutomation.Types.PermissionManagement.Role]$SourceRole,
1089+
10831090
## Name of the source VIRole
1084-
[parameter(Mandatory=$true,Position=0)][string]$SourceRoleName,
1091+
[parameter(Mandatory=$true,Position=0, ParameterSetName="SourceVIRoleByName")][string]$SourceRoleName,
10851092

10861093
## Name to use for new destination VIRole. If none, will use name from source role
1087-
[parameter(Mandatory=$true,Position=1)][string]$DestinationRoleName,
1094+
[parameter(Position=1)][string]$DestinationRoleName,
10881095

10891096
## Source vCenter connection name
1090-
[parameter(Mandatory=$true)][string]$SourceVCName,
1097+
[parameter(Mandatory=$true, ParameterSetName="SourceVIRoleByName")][string]$SourceVCName,
10911098

1092-
## Destination vCenter connection name (to copy VIRole to same vCenter, use same vCenter name for destination as used for source)
1093-
[parameter(Mandatory=$true)][string]$DestinationVCName
1099+
## Destination vCenter connection name, if different from source vCenter (to copy VIRole to same vCenter, either do not use this parameter, or use same vCenter name for destination as used for source)
1100+
[string]$DestinationVCName
10941101
) ## end param
10951102

10961103
process {
1097-
## get the VIRole from the source vCenter
1098-
$oSrcVIRole = Get-VIRole -Server $SourceVCName -Name $SourceRoleName -ErrorAction:SilentlyContinue
1099-
## if the role does not exist in the source vCenter
1100-
if ($null -eq $oSrcVIRole) {Throw "VIRole '$SourceRoleName' does not exist in source vCenter '$SourceVCName'. No source VIRole from which to copy"}
1101-
if (-not $PSBoundParameters.ContainsKey("DestinationRoleName")) {$DestinationRoleName = $oSrcVIRole.Name}
1104+
$oSrcVIRole = Switch ($PSCmdlet.ParameterSetName) {
1105+
"SourceVIRoleByObj" {$SourceRole; break}
1106+
"SourceVIRoleByName" {
1107+
## get the VIRole from the source vCenter
1108+
try {Get-VIRole -Server $SourceVCName -Name $SourceRoleName -ErrorAction:Stop}
1109+
## if there was issue getting the role from the source vCenter
1110+
catch {
1111+
Write-Warning "Issue getting '$SourceRoleName' from source vCenter '$SourceVCName'. Is connection to vCenter ok, and does that VIRole exist there?"
1112+
$PsCmdlet.ThrowTerminatingError($_)
1113+
} ## end catch
1114+
} ## end case
1115+
} ## end switch
1116+
$strDestVIRoleName = if ($PSBoundParameters.ContainsKey("DestinationRoleName")) {$DestinationRoleName} else {$oSrcVIRole.Name}
1117+
$strDestVCName = if ($PSBoundParameters.ContainsKey("DestinationVCName")) {$DestinationVCName} else {$oSrcVIRole.Server}
11021118
## see if there is VIRole by the given name in the destination vCenter
1103-
$oDestVIRole = Get-VIRole -Server $DestinationVCName -Name $DestinationRoleName -ErrorAction:SilentlyContinue
1119+
$oDestVIRole = Get-VIRole -Server $strDestVCName -Name $strDestVIRoleName -ErrorAction:SilentlyContinue
11041120

11051121
## if the role already exists in the destination vCenter
1106-
if ($null -ne $oDestVIRole) {Throw "VIRole '$DestinationRoleName' already exists in destination vCenter '$DestinationVCName'"}
1122+
if ($null -ne $oDestVIRole) {Throw "VIRole '$strDestVIRoleName' already exists in destination vCenter '$strDestVCName'"}
11071123
## else, create the role
11081124
else {
1109-
New-VIRole -Server $DestinationVCName -Name $DestinationRoleName -Privilege (Get-VIPrivilege -Server $DestinationVCName -Id $oSrcVIRole.PrivilegeList)
1125+
New-VIRole -Server $strDestVCName -Name $strDestVIRoleName -Privilege (Get-VIPrivilege -Server $strDestVCName -Id $oSrcVIRole.PrivilegeList)
11101126
} ## end else
11111127
} ## end process
11121128
} ## end fn

0 commit comments

Comments
 (0)