Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Requires -RunAsAdministrator
- <#
- .Synopsis
- Creates a new Hyper-V instance from a named template.
- .DESCRIPTION
- Imports an existing Hyper-V export into a new instance, set-up the local client host/ip resoltution and generate bootstrapping powershell script.
- .PARAMETER OSVersion
- The code name for operating system
- .PARAMETER VMName
- The name of the new instance
- #>
- param(
- [Parameter(Mandatory=$true)]
- [string]$OSVersion,
- [Parameter(Mandatory=$true)]
- [string]$VMName
- )
- $rootPath="C:\Users\Public\Documents\Hyper-V"
- $exportPath=Join-Path $rootPath "Exports"
- $vmNameToImport="Template-$OSVersion"
- $importPath=Join-Path $exportPath $vmNameToImport
- $importPath=Join-Path $importPath "Virtual Machines"
- $vmcxPathToImport=Get-ChildItem $importPath -Filter "*.vmcx" |Select-Object -ExpandProperty FullName -First 1
- if(Get-VM -Name $VMName -ErrorAction SilentlyContinue)
- {
- throw "VM with name $VMName already exists"
- }
- Import-VM -Path $vmcxPathToImport -Copy -GenerateNewId
- Rename-VM $vmNameToImport –NewName $VMName
- Get-VMHardDiskDrive -VMName $VMName |ForEach-Object {
- $vhd=$_
- $newPath = Join-Path (Split-Path $vhd.Path -Parent) "$vmName.vhdx"
- Rename-Item $vhd.Path $newPath
- Set-VMHardDiskDrive -VMName $vhd.VMName -Path $newPath –ControllerType $vhd.ControllerType –ControllerNumber $vhd.ControllerNumber -ControllerLocation $vhd.ControllerLocation
- }
- $null=Start-VM -Name $VMName
- while((Get-VM -Name $VMName).NetworkAdapters.IPAddresses.Count -eq 0)
- {
- }
- $vmIpAddress = (Get-VM -Name $VMName).NetworkAdapters.IPAddresses[0]
- Add-HostEntry -Name $VMName -Address $vmIpAddress -Force
- $trustedHostsItem=Get-Item WSMan:\localhost\Client\TrustedHosts | Select-Object -ExpandProperty Value
- if($trustedHostsItem.Length -gt 0)
- {
- $trustedHosts=$trustedHostsItem -split ','
- }
- else
- {
- $trustedHosts=@()
- }
- if($trustedHosts -notcontains $VMName)
- {
- $trustedHosts+=$vmName
- Set-Item WSMan:\localhost\Client\TrustedHosts -Value ($trustedHosts -join ',') -Force
- }
- $vmAdministratorCredentials=New-HyperVMAdministratorCredential
- $vmAdministratorPassword=$vmAdministratorCredentials |Show-Credential |Select-Object -ExpandProperty Password
- $netArgs=@(
- "use"
- "\\$vmName"
- "/user:.\$($vmAdministratorCredentials.Username)"
- "$vmAdministratorPassword"
- )
- Start-Process -FilePath "net" -ArgumentList $netArgs -Wait -NoNewWindow
- $initCommands= @(
- "#Set execution policy to unrestricted"
- "Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Force"
- "#Set timezone"
- "Set-TimeZone -Name 'Central European Standard Time'"
- "#Enable the num lock"
- "Set-ItemProperty -Path 'HKCU:\Control Panel\Keyboard' -Name InitialKeyboardIndicators -Value 2"
- "#Enable powershell remoting"
- "Enable-PSRemoting -Force"
- "#Rename the computer"
- "Rename-Computer -NewName $VMName -Force"
- "#Enable file sharing"
- "Get-NetFirewallRule -DisplayGroup 'File and Printer Sharing*'|Enable-NetFirewallRule"
- "#Restart"
- "Restart-Computer -Force"
- "#Finish"
- )
- $initCmd=$initCommands -join [System.Environment]::NewLine
- Write-Host "From withing the Virtual OS, execute the following Powershell script."
- Write-Host $initCmd
Add Comment
Please, Sign In to add comment