ned1313

Copy Server 2012 VHD

Mar 31st, 2017
884
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Get Your public IP address (hat tip https://gallery.technet.microsoft.com/scriptcenter/Get-ExternalPublic-IP-c1b601bb)
  2. $ip = Invoke-RestMethod http://ipinfo.io/json | Select -exp ip
  3. $dest = "\\su1fileserver\SU1_ManagementLibrary_1"
  4. $ResourceGroup = "AzureStackRG"
  5.  
  6. #Set Target VM credential
  7. $credential = Get-Credential
  8.  
  9. #Create the parameter objects
  10. $parameterObj = @{
  11.      adminUserName = $credential.UserName;
  12.      adminPassword = $credential.Password;
  13.      dnsNameForPublicIP = "<changeme>";
  14.      SourceIPAddress = $ip.ToString()
  15. }
  16.  
  17. #Create a new azure deployment
  18. New-AzureRmResourceGroup -Name $ResourceGroup -Location "eastus"
  19.  
  20. #You will be prompted for an admin password
  21. $deploy = New-AzureRmResourceGroupDeployment -Name "AzureStackVM" -ResourceGroupName $ResourceGroup `
  22.  -TemplateParameterObject $parameterObj `
  23.  -TemplateFile "https://raw.githubusercontent.com/ned1313/AzureStackVM/master/AzureStackVM/WindowsVirtualMachine.json"
  24.  
  25. #Sometimes public IP doesn't output, so you'll need to run the deployment again.
  26.  
  27. #Enable WinRM connectivity
  28. $pip = $deploy.Outputs["publicIPAddress"].Value
  29. $curValue = (get-item wsman:\localhost\Client\TrustedHosts).value
  30. if($curValue){
  31.     set-item wsman:\localhost\Client\TrustedHosts -value "$curValue, $pip" -Force
  32. }else{
  33.     set-item wsman:\localhost\Client\TrustedHosts -value "$pip" -Force
  34. }
  35.  
  36. #Create Remote Session and run sysprep
  37. $sess = New-PSSession -ComputerName $pip -Credential $credential
  38. Invoke-Command -Session $sess -ScriptBlock {C:\Windows\system32\sysprep\sysprep.exe /generalize /oobe /shutdown}
  39. do{
  40.    $vm = get-azurermvm -ResourceGroupName $ResourceGroup -Name MyWindowsVM -Status
  41.    $status = $vm.Statuses | ?{$_.Code -eq "PowerState/running"}
  42.    Wait-Event -Timeout 5
  43.    Write-Output "VM Still Running"
  44. }while($status -ne $null)
  45.  
  46. #Deallocate VM
  47. Stop-AzureRmVM -Name MyWindowsVM -ResourceGroupName $ResourceGroup -Force
  48.  
  49. #Copy the VHD file locally
  50. $vm = get-azurermvm -ResourceGroupName $ResourceGroup -Name MyWindowsVM
  51. $vhdinfo = $vm.StorageProfile.OsDisk.Vhd.Uri.Split("/")
  52. $sa = $vhdinfo[2].Split(".")[0]
  53. $key = Get-AzureRmStorageAccountKey -ResourceGroupName $ResourceGroup -Name $sa
  54. $ctx = New-AzureStorageContext -StorageAccountName $sa -StorageAccountKey $key.Key1
  55. Get-AzureStorageBlobContent -Blob $vhdinfo[4] -Destination $dest -Container $vhdinfo[3] -Context $ctx
  56.  
  57. Import-Module C:\AzureStack-Tools-master\Connect\AzureStack.Connect.psm1
  58. Import-Module C:\AzureStack-Tools-master\ComputeAdmin\AzureStack.ComputeAdmin.psm1
  59.  
  60. $AzureStackCreds = Get-Credential
  61. $AzureStackADTenant = $AzureStackCreds.UserName.Split("@")[1]
  62. $AadTenant = Get-AADTenantGUID -AADTenantName $AzureStackADTenant
  63.  
  64. Add-VMImage -publisher MicrosoftWindowsServer -offer WindowsServer -sku 2012-R2-Datacenter `
  65.  -version 1.0.0 -osDiskLocalPath "$dest\osdiskforwindowssimple.vhd" -osType Windows `
  66.   -tenantID $AadTenant -location local -EnvironmentName AzureStack `
  67.    -title "Windows Server 2012 R2 Datacenter" -Verbose
Add Comment
Please, Sign In to add comment