This article is more than 1 year old

Want to deploy virtual machines in a hurry? PowerShell is your friend

Improve your automation

Step 3: customisation

So far we have copied our .VHDX file, added a new virtual machine to the Hyper-V management console and assigned it CPU, RAM and network resources. Now that we have a powered-on virtual machine we can start customising it.

There are four clear steps to take with our post-deployment script.

1. Set the virtual machine to use a static IP. Alternatively, we could leave the virtual machine using DHCP, but I much prefer to have all of my virtual machines on static IPs.

2. Supply credentials to the virtual machine so that PowerShell remoting will work. To save having to re-enter them every time they are required we will store them as a variable.

3. Use the Add-Computer cmdlet to join the server to the domain and set its computer name.

4. Use the Install-WindowsFeature cmdlet to install any Windows Server features not already preinstalled.

I have tried repeatedly to make a script that does all of these tasks remotely. Frankly, if you can get it working, then kudos to you. Unfortunately for the time being initiating this step in my lab requires me to boot the virtual machine, log in with the administrator account and then run the script.

Our script to achieve these goals looks like this:

# Intialise your Parameters Param(      [Parameter(Mandatory=$true)][String]$NewComputerName, [Parameter(Mandatory=$true)][String]$vSwitchName, [Parameter(mandatory=$true)][string]$IPAddress,            [Parameter(mandatory=$false)][string]$Subnetmask, [Parameter(mandatory=$true)][string]$DefaultGateway,      [Parameter(mandatory=$true)][array]$DNSservers      #Add additional params here )

# Collect Required Credentials Write-Verbose "Get the credentials for the local computer and the domain" $LocalCred=Get-Credential -UserName Administrator -Message "Local computer credentials" $DomainCred=Get-Credential -USerName Administrator@labrat.wigginsix.com -Message "Domain credentials" # Assign The Specified Static IP to the VM Write-Verbose "Assign a Static (non-DHCP assigned) IP address" Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "IPenabled=TRUE" | % {                Write-host "Trying to set IP Address on $($_.Description)"     $_.EnableStatic($IPAddress,$Subnetmask) | out-null     $_.SetGateways($DefaultGateway)  | out-null     $_.SetDNSServerSearchOrder($DNSServers)  | out-null }            if($?) {      write-host "IP Address set successfully" } else {

write-host "Static IP assignment failed" } # Join The VM to the Domain, Change the Computer Name and Restart Write-Verbose "VM $NewComputerName Join the Domain" Rename-Computer -NewName $NewComputerName sleep 5 Add-Computer -DomainName labrat.wigginsix.com -Credential $DomainCred -Options JoinWithNewName -restart # According to M$ documentation this statement is supposed work. Leaving this here for future reference. PS: It DOES NOT work # Add-Computer -DomainName labrat.wigginsix.com -ComputerName $env:computername -newname $NewComputerName -Credential  -Restart

So there you have it: two working scripts that deploy and then configure our virtual machines; and a working script that you can use to rapidly deploy and customise a virtual machine.  

Next page: Deploying to Azure

More about

TIP US OFF

Send us news


Other stories you might like