Merge branch 'master' into master

This commit is contained in:
William Lam
2019-06-19 06:10:10 -07:00
committed by GitHub
9 changed files with 1439 additions and 170 deletions

View File

@@ -36,6 +36,7 @@ Description = 'PowerShell Module for Managing Hybrid Cloud Extension (HCX) on VM
PowerShellVersion = '6.0' PowerShellVersion = '6.0'
# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export. # Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
FunctionsToExport = 'Connect-HcxServer', 'Get-HcxCloudConfig', 'Get-HcxEndpoint', 'New-HcxMigration', 'Get-HcxMigration', 'Connect-HcxVAMI', FunctionsToExport = 'Connect-HcxServer', 'Get-HcxCloudConfig', 'Get-HcxEndpoint', 'New-HcxMigration', 'Get-HcxMigration', 'Connect-HcxVAMI',
'Get-HcxVCConfig', 'Set-HcxLicense', 'Set-HcxVCConfig', 'Get-HcxNSXConfig', 'Set-HcxNSXConfig', 'Get-HcxCity', 'Get-HcxLocation', 'Set-HcxLocation', 'Get-HcxVCConfig', 'Set-HcxLicense', 'Set-HcxVCConfig', 'Get-HcxNSXConfig', 'Set-HcxNSXConfig', 'Get-HcxCity', 'Get-HcxLocation', 'Set-HcxLocation',
'Get-HcxRoleMapping', 'Set-HcxRoleMapping', 'Get-HcxProxy', 'Set-HcxProxy', 'Remove-HcxProxy', 'Connect-HcxCloudServer', 'Get-HCXCloudActivationKey', 'Get-HcxRoleMapping', 'Set-HcxRoleMapping', 'Get-HcxProxy', 'Set-HcxProxy', 'Remove-HcxProxy', 'Connect-HcxCloudServer', 'Get-HCXCloudActivationKey',

View File

@@ -453,12 +453,23 @@ Function Get-HcxMigration {
Get-HcxMigration -MigrationId <MigrationID> Get-HcxMigration -MigrationId <MigrationID>
#> #>
Param ( Param (
[Parameter(Mandatory=$false)][String]$MigrationId, [Parameter(Mandatory=$false)][String[]]$MigrationId,
[Switch]$RunningMigrations [Switch]$RunningMigrations
) )
If (-Not $global:hcxConnection) { Write-error "HCX Auth Token not found, please run Connect-HcxManager " } Else { If (-Not $global:hcxConnection) { Write-error "HCX Auth Token not found, please run Connect-HcxManager " } Else {
$spec = @{} If($PSBoundParameters.ContainsKey("MigrationId")){
$spec = @{
filter = @{
migrationId = $MigrationId
}
paging =@{
pageSize = $MigrationId.Count
}
}
} Else {
$spec = @{}
}
$body = $spec | ConvertTo-Json $body = $spec | ConvertTo-Json
$hcxQueryUrl = $global:hcxConnection.Server + "/migrations?action=query" $hcxQueryUrl = $global:hcxConnection.Server + "/migrations?action=query"
@@ -468,10 +479,10 @@ Function Get-HcxMigration {
$requests = Invoke-WebRequest -Uri $hcxQueryUrl -Method POST -Headers $global:hcxConnection.headers -UseBasicParsing $requests = Invoke-WebRequest -Uri $hcxQueryUrl -Method POST -Headers $global:hcxConnection.headers -UseBasicParsing
} }
$migrations = ($requests.content | ConvertFrom-Json).rows
if($PSBoundParameters.ContainsKey("MigrationId")){ if($PSBoundParameters.ContainsKey("MigrationId")){
$migrations = $migrations | where { $_.migrationId -eq $MigrationId } $migrations = ($requests.content | ConvertFrom-Json).items
} else {
$migrations = ($requests.content | ConvertFrom-Json).rows
} }
if($RunningMigrations){ if($RunningMigrations){
@@ -559,16 +570,22 @@ Function Get-HcxVCConfig {
#> #>
If (-Not $global:hcxVAMIConnection) { Write-error "HCX Auth Token not found, please run Connect-HcxVAMI " } Else { If (-Not $global:hcxVAMIConnection) { Write-error "HCX Auth Token not found, please run Connect-HcxVAMI " } Else {
$vcConfigUrl = $global:hcxVAMIConnection.Server + "/api/admin/global/config/vcenter" $vcConfigUrl = $global:hcxVAMIConnection.Server + "/api/admin/global/config/vcenter"
$pscConfigUrl = $global:hcxVAMIConnection.Server + "/api/admin/global/config/lookupservice"
if($PSVersionTable.PSEdition -eq "Core") { if($PSVersionTable.PSEdition -eq "Core") {
$vcRequests = Invoke-WebRequest -Uri $vcConfigUrl -Method GET -Headers $global:hcxVAMIConnection.headers -UseBasicParsing -SkipCertificateCheck $vcRequests = Invoke-WebRequest -Uri $vcConfigUrl -Method GET -Headers $global:hcxVAMIConnection.headers -UseBasicParsing -SkipCertificateCheck
$ssoRequests = Invoke-WebRequest -Uri $pscConfigUrl -Method GET -Headers $global:hcxVAMIConnection.headers -UseBasicParsing -SkipCertificateCheck
} else { } else {
$vcRequests = Invoke-WebRequest -Uri $vcConfigUrl -Method GET -Headers $global:hcxVAMIConnection.headers -UseBasicParsing $vcRequests = Invoke-WebRequest -Uri $vcConfigUrl -Method GET -Headers $global:hcxVAMIConnection.headers -UseBasicParsing
$ssoRequests = Invoke-WebRequest -Uri $pscConfigUrl -Method GET -Headers $global:hcxVAMIConnection.headers -UseBasicParsing
} }
$vcData = ($vcRequests.content | ConvertFrom-Json).data.items $vcData = ($vcRequests.content | ConvertFrom-Json).data.items
$ssoData = ($ssoRequests.content | ConvertFrom-Json).data.items
$tmp = [pscustomobject] @{ $tmp = [pscustomobject] @{
Name = $vcData.config.name; Name = $vcData.config.name;
UserName = $vcData.Config.userName
LookupServiceUrl = $ssoData.config.lookupServiceUrl
Version = $vcData.config.version; Version = $vcData.config.version;
Build = $vcData.config.buildNumber; Build = $vcData.config.buildNumber;
UUID = $vcData.config.vcuuid; UUID = $vcData.config.vcuuid;
@@ -578,6 +595,38 @@ Function Get-HcxVCConfig {
} }
} }
Function Get-HcxLicense {
<#
.NOTES
===========================================================================
Created by: Mark McGilly
Date: 4/29/2019
Organization: Liberty Mutual Insurance
===========================================================================
.SYNOPSIS
Returns the license key that is registered with HCX Manager
.DESCRIPTION
This cmdlet returns the license key registered with HCX Manager
.EXAMPLE
Get-HcxLicense
#>
If (-Not $global:hcxVAMIConnection) { Write-error "HCX Auth Token not found, please run Connect-HcxVAMI " } Else {
$hcxConfigUrl = $global:hcxVAMIConnection.Server + "/api/admin/global/config/hcx"
if($PSVersionTable.PSEdition -eq "Core") {
$licenseRequests = Invoke-WebRequest -Uri $hcxConfigUrl -Method GET -Headers $global:hcxVAMIConnection.headers -UseBasicParsing -SkipCertificateCheck
} else {
$licenseRequests = Invoke-WebRequest -Uri $hcxConfigUrl -Method GET -Headers $global:hcxVAMIConnection.headers -UseBasicParsing
}
$license = ($licenseRequests.content | ConvertFrom-Json).data.items
if($licenseRequests) {
$license.config.activationKey
}
}
}
Function Set-HcxLicense { Function Set-HcxLicense {
<# <#
.NOTES .NOTES
@@ -789,6 +838,7 @@ Function Get-HcxNSXConfig {
$tmp = [pscustomobject] @{ $tmp = [pscustomobject] @{
Name = $nsxData.config.url; Name = $nsxData.config.url;
UserName = $nsxData.config.userName
Version = $nsxData.config.version; Version = $nsxData.config.version;
HCXUUID = $nsxData.config.uuid; HCXUUID = $nsxData.config.uuid;
} }
@@ -1200,6 +1250,7 @@ Function Set-HcxProxy {
[Parameter(Mandatory=$True)]$ProxyPort, [Parameter(Mandatory=$True)]$ProxyPort,
[Parameter(Mandatory=$False)]$ProxyUser, [Parameter(Mandatory=$False)]$ProxyUser,
[Parameter(Mandatory=$False)]$ProxyPassword, [Parameter(Mandatory=$False)]$ProxyPassword,
[Parameter(Mandatory=$False)]$ProxyExclusions,
[Switch]$Troubleshoot [Switch]$Troubleshoot
) )
@@ -1214,7 +1265,7 @@ Function Set-HcxProxy {
config = @{ config = @{
proxyHost = "$ProxyServer"; proxyHost = "$ProxyServer";
proxyPort = "$ProxyPort"; proxyPort = "$ProxyPort";
nonProxyHosts = ""; nonProxyHosts = "$ProxyExclusions";
userName = "$ProxyUser"; userName = "$ProxyUser";
password = "$ProxyPassword"; password = "$ProxyPassword";
} }

File diff suppressed because it is too large Load Diff

Binary file not shown.

View File

@@ -690,7 +690,7 @@ Function Get-VMCLogicalNetwork {
Created by: Kyle Ruddy Created by: Kyle Ruddy
Date: 03/06/2018 Date: 03/06/2018
Organization: VMware Organization: VMware
Blog: https://thatcouldbeaproblem.com Blog: https://www.kmruddy.com
Twitter: @kmruddy Twitter: @kmruddy
=========================================================================== ===========================================================================
@@ -761,7 +761,7 @@ Function Remove-VMCLogicalNetwork {
Created by: Kyle Ruddy Created by: Kyle Ruddy
Date: 03/06/2018 Date: 03/06/2018
Organization: VMware Organization: VMware
Blog: https://thatcouldbeaproblem.com Blog: https://www.kmruddy.com
Twitter: @kmruddy Twitter: @kmruddy
=========================================================================== ===========================================================================
@@ -808,7 +808,7 @@ Function New-VMCLogicalNetwork {
Created by: Kyle Ruddy Created by: Kyle Ruddy
Date: 03/06/2018 Date: 03/06/2018
Organization: VMware Organization: VMware
Blog: https://thatcouldbeaproblem.com Blog: https://www.kmruddy.com
Twitter: @kmruddy Twitter: @kmruddy
=========================================================================== ===========================================================================
@@ -1418,11 +1418,205 @@ Twitter: @LucD22
} }
} }
} }
Function New-VMCSDDCCluster {
<#
.NOTES
===========================================================================
Created by: Kyle Ruddy
Date: 03/16/2019
Organization: VMware
Blog: https://www.kmruddy.com
Twitter: @kmruddy
===========================================================================
.SYNOPSIS
Creates a new cluster for the designated SDDC
.DESCRIPTION
Creates a new cluster
.EXAMPLE
New-VMCSDDCCluster -OrgName <Org Name> -SDDCName <SDDC Name> -HostCount 1 -CPUCoreCount 8
#>
[cmdletbinding(SupportsShouldProcess = $true,ConfirmImpact='High')]
param(
[Parameter(Mandatory=$true)][String]$OrgName,
[Parameter(Mandatory=$true)][String]$SDDCName,
[Parameter(Mandatory=$true)][Int]$HostCount,
[Parameter(Mandatory=$true)][ValidateSet("8","16","32")]$CPUCoreCount
)
if (-not $global:DefaultVMCServers) { Write-error "No VMC Connection found, please use the Connect-VMC to connect"; break }
$orgId = Get-VMCOrg -Name $OrgName | Select-Object -ExpandProperty Id
$sddcId = Get-VMCSDDC -Name $SDDCName -Org $OrgName | Select-Object -ExpandProperty Id
if(-not $orgId) {
Write-Host -ForegroundColor red "Unable to find Org $OrgName, please verify input"
break
}
if(-not $sddcId) {
Write-Host -ForegroundColor red "Unable to find SDDC $SDDCName, please verify input"
break
}
$sddcClusterSvc = Get-VmcService -Name com.vmware.vmc.orgs.sddcs.clusters
$sddcClusterCreateSpec = $sddcClusterSvc.Help.create.cluster_config.Create()
$sddcClusterCreateSpec.host_cpu_cores_count = $CPUCoreCount
$sddcClusterCreateSpec.num_hosts = $HostCount
$sddcClusterTask = $sddcClusterSvc.Create($org.Id, $sddc.Id, $sddcClusterCreateSpec)
$sddcClusterTask | Select-Object Id,Task_Type,Status,Created | Format-Table
}
Function Get-VMCSDDCCluster {
<#
.NOTES
===========================================================================
Created by: Kyle Ruddy
Date: 03/16/2019
Organization: VMware
Blog: https://www.kmruddy.com
Twitter: @kmruddy
===========================================================================
.SYNOPSIS
Retreives cluster information for the designated SDDC
.DESCRIPTION
Lists cluster information for an SDDC
.EXAMPLE
Get-VMCSDDCCluster -OrgName <Org Name> -SDDCName <SDDC Name> -HostCount 1 -CPUCoreCount 8
#>
[cmdletbinding(SupportsShouldProcess = $true,ConfirmImpact='Low')]
param(
[Parameter(Mandatory=$true)][String]$OrgName,
[Parameter(Mandatory=$true)][String]$SddcName
)
if (-not $global:DefaultVMCServers) { Write-error "No VMC Connection found, please use the Connect-VMC to connect"; break }
$orgId = Get-VMCOrg -Name $OrgName | Select-Object -ExpandProperty Id
$sddcId = Get-VMCSDDC -Name $SDDCName -Org $OrgName | Select-Object -ExpandProperty Id
if(-not $orgId) {
Write-Host -ForegroundColor red "Unable to find Org $OrgName, please verify input"
break
}
if(-not $sddcId) {
Write-Host -ForegroundColor red "Unable to find SDDC $SDDCName, please verify input"
break
}
$clusterOutput = @()
$sddcClusters = Get-VMCSDDC -Org $OrgName -Name $SDDCName | Select-Object -ExpandProperty resource_config | Select-Object -ExpandProperty clusters
foreach ($c in $sddcClusters) {
$tempCluster = "" | Select-Object Id, Name, State
$tempCluster.Id = $c.cluster_id
$tempCluster.Name = $c.cluster_name
$tempCluster.State = $c.cluster_state
$clusterOutput += $tempCluster
}
return $clusterOutput
}
Function New-VMCSDDCCluster {
<#
.NOTES
===========================================================================
Created by: Kyle Ruddy
Date: 03/16/2019
Organization: VMware
Blog: https://www.kmruddy.com
Twitter: @kmruddy
===========================================================================
.SYNOPSIS
Creates a new cluster for the designated SDDC
.DESCRIPTION
Creates a new cluster
.EXAMPLE
New-VMCSDDCCluster -OrgName <Org Name> -SDDCName <SDDC Name> -HostCount 1 -CPUCoreCount 8
#>
[cmdletbinding(SupportsShouldProcess = $true,ConfirmImpact='High')]
param(
[Parameter(Mandatory=$true)][String]$OrgName,
[Parameter(Mandatory=$true)][String]$SddcName,
[Parameter(Mandatory=$true)][Int]$HostCount,
[Parameter(Mandatory=$false)][ValidateSet("8","16","36","48")]$CPUCoreCount
)
if (-not $global:DefaultVMCServers) { Write-error "No VMC Connection found, please use the Connect-VMC to connect"; break }
$orgId = Get-VMCOrg -Name $OrgName | Select-Object -ExpandProperty Id
$sddcId = Get-VMCSDDC -Name $SDDCName -Org $OrgName | Select-Object -ExpandProperty Id
if(-not $orgId) {
Write-Host -ForegroundColor red "Unable to find Org $OrgName, please verify input"
break
}
if(-not $sddcId) {
Write-Host -ForegroundColor red "Unable to find SDDC $SDDCName, please verify input"
break
}
$sddcClusterSvc = Get-VmcService -Name com.vmware.vmc.orgs.sddcs.clusters
$sddcClusterCreateSpec = $sddcClusterSvc.Help.create.cluster_config.Create()
$sddcClusterCreateSpec.host_cpu_cores_count = $CPUCoreCount
$sddcClusterCreateSpec.num_hosts = $HostCount
$sddcClusterTask = $sddcClusterSvc.Create($org.Id, $sddc.Id, $sddcClusterCreateSpec)
$sddcClusterTask | Select-Object Id,Task_Type,Status,Created | Format-Table
}
Function Remove-VMCSDDCCluster {
<#
.NOTES
===========================================================================
Created by: Kyle Ruddy
Date: 03/16/2019
Organization: VMware
Blog: https://www.kmruddy.com
Twitter: @kmruddy
===========================================================================
.SYNOPSIS
Removes a specified cluster from the designated SDDC
.DESCRIPTION
Deletes a cluster from an SDDC
.EXAMPLE
Remove-VMCSDDCCluster -OrgName <Org Name> -SDDCName <SDDC Name> -Cluster <Cluster Name>
#>
[cmdletbinding(SupportsShouldProcess = $true,ConfirmImpact='High')]
param(
[Parameter(Mandatory=$true)][String]$OrgName,
[Parameter(Mandatory=$true)][String]$SDDCName,
[Parameter(Mandatory=$true)][String]$ClusterName
)
if (-not $global:DefaultVMCServers) { Write-error "No VMC Connection found, please use the Connect-VMC to connect"; break }
$orgId = Get-VMCOrg -Name $OrgName | Select-Object -ExpandProperty Id
$sddcId = Get-VMCSDDC -Name $SDDCName -Org $OrgName | Select-Object -ExpandProperty Id
$clusterId = Get-VMCSDDCCluster -SddcName $SDDCName -OrgName $OrgName | Where-Object {$_.Name -eq $ClusterName} | Select-Object -ExpandProperty Id
if(-not $orgId) {
Write-Host -ForegroundColor red "Unable to find Org $OrgName, please verify input"
break
}
if(-not $sddcId) {
Write-Host -ForegroundColor red "Unable to find SDDC $SDDCName, please verify input"
break
}
if(-not $clusterId) {
Write-Host -ForegroundColor red "Unable to find cluster $ClusterName, please verify input"
break
}
$sddcClusterTask = $sddcClusterSvc.Delete($orgId, $sddcId, $clusterId)
$sddcClusterTask | Select-Object Id,Task_Type,Status,Created | Format-Table
}
Export-ModuleMember -Function 'Get-VMCCommand', 'Connect-VMCVIServer', 'Get-VMCOrg', 'Get-VMCSDDC', Export-ModuleMember -Function 'Get-VMCCommand', 'Connect-VMCVIServer', 'Get-VMCOrg', 'Get-VMCSDDC',
'Get-VMCTask', 'Get-VMCSDDCDefaultCredential', 'Get-VMCSDDCPublicIP', 'Get-VMCVMHost', 'Get-VMCTask', 'Get-VMCSDDCDefaultCredential', 'Get-VMCSDDCPublicIP', 'Get-VMCVMHost',
'Get-VMCSDDCVersion', 'Get-VMCFirewallRule', 'Export-VMCFirewallRule', 'Import-VMCFirewallRule', 'Get-VMCSDDCVersion', 'Get-VMCFirewallRule', 'Export-VMCFirewallRule', 'Import-VMCFirewallRule',
'Remove-VMCFirewallRule', 'Get-VMCLogicalNetwork', 'Remove-VMCLogicalNetwork', 'New-VMCLogicalNetwork', 'Remove-VMCFirewallRule', 'Get-VMCLogicalNetwork', 'Remove-VMCLogicalNetwork', 'New-VMCLogicalNetwork',
'Get-VMCSDDCSummary', 'Get-VMCPublicIP', 'New-VMCPublicIP', 'Remove-VMCPublicIP', 'Set-VMCSDDC', 'Get-VMCSDDCSummary', 'Get-VMCPublicIP', 'New-VMCPublicIP', 'Remove-VMCPublicIP', 'Set-VMCSDDC',
'Get-VMCEdge', 'Get-VMCEdgeNic', 'Get-VMCEdgeStatus', 'Get-VMCEdgeNicStat', 'Get-VMCEdgeUplinkStat' 'Get-VMCEdge', 'Get-VMCEdgeNic', 'Get-VMCEdgeStatus', 'Get-VMCEdgeNicStat', 'Get-VMCEdgeUplinkStat',
'Get-VMCSDDCCluster', 'New-VMCSDDCCluster', 'Remove-VMCSDDCCluster'

View File

@@ -0,0 +1,12 @@
<#
Script name: get-migrations.ps1
Created on: 20/12/2018
Author: Chris Bradshaw @aldershotchris
Description: The purpose of the script is to list the currently running + recently finished VM migrations.
Dependencies: None known
#>
Function Get-Migrations{
Get-Task |
Where-Object{$_.Name -eq "RelocateVM_Task"} |
Select-Object @{Name="VM";Expression={Get-VM -Id $_.ObjectID}}, State, @{Name="% Complete";Expression={$_.PercentComplete}},StartTime
}

View File

@@ -0,0 +1,34 @@
function Move-DatastoreCluster {
<#
.SYNOPSIS
Moves a datastore cluster to a new location
.DESCRIPTION
Will move a datastore cluster to a new location
.NOTES
Author: Kyle Ruddy, @kmruddy
.PARAMETER DatastoreCluster
Specifies the datastore cluster you want to move.
.PARAMETER Destination
Specifies a destination where you want to place the datastore cluster
.EXAMPLE
Move-DatastoreCluster -DatastoreCluster $DSCluster -Destination $DSClusterFolder
Moves the $DSCluster datastore cluster to the specified $DSClusterFolder folder.
#>
[CmdletBinding(SupportsShouldProcess = $True)]
param(
[Parameter(Mandatory=$false,Position=0,ValueFromPipelineByPropertyName=$true)]
[VMware.VimAutomation.ViCore.Types.V1.DatastoreManagement.DatastoreCluster]$DatastoreCluster,
[Parameter(Mandatory=$false,Position=1,ValueFromPipelineByPropertyName=$true)]
[VMware.VimAutomation.ViCore.Types.V1.Inventory.Folder]$Destination
)
if ($Global:DefaultVIServer.IsConnected -eq $false) {
Write-Warning -Message "No vCenter Server connection found."
break
}
If ($Pscmdlet.ShouldProcess($DatastoreCluster,"Move Datastore Cluster")) {
$Destination.ExtensionData.MoveIntoFolder($DatastoreCluster.ExtensionData.MoRef)
}
}

View File

@@ -1,147 +1,158 @@
function Save-PowerCLI { function Save-PowerCLI {
<# <#
.SYNOPSIS .SYNOPSIS
Advanced function which can be used to easily download specific versions of PowerCLI from an online gallery Advanced function which can be used to easily download specific versions of PowerCLI from an online gallery
.DESCRIPTION .DESCRIPTION
Downloads a specific version of PowerCLI and all the dependencies at the appropriate version Downloads a specific version of PowerCLI and all the dependencies at the appropriate version
.NOTES .NOTES
Author: 1.0 - Dimitar Milov Author: 1.0 - Dimitar Milov
Author: 2.0 - Kyle Ruddy, @kmruddy Author: 2.0 - Kyle Ruddy, @kmruddy
.PARAMETER RequiredVersion Author: 2.1 - Luc Dekens, @LucD22
Dynamic parameter used to specify the PowerCLI version - fixed issue with downloading the correct versions
.PARAMETER Path - added a working cleanup of unwanted versions
Directory path where the modules should be downloaded .PARAMETER RequiredVersion
.PARAMETER Repository Dynamic parameter used to specify the PowerCLI version
Repository to access the PowerCLI modules .PARAMETER Path
.PARAMETER Simple Directory path where the modules should be downloaded
Switch used to specify the nested version folders should be removed (therefore adding PowerShell 3/4 compatibility) .PARAMETER Repository
.EXAMPLE Repository to access the PowerCLI modules
Save-PowerCLI -RequiredVersion '10.0.0.7895300' -Path .\Downloads\ .PARAMETER Simple
Downloads PowerCLI 10.0.0 to the Downloads folder Switch used to specify the nested version folders should be removed (therefore adding PowerShell 3/4 compatibility)
.EXAMPLE .EXAMPLE
Save-PowerCLI -RequiredVersion '6.5.2.6268016' -Path .\Downloads\ -Simple Save-PowerCLI -RequiredVersion '10.0.0.7895300' -Path .\Downloads\
Downloads PowerCLI 6.5.2 to the Downloads folder and removes the nested version folders Downloads PowerCLI 10.0.0 to the Downloads folder
#> .EXAMPLE
[CmdletBinding(SupportsShouldProcess = $True)] Save-PowerCLI -RequiredVersion '6.5.2.6268016' -Path .\Downloads\ -Simple
param( Downloads PowerCLI 6.5.2 to the Downloads folder and removes the nested version folders
[Parameter(Mandatory = $true, Position = 1)] #>
[ValidateScript( { Test-Path $_} )] [CmdletBinding(SupportsShouldProcess = $True)]
$Path, param(
[Parameter(Mandatory = $false, Position = 2)] [Parameter(Mandatory = $true, Position = 1)]
[string]$Repository = 'PSGallery', [ValidateScript( { Test-Path $_} )]
[Parameter(Mandatory = $false, Position = 3)] $Path,
[Switch]$Simple [Parameter(Mandatory = $false, Position = 2)]
) [string]$Repository = 'PSGallery',
DynamicParam [Parameter(Mandatory = $false, Position = 3)]
{ [Switch]$Simple
# Set the dynamic parameters name )
$ParameterName = 'RequiredVersion' DynamicParam
{
# Set the dynamic parameters name
$ParameterName = 'RequiredVersion'
# Create the dictionary # Create the dictionary
$RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary $RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
# Create the collection of attributes # Create the collection of attributes
$AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute] $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
# Create and set the parameters' attributes # Create and set the parameters' attributes
$ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute
$ParameterAttribute.ValueFromPipeline = $true $ParameterAttribute.ValueFromPipeline = $true
$ParameterAttribute.ValueFromPipelineByPropertyName = $true $ParameterAttribute.ValueFromPipelineByPropertyName = $true
$ParameterAttribute.Mandatory = $true $ParameterAttribute.Mandatory = $true
$ParameterAttribute.Position = 0 $ParameterAttribute.Position = 0
# Add the attributes to the attributes collection # Add the attributes to the attributes collection
$AttributeCollection.Add($ParameterAttribute) $AttributeCollection.Add($ParameterAttribute)
# Generate and set the ValidateSet # Generate and set the ValidateSet
$pcliVersions = Find-Module -Name 'VMware.PowerCLI' -AllVersions $pcliVersions = Find-Module -Name 'VMware.PowerCLI' -AllVersions
$arrSet = $pcliVersions | select-Object -ExpandProperty Version $arrSet = $pcliVersions | select-Object -ExpandProperty Version
$ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet) $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet)
# Add the ValidateSet to the attributes collection # Add the ValidateSet to the attributes collection
$AttributeCollection.Add($ValidateSetAttribute) $AttributeCollection.Add($ValidateSetAttribute)
# Create and return the dynamic parameter # Create and return the dynamic parameter
$RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterName, [string], $AttributeCollection) $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterName, [String], $AttributeCollection)
$RuntimeParameterDictionary.Add($ParameterName, $RuntimeParameter) $RuntimeParameterDictionary.Add($ParameterName, $RuntimeParameter)
return $RuntimeParameterDictionary return $RuntimeParameterDictionary
}
begin {
$powercliModuleName = 'VMware.PowerCLI'
$desiredPowerCLIModule = Find-Module -Name $powercliModuleName -RequiredVersion $PSBoundParameters.RequiredVersion -Repository $Repository
$depsOrder = 'VMware.VimAutomation.Sdk', 'VMware.VimAutomation.Common', 'VMware.Vim', 'VMware.VimAutomation.Cis.Core', 'VMware.VimAutomation.Core', 'VMware.VimAutomation.Nsxt', 'VMware.VimAutomation.Vmc', 'VMware.VimAutomation.Vds', 'VMware.VimAutomation.Srm', 'VMware.ImageBuilder', 'VMware.VimAutomation.Storage', 'VMware.VimAutomation.StorageUtility', 'VMware.VimAutomation.License', 'VMware.VumAutomation', 'VMware.VimAutomation.HorizonView', 'VMware.DeployAutomation', 'VMware.VimAutomation.vROps', 'VMware.VimAutomation.PCloud'
$orderedDependencies = @()
foreach ($depModuleName in $depsOrder) {
$orderedDependencies += $desiredPowerCLIModule.Dependencies | Where-Object {$_.Name -eq $depModuleName}
} }
begin { foreach ($remainingDep in $desiredPowerCLIModule.Dependencies) {
$powercliModuleName = 'VMware.PowerCLI' if ($orderedDependencies.Name -notcontains $remainingDep.Name) {
$desiredPowerCLIModule = Find-Module -Name $powercliModuleName -RequiredVersion $RequiredVersion -Repository $Repository $orderedDependencies += $remainingDep
$depsOrder = 'VMware.VimAutomation.Sdk', 'VMware.VimAutomation.Common', 'VMware.Vim', 'VMware.VimAutomation.Cis.Core', 'VMware.VimAutomation.Core', 'VMware.VimAutomation.Nsxt', 'VMware.VimAutomation.Vmc', 'VMware.VimAutomation.Vds', 'VMware.VimAutomation.Srm', 'VMware.ImageBuilder', 'VMware.VimAutomation.Storage', 'VMware.VimAutomation.StorageUtility', 'VMware.VimAutomation.License', 'VMware.VumAutomation', 'VMware.VimAutomation.HorizonView', 'VMware.DeployAutomation', 'VMware.VimAutomation.vROps', 'VMware.VimAutomation.PCloud'
$orderedDependencies = @()
foreach ($depModuleName in $depsOrder) {
$orderedDependencies += $desiredPowerCLIModule.Dependencies | Where-Object {$_.Name -eq $depModuleName}
}
foreach ($remainingDep in $desiredPowerCLIModule.Dependencies) {
if ($orderedDependencies.Name -notcontains $remainingDep.Name) {
$orderedDependencies += $remainingDep
}
}
}
process {
# Save PowerCLI Module Version
$desiredPowerCLIModule | Save-Module -Path $Path
# Working with the depenent modules
foreach ($dependency in $orderedDependencies) {
if (Get-ChildItem -Path (Join-Path $path $dependency.Name) | Where-Object {$_.Name -ne $dependency.MinimumVersion}) {
# Save dependencies with minimum version
Find-Module $dependency.Name -RequiredVersion $dependency.MinimumVersion | Save-Module -Path $Path
# Remove newer dependencies version
Get-ChildItem -Path (Join-Path $path $dependency.Name) | Where-Object {$_.Name -ne $dependency.MinimumVersion} | Remove-Item -Confirm:$false -Force -Recurse
}
}
}
end {
if ($Simple) {
function FolderCleanup {
param(
[Parameter(Mandatory = $true, Position = 0)]
[ValidateScript( { Test-Path $_} )]
$ParentFolder,
[Parameter(Mandatory = $true, Position = 1)]
[String]$ModuleName,
[Parameter(Mandatory = $true, Position = 2)]
$Version
)
$topFolder = Get-Item -Path (Join-Path $ParentFolder $ModuleName)
$versionFolder = $topFolder | Get-ChildItem -Directory | Where-Object {$_.Name -eq $Version}
$versionFolder | Get-ChildItem | Copy-Item -Destination $topFolder
# Checking for any nested folders within the PowerCLI module version folder
if ($versionFolder| Get-ChildItem -Directory) {
# Obtaining and storing the child items to a variable, then copying the items to the parent folder's nested folder
$nestFolder = $versionFolder| Get-ChildItem -Directory
foreach ($nestDir in $nestFolder) {
$nestDir | Get-ChildItem | Copy-Item -Destination (Join-Path $topFolder $nestDir.Name)
}
}
# Removing any of the former, no longer needed, directory structure
$versionFolder| Remove-Item -Recurse -Force
}
FolderCleanup -ParentFolder $Path -ModuleName $desiredPowerCLIModule.Name -Version $desiredPowerCLIModule.Version
foreach ($cleanUp in $orderedDependencies) {
FolderCleanup -ParentFolder $Path -ModuleName $cleanUp.Name -Version $cleanUp.MinimumVersion
}
} }
} }
} }
process {
# Save PowerCLI Module Version
$desiredPowerCLIModule | Save-Module -Path $Path
# Working with the depenent modules
foreach ($dependency in $orderedDependencies) {
if (Get-ChildItem -Path (Join-Path $path $dependency.Name) | Where-Object {$_.Name -ne $dependency.MinimumVersion}) {
# Save dependencies with minimum version
Find-Module $dependency.Name -RequiredVersion $dependency.MinimumVersion | Save-Module -Path $Path
}
}
}
end {
Get-Item -Path "$($Path)\*" -PipelineVariable dir |
ForEach-Object -Process {
$children = Get-ChildItem -Path $dir.FullName -Directory
if($children.Count -gt 1){
$tgtVersion = $orderedDependencies.GetEnumerator() | where {$_.Name -eq $dir.Name}
$children | where{$_.Name -ne $tgtVersion.MinimumVersion} |
ForEach-Object -Process {
Remove-Item -Path $_.FullName -Recurse -Force -Confirm:$false
}
}
}
if ($Simple) {
function FolderCleanup {
param(
[Parameter(Mandatory = $true, Position = 0)]
[ValidateScript( { Test-Path $_} )]
$ParentFolder,
[Parameter(Mandatory = $true, Position = 1)]
[String]$ModuleName,
[Parameter(Mandatory = $true, Position = 2)]
$Version
)
$topFolder = Get-Item -Path (Join-Path $ParentFolder $ModuleName)
$versionFolder = $topFolder | Get-ChildItem -Directory | Where-Object {$_.Name -eq $Version}
$versionFolder | Get-ChildItem | Copy-Item -Destination $topFolder
# Checking for any nested folders within the PowerCLI module version folder
if ($versionFolder| Get-ChildItem -Directory) {
# Obtaining and storing the child items to a variable, then copying the items to the parent folder's nested folder
$nestFolder = $versionFolder| Get-ChildItem -Directory
foreach ($nestDir in $nestFolder) {
$nestDir | Get-ChildItem | Copy-Item -Destination (Join-Path $topFolder $nestDir.Name)
}
}
# Removing any of the former, no longer needed, directory structure
$versionFolder| Remove-Item -Recurse -Force
}
FolderCleanup -ParentFolder $Path -ModuleName $desiredPowerCLIModule.Name -Version $desiredPowerCLIModule.Version
foreach ($cleanUp in $orderedDependencies) {
FolderCleanup -ParentFolder $Path -ModuleName $cleanUp.Name -Version $cleanUp.MinimumVersion
}
}
}
}

View File

@@ -0,0 +1,50 @@
function Set-NetworkAdapterOpaqueNetwork {
param(
[Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 1)]
[VMware.VimAutomation.Types.NetworkAdapter]
$NetworkAdapter,
[Parameter(Mandatory = $true, Position = 2)]
[string]
$OpaqueNetworkName,
[Parameter()]
[switch]
$Connected,
[Parameter()]
[switch]
$StartConnected
)
process {
$opaqueNetwork = Get-View -ViewType OpaqueNetwork | ? {$_.Name -eq $OpaqueNetworkName}
if (-not $opaqueNetwork) {
throw "'$OpaqueNetworkName' network not found."
}
$opaqueNetworkBacking = New-Object VMware.Vim.VirtualEthernetCardOpaqueNetworkBackingInfo
$opaqueNetworkBacking.OpaqueNetworkId = $opaqueNetwork.Summary.OpaqueNetworkId
$opaqueNetworkBacking.OpaqueNetworkType = $opaqueNetwork.Summary.OpaqueNetworkType
$device = $NetworkAdapter.ExtensionData
$device.Backing = $opaqueNetworkBacking
if ($StartConnected) {
$device.Connectable.StartConnected = $true
}
if ($Connected) {
$device.Connectable.Connected = $true
}
$spec = New-Object VMware.Vim.VirtualDeviceConfigSpec
$spec.Operation = [VMware.Vim.VirtualDeviceConfigSpecOperation]::edit
$spec.Device = $device
$configSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
$configSpec.DeviceChange = @($spec)
$NetworkAdapter.Parent.ExtensionData.ReconfigVM($configSpec)
# Output
Get-NetworkAdapter -Id $NetworkAdapter.Id
}
}