Skip to content

Commit 543a886

Browse files
Initial commit
0 parents  commit 543a886

9 files changed

+1852
-0
lines changed

Add-DHCPReservation.ps1

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#---------------------------------------------------------------------------
2+
# Sets a DHCP reservation for a VM based on its MAC address discovered from VMM
3+
#
4+
#---------------------------------------------------------------------------
5+
6+
# Set variables for the target infrastructure
7+
# ----------
8+
Param(
9+
# VMM Server
10+
[Parameter(Mandatory=$false)]
11+
[string]$VMMServer = 'oncvmm01.infra.saaas.com',
12+
[Parameter(Mandatory=$false)]
13+
[System.Management.Automation.PSCredential]$SCVMMCred = (New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "INFRA\Administrator",(Get-Content "\\XADCSF01\C$\Scripts\MCSScripts\MCSCredential.txt" | ConvertTo-SecureString -Key (get-content "\\XADCSF01\C$\Scripts\MCSScripts\AES.key"))),
14+
15+
16+
#IP Address
17+
[Parameter(Mandatory=$true)]
18+
[string]$ipAddress = "10.10.51.141",
19+
20+
#Master image
21+
[Parameter(Mandatory=$true)]
22+
[string]$vm, # Name of VM you're creating a reservation for
23+
24+
[Parameter(Mandatory=$false)]
25+
[string]$dhcpServer = "localhost" # Name of DHCP server you're using
26+
27+
28+
29+
)
30+
#EndParamBlock
31+
32+
Import-Module DHCPServer
33+
Import-Module VirtualMachineManager
34+
35+
# Set up the connection to the SCVMM server
36+
Get-SCVMMServer -ComputerName $VMMServer -Credential $SCVMMCred | Out-Null
37+
38+
$vmNetworkAdapter = (Get-SCVirtualMachine $vm).VirtualNetworkAdapters
39+
if($vmNetworkAdapter.count -gt 1){
40+
Throw "More than one network adapter added to machine! I'm too lazy to deal with this."
41+
}
42+
if($vmNetworkAdapter.MACAddressType -eq "Dynamic"){
43+
Throw "MAC address on network adapter for $vm is Dynamic - need to change to STATIC"
44+
}
45+
46+
$macAddress = $vmNetworkAdapter.MACAddress -replace ":"
47+
48+
Write-Host "Adding DHCP reservation for $($vm), MAC address $($macAddress) with IP address $($ipAddress)" -fore Yellow
49+
if(Get-DhcpServerv4Reservation -IPAddress $ipAddress){
50+
# remove existing DHCP reservation if it exists!
51+
Write-Host "Removing existing DHCP reservation for $($ipAddress)" -fore Yellow
52+
Get-DhcpServerv4Reservation -IPAddress $ipAddress -ComputerName $dhcpServer | Remove-DhcpServerv4Reservation -ComputerName $dhcpServer
53+
Reconcile-DhcpServerv4IPRecord -ComputerName $dhcpServer -ScopeId $scopeID -Force
54+
}
55+
Add-DhcpServerv4Reservation -ComputerName $dhcpServer -Name "$($vm).$($env:UserDNSDomain)" -Description "$($vm).$($env:UserDNSDomain)" -IPAddress $ipAddress -ClientId $macAddress -ScopeID $scopeID
56+
57+
Write-Host "Adding MAC address $($macAddress) to DHCP Allow Filter" -fore Yellow
58+
Add-DhcpServerv4Filter -ComputerName $dhcpServer -List Allow -MacAddress $macAddress -Description "$($vm).$($env:UserDNSDomain)" -ErrorAction SilentlyContinue
59+
60+
61+
Write-Host "Forcing replication of scope to partner server" -fore yellow
62+
Invoke-DHCPServerv4FailoverReplication -Computername $dhcpServer -ScopeID $scopeID -Force

Build-MCSMachines.ps1

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
#---------------------------------------------------------------------------
2+
# Build MCS machines from MCS Catalog
3+
#
4+
#---------------------------------------------------------------------------
5+
6+
# Set variables for the target infrastructure
7+
# ----------
8+
Param(
9+
# VMM Server
10+
[Parameter(Mandatory=$false)]
11+
[string]$VMMServer = 'oncvmm01.infra.saaas.com',
12+
[Parameter(Mandatory=$false)]
13+
[System.Management.Automation.PSCredential]$SCVMMCred = (New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "INFRA\Administrator",(Get-Content "\\XADCSF01\C$\Scripts\MCSScripts\MCSCredential.txt" | ConvertTo-SecureString -Key (get-content "\\XADCSF01\C$\Scripts\MCSScripts\AES.key"))),
14+
15+
# XD Controllers
16+
[Parameter(Mandatory=$false)]
17+
[string]$adminAddress = 'xadcsf01.corp.saaas.com', #The XD Controller we're going to execute against
18+
[Parameter(Mandatory=$false)]
19+
[array]$xdControllers = ('xadcsf01.corp.saaas.com','xadcsf02.corp.saaas.com'),
20+
21+
# MCS Catalog
22+
[Parameter(Mandatory=$true)]
23+
[string]$machineCatalogName,
24+
25+
# Details on the machines you want built/updated
26+
[Parameter(ParameterSetName='addMachinesByCount',Mandatory=$false)]
27+
[ValidateRange(1,10)]
28+
[int]$noMachines,
29+
[Parameter(ParameterSetName='addMachinesByCount',Mandatory=$false)]
30+
$acctNameStartNumber = $null,
31+
[Parameter(ParameterSetName='addMachinesByName',Mandatory=$false)]
32+
$vdaName = $null,
33+
[Parameter(Mandatory=$false)]
34+
[switch]$addDHCPReservation = $true,
35+
[Parameter(Mandatory=$false)]
36+
$ipAddress = $null,
37+
[Parameter(Mandatory=$false)]
38+
$vmMAC = $null,
39+
[Parameter(Mandatory=$false)]
40+
$adNETBIOS = $env:USERDOMAIN,
41+
42+
43+
# Other options
44+
[Parameter(Mandatory=$false)]
45+
[string]$emailTo = "sysadmin@saaas.com"
46+
)
47+
48+
#EndParamBlock
49+
50+
cd 'C:\Scripts\MCSScripts'
51+
52+
# Email Report Variables
53+
$reportFilter = $machineCatalogName
54+
# ----------
55+
56+
# Load the Citrix PowerShell modules
57+
Write-Host "Loading Citrix & VMM modules." -fore Yellow
58+
if(!(Get-PSSnapin Citrix* -ea SilentlyContinue)){
59+
Add-PSSnapin Citrix*
60+
}
61+
if(!(Get-Module VirtualMachineManager -ea SilentlyContinue)){
62+
Import-Module virtualmachinemanager
63+
}
64+
65+
Write-Host "Attempting to connect to VMM..." -fore yellow -NoNewline
66+
# Set up the connection to the SCVMM server
67+
$vmmConnAttempts = 0
68+
do{
69+
$vmmConn = Get-SCVMMServer -ComputerName $VMMServer -Credential $SCVMMCred
70+
if($vmmConn -eq $null){
71+
Write-Host "It seems the connection to VMM has failed, pausing a bit and then retrying..." -fore Red
72+
Start-Sleep -Seconds 60
73+
$vmmConnAttempts++
74+
}
75+
}until($vmmConn -ne $null -or $vmmConnAttempts -gt 2)
76+
77+
if($vmmConn -eq $null){
78+
Write-Host "Couldn't connect to VMM! Emailing report and cancelling run." -ForegroundColor Red
79+
Send-MailMessage -To $emailTo -From "Citrix Reporting <$($env:computername)@$($env:userDNSdomain)>" -Subject "$($reportFilter) Build Task" -Body "The Update Task for $($reportFilter) failed, due to a VMM connection error" -SmtpServer mx-gslb.saaas.com
80+
Exit
81+
}else{
82+
Write-Host "done!" -ForegroundColor Yellow
83+
}
84+
85+
86+
# Get Machine Catalog details
87+
88+
$machineCatalog = Get-BrokerCatalog -Name $machineCatalogName
89+
90+
# Create necessary new AD accounts, if you specify vdaName, it'll just create one account/one VM.
91+
92+
$acctIDPool = Get-AcctIdentityPool -AdminAddress $adminAddress -IdentityPoolName $machineCatalogName
93+
if($vdaName -ne $null){
94+
Write-Host "Creating AD Account for $($vdaName)" -fore Yellow
95+
$adAccts= New-AcctADAccount -AdminAddress $adminAddress -IdentityPoolName $machineCatalogName -ADAccountName $vdaName
96+
97+
}elseif($acctNameStartNumber -match "[0-9]"){
98+
Write-Host "Restting account identity pool $($machineCatalogName) to start at $($acctNameStartNumber)." -ForegroundColor Yellow
99+
$adAccts= New-AcctADAccount -AdminAddress $adminAddress -Count $noMachines -IdentityPoolName $machineCatalogName -StartCount $acctNameStartNumber
100+
}else{
101+
$adAccts= New-AcctADAccount -AdminAddress $adminAddress -Count $noMachines -IdentityPoolName $machineCatalogName
102+
}
103+
104+
foreach($adAcct in $adAccts.SuccessfulAccounts){
105+
106+
$VDAName = $adAcct.ADAccountName -replace "$($adNETBIOS)\\" -replace "\$"
107+
if($ipAddress -eq $null){
108+
$ipAddress = "10.10.51.$([int]$VDAName.Substring(2,2))$([int]$VDAName.Substring(8,1))"
109+
}
110+
Write-Host "Creating new VM $($VDAName), IP address $($ipAddress)" -fore Yellow
111+
$buildTask = New-ProvVM -ADAccountName $adAcct.ADAccountName -AdminAddress $adminAddress -ProvisioningSchemeName $machineCatalogName -RunAsynchronously
112+
do{
113+
$completion = $provTask.TaskExpectedCompletion - (Get-Date)
114+
$provTask = Get-ProvTask -AdminAddress $adminAddress -TaskId $buildTask.Guid
115+
Write-Progress -Activity "Building VDA $($VDAName) - $([math]::Round($completion.TotalMinutes,0)) minutes remaining" -Status "$($provTask.TaskState) - $([int]$provTask.TaskProgress)%" -PercentComplete ([int]$provTask.TaskProgress)
116+
Start-Sleep 5
117+
}until($provTask.Active -eq $false)
118+
119+
if($provTask.TaskState -ne "Finished"){
120+
Write-Error "The creation task for $($VDAName) failed with error: $($provTask.TaskState). Cancelling..."
121+
Continue
122+
}elseif($provTask.TaskState -eq "Finished"){
123+
Write-Host "Finished creating the VM $($VDAName)!" -fore Green
124+
125+
}
126+
Write-Host "Adding $($VDAName) to machine catalog" -fore Yellow
127+
New-BrokerMachine -AdminAddress $adminAddress -CatalogUid $machineCatalog.Uid -MachineName $adAcct.ADAccountSid | Out-Null
128+
129+
Write-Host "Setting CPU Compatibility mode to enabled" -ForegroundColor Yellow
130+
Set-SCVirtualMachine -VM $VDAName -CPULimitForMigration $true | Out-Null
131+
132+
Write-Host "Adding blank SCSI adaptor" -fore Yellow
133+
New-SCVirtualScsiAdapter -VM $VDAName -RunAsynchronously | Out-Null
134+
135+
Write-Host "Setting MAC address to static" -fore Yellow
136+
Get-SCVirtualNetworkAdapter -VM $VDAName | Set-SCVirtualNetworkAdapter -MACAddressType Static | Out-Null
137+
138+
Write-Host "Setting memory to static" -fore Yellow
139+
Set-SCVirtualMachine -VM $VDAName -DynamicMemoryEnabled $false
140+
141+
142+
if($vmMAC -ne $null){
143+
# convert MAC from 001DD8B71C1E to 00-1D-D8-B7-1C-1E if it doesn't match that pattern
144+
if($vmMac -notmatch '..-..-..-..-..-..'){
145+
$vmMAC = ($vmMAC -replace '(..)','$1-').Trim('-')
146+
}
147+
Write-Host "Setting VM MAC address" -fore Yellow
148+
Get-SCVirtualNetworkAdapter -VM $VDAName | Set-SCVirtualNetworkAdapter -MACAddressType Static -MACAddress $vmMAC | Out-Null
149+
150+
}
151+
152+
153+
if($addDHCPReservation){
154+
Write-Host "Adding DHCP reservation for $($VDAName), IP: $($ipAddress)" -ForegroundColor Yellow
155+
.\Add-DHCPReservation.ps1 `
156+
-ipAddress $ipAddress `
157+
-vm $VDAName `
158+
}
159+
160+
}
161+
162+
.\MCS-Report.ps1 `
163+
-filter $reportFilter `
164+
-emailTo $emailTo `
165+
-emailSubject "$($machineCatalogName) Build Task"
166+
167+

0 commit comments

Comments
 (0)