Skip to content

Instantly share code, notes, and snippets.

@pkirch
Created May 3, 2015 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pkirch/b1e816d9ddf9f3a571b4 to your computer and use it in GitHub Desktop.
Save pkirch/b1e816d9ddf9f3a571b4 to your computer and use it in GitHub Desktop.
# Microsoft Virtual Academy
# Automatisierte Konfiguration in Azure im Überblick
# 202 - http://aka.ms/mva-1505-202
#region Create VM
# Settings
$subscriptionName = "MSFT MVA Stage" # Get-AzureSubscription
$location = "West Europe" # Get-AzureLocation
$serviceName = "mvaconfig204"
$storageAccountName = $serviceName
$adminUsername = "adm_demo"
$adminPassword = "Azureisttoll!"
$imageFamily = "Windows Server 2012 R2 Datacenter"
$vmName = $serviceName
$instanceSize = "Medium" # Get-AzureRoleSize
# In case you have more than one Azure subscription, select one.
Select-AzureSubscription -SubscriptionName $subscriptionName
# Get latest image for defined image family.
# MVA03-Images.ps1 / https://gist.github.com/pkirch/058d757a799fa0087241
$imageName = Get-AzureVMImage |
Where-Object -Property ImageFamily -eq $imageFamily |
Sort-Object -Property PublishedDate -Descending |
Select-Object -ExpandProperty ImageName -First 1
# Create storage account and set is as current.
New-AzureStorageAccount -Location $location -StorageAccountName $storageAccountName -Type Standard_LRS
Set-AzureSubscription -SubscriptionName $subscriptionName -CurrentStorageAccountName $storageAccountName
# Create new VM configuration, add provisioning data to it, and start it.
New-AzureVMConfig -ImageName $imageName -InstanceSize $instanceSize -Name $vmName |
Add-AzureProvisioningConfig -Windows -AdminUsername $adminUsername -Password $adminPassword |
Add-AzureEndpoint -LocalPort 80 -Name HTTP -Protocol tcp -PublicPort 80 |
New-AzureVM -ServiceName $serviceName -Location $location -WaitForBoot
#endregion
#region Prepare connection
# Create credentials for remote session.
$secPasswd = ConvertTo-SecureString $adminPassword -AsPlainText -Force
$credentialHost = New-Object System.Management.Automation.PSCredential ($adminUsername, $secPasswd)
# Get host and port to connect to VM via PowerShell Remoting
$uriHost = Get-AzureWinRMUri -ServiceName $serviceName -Name $vmName
# Prepare and create new PSSession object.
$psso = New-PSSessionOption -SkipCACheck # Alternative way would be to download and import WinRM certificate.
$sessionHost = New-PSSession -ComputerName $uriHost.Host -Port $uriHost.Port -Credential $credentialHost -UseSSL -SessionOption $psso
#endregion
$results1 = Invoke-Command -Session $sessionHost -ScriptBlock {
Get-WindowsFeature
Get-Process
}
$results1
$results2 = Invoke-Command -Session $sessionHost -ScriptBlock {
$features = Get-WindowsFeature
$processes = Get-Process
New-Object PSObject -Property @{
Features = $features
Processes = $processes
}
}
$results2
$results2.Features
$results2.Processes
# Clean up: close connection
Remove-PSSession -Session $sessionHost
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment