-
Notifications
You must be signed in to change notification settings - Fork 1
/
ServerProvisioning.CreateVM.ps1
57 lines (48 loc) · 2.28 KB
/
ServerProvisioning.CreateVM.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# Variable Declarations
. "$PSScriptRoot\ServerProvisioning.GlobalVariables.ps1"
# Prompt the user for Login Credentials
Write-Host "READ ME! Authentication: Enter your privileged user account. Example; jdoe" -ForegroundColor Yellow
$DomainUser = Read-Host -Prompt "Privileged User Account"
$DomainLogin = $DomainSuffix + $DomainUser
Write-Host "READ ME! Authentication: Enter your privileged user account password." -ForegroundColor Yellow
$DomainPassword = Read-Host -AsSecureString "Privileged User Account Password"
# Create credential sets
$DomainCredentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $DomainLogin,$DomainPassword
# Connect to vCenter
Connect-VIServer -Server $vCenterServer -Credential $DomainCredentials
#Prompt for Server information
$VMname = Read-Host -Prompt "Enter VM Name"
$VMname = $VMname.ToUpper().Trim()
$IPAddress = Read-Host -Prompt "Enter VM IP Address"
$VMDescription = Read-Host -Prompt "Enter VM Description"
# Set the VM IP Address
$SOSCM_Params = @{
IpMode = 'UseStaticIp'
IpAddress = $IPAddress
SubnetMask = $SubnetMask
DefaultGateway = $DefaultGateway
DNS = $DNS01,$DNS02
}
Get-OSCustomizationSpec $OSCusSpec |
Get-OSCustomizationNicMapping |
Set-OSCustomizationNicMapping @SOSCM_Params
# VM Placement conditions (cluster, folder location, and datastore)
if ($VMname -like $ProdVMConvention)
{
$Cluster = $ProdCluster
$FolderLocation = $ProdFolder
$DSname = $ProdStorage
}
if ($VMname -like $DevVMConvention)
{
$Cluster = $DevCluster
$FolderLocation = $DevFolder
$DSname = $DevStorage
}
$Datastore = Get-Datastore -Name $DSname | Sort-Object -Property FreeSpaceGB -Descending | Select -First 1
# Deploy Virtual Machine and remove temp custom spec
New-VM -Name $VMName -Template $Template -ResourcePool $Cluster -location $FolderLocation -DiskStorageFormat thin -Datastore $Datastore -OSCustomizationSpec $OSCusSpec
# Set VM Notes
Set-vm -vm $VMName -Notes $VMDescription -Confirm:$false
#Start the VM to continue the customization process
Start-VM -VM $VMname