|
| 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