Skip to content

Instantly share code, notes, and snippets.

@mtboren
Last active February 15, 2017 22:37
Show Gist options
  • Save mtboren/ffe9f302699281ce4ab85a3897dccb68 to your computer and use it in GitHub Desktop.
Save mtboren/ffe9f302699281ce4ab85a3897dccb68 to your computer and use it in GitHub Desktop.
Use parameters for PowerShell, and accept them from pipeline by property name, for ease of script consumption by user (including getting help on the script -- no need to open the script in an editor -- use Get-Help!). The .ps1 file shows an example of what a parameterized-version of @lamw's vSphere-lab-deployment PowerShell script might look lik…
{
"VIServer": "vcenter.mydom.com",
"VIUsername": "administrator@vsphere.local",
"VIPassword": "!!!extraSweetPassword!!!",
"DeploymentTarget": "VCENTER",
"NestedESXiApplianceOVA": "C:\\temp\\Nested_ESXi6.5_Appliance_Template_v1.ova",
"VCSAInstallerPath": "C:\\temp\\test-VMware-VCSA-extr",
"NestedESXiHostnameToIPs": {
"vesxi65-1": "172.1.0.10",
"vesxi65-2": "172.1.0.11",
"vesxi65-3": "172.1.0.12"
},
"NestedESXivCPU": 3,
"NestedESXivMEM": 7,
"NestedESXiCachingvDisk": 5,
"NestedESXiCapacityvDisk": 9,
"VCSADeploymentSize": "tiny",
"VCSADisplayName": "vcenter65-1",
"VCSAIPAddress": "172.1.0.22",
"VCSAHostname": "vcenter65-1.mydom.com",
"VCSAPrefix": 24,
"VCSASSODomainName": "mydom.local",
"VCSASSOSiteName": "mySSOSite",
"VCSASSOPassword": "VMware1!",
"VCSARootPassword": "VMware1!",
"VCSASSHEnable": false,
"VMNetwork": "myVMNetwk0",
"VMDatastore": "sweetSuperfastDStoreCluster",
"VMNetmask": "255.255.248.0",
"VMGateway": "172.1.0.1",
"VMDNS": "172.1.0.1",
"VMNTP": "pool.ntp.org",
"VMPassword": "vmware123",
"VMDomain": "mydom.com",
"VMSyslog": "mysyslogserver.mydom.com",
"VMSSH": true,
"VMVMFS": true,
"VMCluster": "Test",
"NewVCDatacenterName": "Datacenter",
"NewVCVSANClusterName": "VSAN-Cluster"
}
<# .Description
Code to do super awesome things, and in a hurry!
This is an example snippet of parameterizing the .PS1 file at https://github.com/lamw/vghetto-vsphere-automated-lab-deployment/blob/master/vsphere-6.5-vghetto-standard-lab-deployment.ps1
And, these are just light example of some of the über-powerful Validation/flexibility available for parameters. Soo much more can be done to perform the validation up front
.Example
(Get-Content -Raw C:\Temp\myParamsForNewVsphereLab.json) | ConvertFrom-Json | New-vSphereLab.ps1
Take all parameters specified in the JSON file and use them in calling New-vSphereLab.ps1
This takes the JSON, converts it to an object with properties and values, and since the property names match the parameter names and the parameters take value from pipeline by property name, it's a match! The parameter is specified!
.Example
New-vSphereLab.ps1 -VMCluster someDifferentCluster
Create a new vSphere lab, thanks to lamw, accepting all of the default values except for the VMCluster -- use the specified cluster name there.
.Example
Get-Help -Full New-vSphereLab.ps1
Get the full, PowerShell-like (because it _is_ PowerShell help!) help for this script, with descriptions and default-value information for all parameters, with examples, etc. What a wonderful gift to the PowerShell user: they can get help in the way that they do with everything else in PowerShell.
#>
[CmdletBinding()]
param (
# Physical ESXi host or vCenter Server to deploy vSphere 6.5 lab
[parameter(ValueFromPipelineByPropertyName=$true)][string]$VIServer = "vcenter.primp-industries.com",
[parameter(ValueFromPipelineByPropertyName=$true)][string]$VIUsername = "administrator@vsphere.local",
[parameter(ValueFromPipelineByPropertyName=$true)][string]$VIPassword = "!!!MySuperDuperSecurePassword!!!",
# Specifies whether deployment is to an ESXi host or vCenter Server. Either ESXI or VCENTER
[parameter(ValueFromPipelineByPropertyName=$true)][ValidateSet("ESXI","VCENTER")]$DeploymentTarget = "VCENTER",
# Full Path to both the Nested ESXi 6.5 VA OVA
[parameter(ValueFromPipelineByPropertyName=$true)][ValidateScript({Test-Path -Path $_})]$NestedESXiApplianceOVA = "C:\Users\primp\Desktop\Nested_ESXi6.5_Appliance_Template_v1.ova",
# Full Path to both the extracted VCSA 6.5 ISO
[parameter(ValueFromPipelineByPropertyName=$true)][ValidateScript({Test-Path -PathType Container -Path $_})]$VCSAInstallerPath = "C:\Users\primp\Desktop\VMware-VCSA-all-6.5.0-4944578",
# Nested ESXi VMs to deploy. Expects hashtable with ESXi host shortnames as keys, and the corresponding IP addresses as values
[parameter(ValueFromPipelineByPropertyName=$true)][PSObject]$NestedESXiHostnameToIPs = @{
"vesxi65-1" = "172.30.0.171"
"vesxi65-2" = "172.30.0.172"
"vesxi65-3" = "172.30.0.173"
},
# Nested ESXi VM Resources -- number of vCPU
[parameter(ValueFromPipelineByPropertyName=$true)][int]$NestedESXivCPU = 2,
# Nested ESXi VM Resources -- amount of memory (GB)
[parameter(ValueFromPipelineByPropertyName=$true)][int]$NestedESXivMEM = 6, #GB
# Nested ESXi VM Resources -- size of caching vDisk (GB)
[parameter(ValueFromPipelineByPropertyName=$true)][int]$NestedESXiCachingvDisk = 4,
# Nested ESXi VM Resources -- size of capacity vDisk (GB)
[parameter(ValueFromPipelineByPropertyName=$true)][int]$NestedESXiCapacityvDisk = 8,
# VCSA Deployment Configuration
[parameter(ValueFromPipelineByPropertyName=$true)][ValidateSet("tiny","small","medium","large","massive")][string]$VCSADeploymentSize = "tiny",
## Display name for new VCSA VM
[parameter(ValueFromPipelineByPropertyName=$true)][string]$VCSADisplayName = "vcenter65-1",
[parameter(ValueFromPipelineByPropertyName=$true)][System.Net.IPAddress]$VCSAIPAddress = "172.30.0.170",
# VCSA hostname. Change to IP if you don't have valid DNS
[parameter(ValueFromPipelineByPropertyName=$true)][string]$VCSAHostname = "vcenter65-1.primp-industries.com",
# VCSA Subnet Mask prefix length
[parameter(ValueFromPipelineByPropertyName=$true)][int]$VCSAPrefix = 24,
[parameter(ValueFromPipelineByPropertyName=$true)][string]$VCSASSODomainName = "vghetto.local",
[parameter(ValueFromPipelineByPropertyName=$true)][string]$VCSASSOSiteName = "virtuallyGhetto",
[parameter(ValueFromPipelineByPropertyName=$true)][string]$VCSASSOPassword = "VMware1!",
[parameter(ValueFromPipelineByPropertyName=$true)][string]$VCSARootPassword = "VMware1!",
## Switch: enable SSH on VCSA?
[parameter(ValueFromPipelineByPropertyName=$true)][Switch]$VCSASSHEnable = $true,
# General Deployment Configuration for both Nested ESXi VMs + VCSA
[parameter(ValueFromPipelineByPropertyName=$true)][string]$VMNetwork = "access333",
[parameter(ValueFromPipelineByPropertyName=$true)][string]$VMDatastore = "himalaya-local-SATA-dc3500-0",
[parameter(ValueFromPipelineByPropertyName=$true)][System.Net.IPAddress]$VMNetmask = "255.255.255.0",
[parameter(ValueFromPipelineByPropertyName=$true)][System.Net.IPAddress]$VMGateway = "172.30.0.1",
[parameter(ValueFromPipelineByPropertyName=$true)][System.Net.IPAddress]$VMDNS = "172.30.0.100",
[parameter(ValueFromPipelineByPropertyName=$true)][string]$VMNTP = "pool.ntp.org",
[parameter(ValueFromPipelineByPropertyName=$true)][string]$VMPassword = "vmware123",
[parameter(ValueFromPipelineByPropertyName=$true)][string]$VMDomain = "primp-industries.com",
## Syslog server address
[parameter(ValueFromPipelineByPropertyName=$true)][string]$VMSyslog = "mysyslogserver.primp-industries.com",
# Applicable to Nested ESXi only
[parameter(ValueFromPipelineByPropertyName=$true)][Switch]$VMSSH = $true,
[parameter(ValueFromPipelineByPropertyName=$true)][Switch]$VMVMFS = $false,
# Name of destination vSphere cluster. Applicable to VC Deployment Target only
[parameter(ValueFromPipelineByPropertyName=$true)][ValidateScript({Get-Cluster -Name $_})][string]$VMCluster = "Primp-Cluster",
# Name of new vSphere Datacenter/Cluster when VCSA is deployed
[parameter(ValueFromPipelineByPropertyName=$true)][string]$NewVCDatacenterName = "Datacenter",
[parameter(ValueFromPipelineByPropertyName=$true)][string]$NewVCVSANClusterName = "VSAN-Cluster"
)
# this is just for returning, for demonstration purposes, what are the parameters and their values
$hshOut = [ordered]@{}
Get-Variable -Name (Get-Command -Name $PSCmdlet.MyInvocation.InvocationName).Parameters.Values.Name -ErrorAction:SilentlyContinue | %{$hshOut[$_.Name] = $_.Value}
$hshOut
## do the actual awesome stuff here...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment