-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerate_ad_dns_object.ps1
executable file
·151 lines (116 loc) · 5.92 KB
/
generate_ad_dns_object.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#check if PowerCLI is installed if not it will install
If (-not(Get-InstalledModule "VMware.PowerCLI" -ErrorAction silentlycontinue)) {
Write-Output "Installing VMware.PowerCLI..."
Install-Module -Name VMware.PowerCLI -Confirm:$false -AllowClobber -Force
}
$ErrorActionPreference = "silentlycontinue"
#l = Linux W = Windows
$serverType = 'l'
$templateName = "centos7_pkr"
#vSphere resources
$ResourcePool = "Low"
#Naming
$serverName = "linux-01"
$supCode = "1102"
#VM configuration
$MemoryGB = "1"
$NumCpu = "2"
$NetworkName = ""
$DatastoreDSC = "synol"
#Options Thin, Thick, and EagerZeroedThick
$DiskStorageFormat = "Thin"
#Networking Settings
#DNS servers are set in the OSCustomizationSpec
$IpAddress = "0.0.0.0"
$DefaultGateway = ""
$SubnetMask = ""
################################################################################
# Constant Variables
################################################################################
#$inputMode = 0
$vSphere = "192.168.50.136"
$linuxOU = "OU=Linux,OU=Managed Servers,DC=home,DC=lab"
$windowsOU = "OU=Windows,OU=Managed Servers,DC=home,DC=lab"
$securityGroupsOU = "OU=Servers,OU=Groups,OU=Services,OU=Administration,DC=home,DC=lab"
$dnsServer = "home.lab"
#This var isn't really needed we can concatenate to form needed info
$serverFQDN = "$($serverName).$(dnsServer)"
$systemAdministratorGroups = "home lab group"
$systemUserGroups = "SSSD Group",
"home lab group"
#Gets the Folder to place the VM based on sup code
$Location = Get-Folder -Name "*$($supCode)*"
#Generate the vSphere VM object name
$vcenterservername = $supCode + "_" + $serverName
################################################################################
# Script Logic
################################################################################
#Sets logic for Computer object OU
if($serverType.tolower() -eq 'l')
{
$selectedComputerOU = $linuxOU
$linuxOSCustomizationSpec = "Linux"
}
elseif($serverType.tolower() -eq 'w')
{
$selectedComputerOU = $windowsOU
$linuxOSCustomizationSpec = "Windows"
}
################################################################################
# Login to vSphere
################################################################################
Write-Output "`Connecting to $($vSphere)..."
Write-Output "`nEnter vSphere credentials:"
$privilegedCredential = Get-Credential
$ErrorActionPreference = "stop"
try
{
#$vsphereCredential = get-credential -Credential $null
Connect-VIServer -Server $vSphere -Credential $privilegedCredential
}
catch [VMware.VimAutomation.ViCore.Types.V1.ErrorHandling.InvalidLogin]
{
Write-Output "Invalid login, try again:"
#$vsphereCredential = get-credential -Credential $null
Connect-VIServer -Server $vSphere -Credential $privilegedCredential
}
catch
{
Write-Output "Unhandled exception error. Exiting."
}
$ErrorActionPreference = "silentlycontinue"
Write-Output "Login success..."
################################################################################
# Create AD Objects
################################################################################
Write-Output "`nWill create an AD Computer object with following parameters:`nObject Name: $($serverName)`nSAMAccountName: $($serverName)$`nDNS Name: $($serverFQDN)`nOU: $($selectedComputerOU)"
# New-ADComputer -path $selectedComputerOU -name $serverName -SAMAccountName "$($serverName)$" -DNSHostName $serverFQDN -Credential $privilegedCredential
Write-Output "`nWill create a new AD Group with the following parameters:`nGroup Name: $($serverName) - Server - Administrators`nOU: $($securityGroupsOU)`nMembers: $($systemAdministratorGroups)"
# New-ADGroup -Name "$($serverName) - admin group" -path $securityGroupsOU -ManagedBy $systemAdministratorGroups
#If Linux create SSSD group
if($serverType.tolower() -eq 'l')
{
#New-ADGroup -Name "$($serverName) - user group" -path $securityGroupsOU -ManagedBy $systemUserGroups
}
Write-Output "`nWill create a new DNS record (and accompanying PTR Record) with the following parameters:`nName: $serverName`nDNS Server: $($dnsServer)`nForward Lookup Zone: $($dnsServer)`nIP: $IpAddress"
# Add-DnsServerResourceRecordA -Name $serverName -ComputerName $dnsServer" -ZoneName $dnsServer -IPv4Address $IpAddress -CreatePtr
################################################################################
# Create VM
################################################################################
#clone vm OSCustomizationSpec
Get-OSCustomizationSpec -name $OSCustomizationSpec | New-OSCustomizationSpec -name temp -type nonpersistent
#set some settings
Get-OSCustomizationSpec -Name temp | Set-OSCustomizationSpec -NamingScheme "Fixed" -NamingPrefix $hostname
#Set networking information
Get-OSCustomizationSpec -Name temp | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIp -IpAddress $IpAddress -DefaultGateway $DefaultGateway -SubnetMask $SubnetMask
#Needs to be updates to be dynamic
$toClone = Get-Folder Templates | Get-Folder main | Get-Folder $linuxOSCustomizationSpec | get-template -Name $templateName
#Write-Output "`nWill create a VM in vCenter with following parameters:`nDatastore: $($datastore)`nTemplate: $($template)`nvCenter Server Name: $($vSphere)"
VMware.VimAutomation.Core\New-VM -Name $vcenterservername -Template $toClone -ResourcePool $ResourcePool -OSCustomizationSpec temp -Notes "" -Datastore $DatastoreDSC -DiskStorageFormat $DiskStorageFormat -Location $Location -NetworkName $NetworkName
Start-VM -VM $vcenterservername
################################################################################
# Fin`
################################################################################
Get-OSCustomizationSpec -name temp | Remove-OSCustomizationSpec -Confirm:$false
Write-Output "Loging Out"
Disconnect-viServer -server * -force -Confirm:$false