Created
June 11, 2019 15:56
-
-
Save eguyd/bec7a8aa613bf02f939d340e792406e0 to your computer and use it in GitHub Desktop.
VM Deployement using HyperV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$vSwitch = "Deployment Test" | |
$VHDXPath = "D:\Hyper-V\Virtual hard disks\VHDXPath\" | |
$SubMaskBit = "24" | |
#DOMAIN CONTROLLER SETTINGS | |
$DCVMName = "TEST-DC01" | |
$DCIP = "10.0.0.50" | |
$DomainMode = "WinThreshold" | |
$ForestMode = "WinThreshold" | |
$DomainName = "TestDomain.local" | |
$DSRMPWord = convertto-securestring "Pa55w.rd" -AsPlainText -Force | |
$NewAdminUserName = convertto-securestring "Administrator" -AsPlainText -Force | |
$NewAdminUserPWord = convertto-securestring "Pa55w.rd" -AsPlainText -Force | |
#FILE SERVER SETTINGS | |
$FSVMName = "TEST-FS01" | |
$FSIP = "10.0.0.51" | |
$SharePath = "C:\ShareTest" | |
$FolderName = "Public" | |
$ShareName = "Public" | |
#FILE SERVER VM LOGIN | |
$DCLocalUser = "$DCVMName\Administrator" | |
$DCLocalPWord = convertto-securestring "Pa55w.rd" -AsPlainText -Force | |
$DCLocalCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $DCLocalUser, $DCLocalPword | |
#DOMAIN CONTROLLER VM LOGIN | |
$FSDomainUser = "$DomainName\administrator" | |
$FSDomainPWord = convertto-securestring "Pa55w.rd!" -AsPlainText -Force | |
$DomainCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $FSDomainUser, $FSDomainPword | |
$DomainUser = "$DomainName\administrator" | |
$DomainPWord = ConvertTo-SecureString -String "Password01" -AsPlainText -Force | |
$DomainCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $DomainUser, $DomainPWord | |
##################################### COMMAND EXECUTION #################################### | |
Write-Verbose "Copying Master VHDX and Deploying New VM with name [DCVMName]" -Verbose | |
Copy-Item "$VHDXPath\Master.vhdx" "$VHDXPath\$DCVMNAME.vhdx" | |
Write-Verbose "VHDX Copied, Building VM...." -Verbose | |
New-VM -Name $DCVMName -MemoryStartupBytes 1GB -VHDPath "$VHDXPath\$DCVName.vhdx" -Generation 2 -SwitchName $vSwitch | |
Start-VM -Name $DCVName | |
Write-Verbose "Waiting for Powershell Direct to start on VM [$DCVMName]" -Verbose | |
Write-Verbose "PowerShell Direct responding on VM [$DCVMName]. Moving On..." -Verbose | |
#INSTALL AD DS | |
Invoke-Command -VMName $DCVMName -Credential $DCLocalCredential -ScriptBlock { | |
param ($DCVMName, $DomainMode, $ForestMode, $DomainName, $DSRMPWord) | |
Write-Verbose "Installing Active Directory Services on VM [$DCVMName]" -Verbose | |
Install -WindowsFeature -Name "AD-Domain-Services" -IncludeManagementTools | |
Write-Verbose "Configuring New Domain with Name [$DomainName] on VM [$DCVMName]" -Verbose | |
Install-ADDDSForest -ForestMode $ForestMode -DomainMode $DomainMode -DomainName $DomainName -InstallDns -NoDNSonNetwork -SafeModeAdministratorPassword $DSRMPWord -Force -NoRebootOnCompletion | |
} -ArgumentList $DCVMName, $DomainMode, $ForestMode, $DomainMode, $DSRMPWord | |
Write-Verbose "Rebooting VM [$DCVMName] to complete installation of new AD Forest" -Verbose | |
Stop-VM -Name $DCVMName | |
Start-VM -Name $DCVMName | |
Write-Verbose "Waiting for PowerShell Direct to start on VM [$DCVMName]" -Verbose | |
Write-Verbose "PowerShell Direct responding on VM [$DCVMName]. Moving On..." -Verbose | |
Write-Verbose "DC Provisioning Complete!!!!" -Verbose | |
Write-Verbose "Creating new Administrative User within Domain [$DomainName] That will have access to Share [$ShareName] on VM [$FSVMName]" -Verbose | |
Invoke-Command $DCVMName -Credential $DomainCredential -ScriptBlock { | |
param ($NewAdminUserName, $NewAdminUserPWord) | |
Write-Verbose "Waiting for AD Web Services to be in a running state" -Verbose | |
$ADWebSvc = Get-Service ADWS | Select-Object * | |
while($ADWebSvc.Status -ne 'Running') | |
{ | |
Start-Sleep -Seconds 1 | |
} | |
Do { | |
Start-Sleep -Seconds 30 | |
Write-Verbose "Waiting for AD to be Ready for User Creation" -Verbose | |
New-ADUser -Name "$NewAdminUserName" -AccountPassword $NewAdminUserPWord | |
Enable-ADAccount -Identity "$NewAdminUserName" | |
$ADReadyCheck = Get-ADUser -Identity $NewAdminUserName | |
} | |
Until ($ADReadyCheck.Enabled -eq "True") | |
Add-ADGroupMember -Identity "Domain Admins" -Members "$NewAdminUserName" | |
} -ArgumentList $NewAdminUserName, $NewAdminUserPWord | |
Write-Verbose "User [$NewAdminUserName] Created." -Verbose | |
# The below section is used to Provision a new file server VM, add it to the new domain, and configure a basic share. | |
# First we make a copy of the sysprepped "Gold Image" VHDX file. Also, note that a Unattend.XML file has been placed within the image as well. | |
Write-Verbose "Copying Master VHDX and Deploying new VM with name [$FSVMName]" -Verbose | |
Copy-Item "$VHDXPath\Master.vhdx" "$VHDXPath\$FSVMNAME.vhdx" | |
Write-Verbose "VHDX Copied, Building VM...." -Verbose | |
New-VM -Name $FSVMName -MemoryStartupBytes 1GB -VHDPath "$VHDXPath\$FSVMName.vhdx" -Generation 2 -SwitchName $vSwitch | |
Write-Verbose "VM Creation Completed. Starting VM [$FSVMName]" -Verbose | |
Start-VM -Name $FSVMName | |
# After the inital provisioning, we wait until the PowerShell Direct is functional and working within the guest VM before moving on. | |
# Big thanks to Ben Armstrong for the below useful Wait code | |
Write-Verbose “Waiting for PowerShell Direct to start on VM [$FSVMName]” -Verbose | |
Write-Verbose "PowerShell Direct responding on VM [$FSVMName]. Moving On...." -Verbose | |
# Next we configure the networking for the new FS VM. | |
# NOTE: that the host variables are passed through by makinguse of the param command along with the -ArgumentList Paramater at the end of | |
# the ScriptBlock. | |
# NOTE: The InterfaceAlias value may be different for your gold image, so adjust accordingly. | |
Invoke-Command -VMName $FSVMName -Credential $FSLocalCredential -ScriptBlock { | |
param ($FSVMName, $FSIP, $SubMaskBit, $DFGW, $DCVMName, $DCIP) | |
New-NetIPAddress -IPAddress "$FSIP" -InterfaceAlias "Ethernet 2" -PrefixLength "$SubMaskBit" | Out-Null | |
$FSEffectiveIP = Get-NetIPAddress -InterfaceAlias "Ethernet 2" | Select-Object IPAddress | |
Write-Verbose "Assigned IPv4 and IPv6 IPs for VM [$FSVMName] are as follows" -Verbose | |
Write-Host $FSEffectiveIP | Format-List | |
Write-Verbose "Setting DNS Source to [$DCVMName] with IP [$DCIP]" -Verbose | |
Set-DnsClientServerAddress -InterfaceAlias "Ethernet 2" -ServerAddresses "$DCIP" | |
Write-Verbose "Updating Hostname for VM [$FSVMName]" -Verbose | |
Rename-Computer -NewName "$FSVMName" | |
} -ArgumentList $FSVMName, $FSIP, $SubMaskBit, $DFGW, $DCVMName, $DCIP | |
Write-Verbose "Rebooting VM [$FSVMName] for hostname change to take effect" -Verbose | |
Stop-VM -Name $FSVMName | |
Start-VM -Name $FSVMName | |
Write-Verbose “Waiting for PowerShell Direct to start on VM [$FSVMName]” -Verbose | |
Write-Verbose "PowerShell Direct responding on VM [$FSVMName]. Moving On...." -Verbose | |
# The below Adds the File Server VM to the newly Created Domain. | |
Write-Verbose "Adding VM [$FSVMName] to domain [$DomainName]" -Verbose | |
Invoke-Command -VMName $FSVMName -Credential $FSLocalCredential -ScriptBlock { | |
param ($DomainName, $DomainCredential) | |
Add-Computer -DomainName $DomainName -Credential $DomainCredential | |
} -ArgumentList $DomainName, $DomainCredential | |
Write-Verbose "Initiating Reboot of VM [$FSVMName] to complete domain join to domain [$DomainName]" -Verbose | |
Stop-VM -Name $FSVMName | |
Start-VM -Name $FSVMName | |
Write-Verbose “Waiting for PowerShell Direct to start on VM [$FSVMName]” -Verbose | |
Write-Verbose "PowerShell Direct responding on VM [$FSVMName]. Moving On...." -Verbose | |
# Now we install the File Server Role and Create the Share | |
Write-Verbose "Installing File-Server Role on VM [$FSVMName]." -Verbose | |
Invoke-Command -VMName $FSVMName -Credential $DomainCredential -ScriptBlock { | |
param ($SharePath, $FolderName, $ShareName, $DomainName, $NewAdminUserName) | |
Install-WindowsFeature -Name "FS-FileServer" -IncludeManagementTools | |
Write-Verbose "Creating File Share [$ShareName] at path [$SharePath\$Foldername]." -Verbose | |
New-Item -Path $SharePath -Name $FolderName -ItemType "Directory"; | |
New-SmbShare -Name "$ShareName" -Path "$SharePath\$FolderName" -FullAccess "$DomainName\$NewAdminUserName" | |
} -ArgumentList $SharePath, $FolderName, $ShareName, $DomainName, $NewAdminUserName | |
Write-Verbose "Environment Setup Complete. End of Script" -Verbose | |
# END OF SCRIPT |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment