Guest User

Untitled

a guest
Oct 11th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. #Requires -RunAsAdministrator
  2.  
  3. <#
  4. .Synopsis
  5. Creates a new Hyper-V instance from a named template.
  6. .DESCRIPTION
  7. Imports an existing Hyper-V export into a new instance, set-up the local client host/ip resoltution and generate bootstrapping powershell script.
  8. .PARAMETER OSVersion
  9. The code name for operating system
  10. .PARAMETER VMName
  11. The name of the new instance
  12. #>
  13. param(
  14. [Parameter(Mandatory=$true)]
  15. [string]$OSVersion,
  16. [Parameter(Mandatory=$true)]
  17. [string]$VMName
  18. )
  19. $rootPath="C:\Users\Public\Documents\Hyper-V"
  20. $exportPath=Join-Path $rootPath "Exports"
  21.  
  22. $vmNameToImport="Template-$OSVersion"
  23. $importPath=Join-Path $exportPath $vmNameToImport
  24. $importPath=Join-Path $importPath "Virtual Machines"
  25. $vmcxPathToImport=Get-ChildItem $importPath -Filter "*.vmcx" |Select-Object -ExpandProperty FullName -First 1
  26.  
  27. if(Get-VM -Name $VMName -ErrorAction SilentlyContinue)
  28. {
  29. throw "VM with name $VMName already exists"
  30. }
  31.  
  32. Import-VM -Path $vmcxPathToImport -Copy -GenerateNewId
  33.  
  34. Rename-VM $vmNameToImport –NewName $VMName
  35. Get-VMHardDiskDrive -VMName $VMName |ForEach-Object {
  36. $vhd=$_
  37. $newPath = Join-Path (Split-Path $vhd.Path -Parent) "$vmName.vhdx"
  38.  
  39. Rename-Item $vhd.Path $newPath
  40. Set-VMHardDiskDrive -VMName $vhd.VMName -Path $newPath –ControllerType $vhd.ControllerType –ControllerNumber $vhd.ControllerNumber -ControllerLocation $vhd.ControllerLocation
  41. }
  42.  
  43.  
  44. $null=Start-VM -Name $VMName
  45.  
  46. while((Get-VM -Name $VMName).NetworkAdapters.IPAddresses.Count -eq 0)
  47. {
  48.  
  49. }
  50. $vmIpAddress = (Get-VM -Name $VMName).NetworkAdapters.IPAddresses[0]
  51.  
  52. Add-HostEntry -Name $VMName -Address $vmIpAddress -Force
  53.  
  54. $trustedHostsItem=Get-Item WSMan:\localhost\Client\TrustedHosts | Select-Object -ExpandProperty Value
  55. if($trustedHostsItem.Length -gt 0)
  56. {
  57. $trustedHosts=$trustedHostsItem -split ','
  58. }
  59. else
  60. {
  61. $trustedHosts=@()
  62. }
  63.  
  64. if($trustedHosts -notcontains $VMName)
  65. {
  66. $trustedHosts+=$vmName
  67. Set-Item WSMan:\localhost\Client\TrustedHosts -Value ($trustedHosts -join ',') -Force
  68. }
  69.  
  70. $vmAdministratorCredentials=New-HyperVMAdministratorCredential
  71. $vmAdministratorPassword=$vmAdministratorCredentials |Show-Credential |Select-Object -ExpandProperty Password
  72. $netArgs=@(
  73. "use"
  74. "\\$vmName"
  75. "/user:.\$($vmAdministratorCredentials.Username)"
  76. "$vmAdministratorPassword"
  77. )
  78.  
  79. Start-Process -FilePath "net" -ArgumentList $netArgs -Wait -NoNewWindow
  80.  
  81. $initCommands= @(
  82. "#Set execution policy to unrestricted"
  83. "Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Force"
  84. "#Set timezone"
  85. "Set-TimeZone -Name 'Central European Standard Time'"
  86. "#Enable the num lock"
  87. "Set-ItemProperty -Path 'HKCU:\Control Panel\Keyboard' -Name InitialKeyboardIndicators -Value 2"
  88. "#Enable powershell remoting"
  89. "Enable-PSRemoting -Force"
  90. "#Rename the computer"
  91. "Rename-Computer -NewName $VMName -Force"
  92. "#Enable file sharing"
  93. "Get-NetFirewallRule -DisplayGroup 'File and Printer Sharing*'|Enable-NetFirewallRule"
  94. "#Restart"
  95. "Restart-Computer -Force"
  96. "#Finish"
  97. )
  98.  
  99. $initCmd=$initCommands -join [System.Environment]::NewLine
  100.  
  101. Write-Host "From withing the Virtual OS, execute the following Powershell script."
  102. Write-Host $initCmd
Add Comment
Please, Sign In to add comment