Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Get Your public IP address (hat tip https://gallery.technet.microsoft.com/scriptcenter/Get-ExternalPublic-IP-c1b601bb)
- $ip = Invoke-RestMethod http://ipinfo.io/json | Select -exp ip
- $dest = "\\su1fileserver\SU1_ManagementLibrary_1"
- $ResourceGroup = "AzureStackRG"
- #Set Target VM credential
- $credential = Get-Credential
- #Create the parameter objects
- $parameterObj = @{
- adminUserName = $credential.UserName;
- adminPassword = $credential.Password;
- dnsNameForPublicIP = "<changeme>";
- SourceIPAddress = $ip.ToString()
- }
- #Create a new azure deployment
- New-AzureRmResourceGroup -Name $ResourceGroup -Location "eastus"
- #You will be prompted for an admin password
- $deploy = New-AzureRmResourceGroupDeployment -Name "AzureStackVM" -ResourceGroupName $ResourceGroup `
- -TemplateParameterObject $parameterObj `
- -TemplateFile "https://raw.githubusercontent.com/ned1313/AzureStackVM/master/AzureStackVM/WindowsVirtualMachine.json"
- #Sometimes public IP doesn't output, so you'll need to run the deployment again.
- #Enable WinRM connectivity
- $pip = $deploy.Outputs["publicIPAddress"].Value
- $curValue = (get-item wsman:\localhost\Client\TrustedHosts).value
- if($curValue){
- set-item wsman:\localhost\Client\TrustedHosts -value "$curValue, $pip" -Force
- }else{
- set-item wsman:\localhost\Client\TrustedHosts -value "$pip" -Force
- }
- #Create Remote Session and run sysprep
- $sess = New-PSSession -ComputerName $pip -Credential $credential
- Invoke-Command -Session $sess -ScriptBlock {C:\Windows\system32\sysprep\sysprep.exe /generalize /oobe /shutdown}
- do{
- $vm = get-azurermvm -ResourceGroupName $ResourceGroup -Name MyWindowsVM -Status
- $status = $vm.Statuses | ?{$_.Code -eq "PowerState/running"}
- Wait-Event -Timeout 5
- Write-Output "VM Still Running"
- }while($status -ne $null)
- #Deallocate VM
- Stop-AzureRmVM -Name MyWindowsVM -ResourceGroupName $ResourceGroup -Force
- #Copy the VHD file locally
- $vm = get-azurermvm -ResourceGroupName $ResourceGroup -Name MyWindowsVM
- $vhdinfo = $vm.StorageProfile.OsDisk.Vhd.Uri.Split("/")
- $sa = $vhdinfo[2].Split(".")[0]
- $key = Get-AzureRmStorageAccountKey -ResourceGroupName $ResourceGroup -Name $sa
- $ctx = New-AzureStorageContext -StorageAccountName $sa -StorageAccountKey $key.Key1
- Get-AzureStorageBlobContent -Blob $vhdinfo[4] -Destination $dest -Container $vhdinfo[3] -Context $ctx
- Import-Module C:\AzureStack-Tools-master\Connect\AzureStack.Connect.psm1
- Import-Module C:\AzureStack-Tools-master\ComputeAdmin\AzureStack.ComputeAdmin.psm1
- $AzureStackCreds = Get-Credential
- $AzureStackADTenant = $AzureStackCreds.UserName.Split("@")[1]
- $AadTenant = Get-AADTenantGUID -AADTenantName $AzureStackADTenant
- Add-VMImage -publisher MicrosoftWindowsServer -offer WindowsServer -sku 2012-R2-Datacenter `
- -version 1.0.0 -osDiskLocalPath "$dest\osdiskforwindowssimple.vhd" -osType Windows `
- -tenantID $AadTenant -location local -EnvironmentName AzureStack `
- -title "Windows Server 2012 R2 Datacenter" -Verbose
Add Comment
Please, Sign In to add comment