Showing posts with label Powershell. Show all posts
Showing posts with label Powershell. Show all posts

Sunday, March 15, 2015

Azure VM Deployment script




  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
<#
.SYNOPSIS
This script will evalute you current version of powershell, and deploy Windows VMs with Antimalware. 
.Description

.NOTES
    File Name : DeployAzureVM
    Author    : Drew the horrid, drew@gui.us 

#>

#Import Azure Powershell
Import-Module "C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\ServiceManagement\azure\azure.psd1"

#Write installed Azure PoSh module version
Write-Host ""
Write-host "Checking Azure Powershell Version.."
Write-Host ""
Write-Host "Your Installed Azure Powershell Version is: " -NoNewline; Write-Host -ForegroundColor "Yellow" (Get-Module -Name azure).version.tostring()

#Code block to check online version
$web=(Invoke-Webrequest -Uri "http://github.com/Azure/azure-powershell/releases" -MaximumRedirection 1).links | where {$_.outertext -like "Windows Standalone"}
    Write-Host "Lastest version online is:                  " -NoNewline;Write-Host -ForegroundColor "Yellow" ($web[0].href.Split("/")[4].trim("azure-powershell.").trim(".msi"))
    Write-Host ""
    Write-Host "MSI Direct Download Link: " -NoNewline;Write-Host -ForegroundColor Yellow $web[0].href
    Write-Host "Reference: https://github.com/Azure/azure-powershell/releases"
    Write-Host ""
    
#Get Subscription info
Write-Host "Checking Subscription:";$AZsub=Get-AzureSubscription -Current
Write-Host ""

#Check if currentstorageaccount is defined
If ($AZsub.CurrentStorageAccountName -eq $null) {Write-Warning "Storage Account is not defined";Write-Host ""
    Write-Host "Please enter the target storage account name";Write-Host "Note: target storage account must be in the same region as the target cloud servicename"
    Write-Host ""
    $AZSubStorage=Read-Host "Storage account name (all lowercase) "
    Set-AzureSubscription -SubscriptionName $AZsub.SubscriptionName -CurrentStorageAccountName $AZSubStorage}
    Write-Host ""

#confirmation 
$AZsub=Get-AzureSubscription -Current
    Write-Host ""
    Write-host "Current Subscription Name: " -NoNewline; Write-Host $AZsub.SubscriptionName -ForegroundColor Yellow
    Write-host "Current Storage Account: " -NoNewline; Write-Host $AZsub.CurrentStorageAccountName -ForegroundColor Yellow
    Write-Host ""

#Get service account
Write-Host "Please enter" -NoNewline;Write-Host -ForegroundColor Yellow " Cloud Service Account Name" -NoNewline;$servicename=Read-Host " "
    Write-host ""

#Check Service Account
If ((Get-AzureService $servicename) -eq $null) {New-AzureService -Servicename $Servicename -location (Get-AzureStorageAccount $AZsub.currentstorageaccountname).Geoprimarylocation -verbose}

# Get-VM Images and filter Windows Server 2012 R2
$VMImages=Get-AzureVMImage |Where-Object {($_.PublisherName -like "Microsoft*" -and $_.ImageFamily -like 'Windows Server 2012 R2 Datacenter') }
    Write-Host "Image we will use: " -NoNewline;Write-Host $VMImages[1].Label -ForegroundColor Magenta;`
    Write-Host "Image Published Date: " -NoNewline;Write-Host $VMImages[1].PublishedDate -ForegroundColor Magenta;`
    
#Get Credentials that defined in the deployment
$Cred=Get-Credential -Message "Please enter the Username and Password for this deployment"
    Write-Host ""
    Write-Host "Default VM naming scheme is " -NoNewline;Write-Host -ForegroundColor Yellow "thing"; Write-Host "Example: thing1, thing2, thing3, etc.."
    $thingname = 'thing'; if (($result = Read-Host "Press enter to accept default computername scheme 'thingX' or enter a new one: ") -eq '') {$thingname} else {$thingname=$result}
    Write-Host ""
    [int]$ManyVMs=Read-Host "How Many VMs do you want created:  " 

#Recipe ready, cooking the request         
Write-host -ForegroundColor Yellow "cooking your request" 
Write-Host ""
Write-Host -ForegroundColor Magenta "Azure tip: local redudant storage costs less than geo replicated storage (geo is default)"
    
#Deployment engine code block
1..$ManyVMs |% {$VMName = "$thingname$_"
    New-AzureVMConfig -Name $VMName -InstanceSize "Medium" -ImageName $vmimages[1].ImageName |Add-AzureProvisioningConfig -Windows -AdminUsername $cred.UserName -Password $Cred.GetNetworkCredential().password -TimeZone "Central Standard Time" -Verbose |Add-AzureDataDisk -CreateNew -DiskSizeInGB '15' -DiskLabel "disk 1" -LUN '0' |New-AzureVM -servicename $ServiceName -verbose
        
    # Install Antimalware 
    Write-host "Installing IaaS AntiMalware on $VMName" -ForegroundColor Yellow

        # Create Json String for Azure IaaS Antimalware 
        $JsonString="{ 'AntimalwareEnabled': true }"
                
        # Deploy Antimalware    
        Get-AzureVM -Name $VMName -ServiceName $ServiceName  | Set-AzureVMExtension -Publisher Microsoft.Azure.Security -ExtensionName IaaSAntimalware -Version 1.* -PublicConfiguration $JsonString |Update-AzureVM  
        
               }

#Function to access Virtual Machines

Function Enter-AZSession ($VMRM){
 if ($VMRM.status -ne "ReadyRole") {Write-Error "VM Status is not ReadyRole";break}
else {
Write-Host Connecting to hostname: "  -ForeGroundColor Yellow -nonewline; $($VMRM.VM.RoleName)
Enter-PSSession -ConnectionUri ($VMRM|Get-AzureWinRMUri) -Credential (Get-Credential) -SessionOption (New-PSSessionOption -SkipCACheck:$true)}}
Write-Host ""
Write-host "If you would like to PS remote into your machines, enter the following"
Write-host -ForegroundColor Magenta "$VMS=Get-AzureVM" 
Write-Host "$VMS" -ForegroundColor Magenta
Write-Host "starting from the top, index number is zero '0', you can PS remote into a VM by entering the following command (0 being the first VM)" 
Write-Host -ForegroundColor Magenta 'Enter-AZSession $VMS[0]' 

Sunday, March 1, 2015

Azure Virtual Machines (VMS) Powershell Remote (WinRM), function that skips the CA validation. 


Function Enter-AZSession ($VMRM){ if ($VMRM.status -ne "ReadyRole") {Write-Error "VM Status is not ReadyRole";break} else { Write-Host “Connecting to hostname: " -ForeGroundColor Yellow -NoNewLine; $($VMRM.VM.RoleName) Enter-PSSession -ConnectionUri ($VMRM|Get-AzureWinRMUri) -Credential (Get-Credential) -SessionOption (New-PSSessionOption -SkipCACheck:$true)} }

#powershell
#azure
#iaas
#virtual machines
#remote
#powershell
#winrm
#CA
#PSSession
#VM

Saturday, September 6, 2014

Deploy 10 Virtual Machines in Azure

 

Prompts for ServiceName, also securely passes Admin/Pass to Add-AzureProvisioningConfig

Creates 10 Servers hostname: Thing#

 

<#

 

.NOTES

Author: Drew Robinson

Last Updated: 9/2/2014

#>

 

 

# Change color to yellow, prompt human for Servicename, reset color back to default

[console]::ForegroundColor = "yellow"

 

$Servicename = Read-Host -Prompt "Please Enter Servicename for this deployment: (servicename is the internet facing name, [name].cloudapp.net)"

 

[console]::ResetColor()

 

# Get Credentials (secure)

$Cred=Get-Credential -Message "Please Enter the Administrator Username and Password for the VMs, note: you can't use Admin/Administrator, password must be complex"

 

# Get-VM Images and filter Windows Server 2012 R2

$VMImages=Get-AzureVMImage |Where-Object {($_.PublisherName -ilike "Microsoft*" -and $_.ImageFamily -ilike 'Windows Server 2012 R2 Datacenter') }

 

# 1..10 = array of of 10 things, $_ = put array # here

 

1..10 |ForEach {$VMName = "thing$_"

New-AzureVMConfig -Name $VMName -InstanceSize "ExtraSmall" -ImageName $vmimages[1].ImageName |Add-AzureProvisioningConfig -Windows -AdminUsername $cred.UserName -Password $Cred.GetNetworkCredential().password -TimeZone "Central Standard Time" -Verbose |Add-AzureDataDisk -CreateNew -DiskSizeInGB '15' -DiskLabel "disk 1" -LUN '0' |New-AzureVM -location 'South Central US' -servicename $Servicename -Verbose

}

 

 

 

How to connect/bind an Azure subscription to Powershell



Here's how you  bind an Azure subscription to Powershell:


1. Click this link to download Azure Powershell

2. After you have installed Azure Powershell, login to the azure management portal 

3. Click this Link to Download you Azure Publishing file, save this file to a secure location as it has your management certificate and subscription information. 

4. In Azure Powershell, CD into the folder where the Publishing file was save.

Type the following command, Tip: you can use TAB key to complete the command

Import-AzurePublishSettingsFile .\[filename]


If you want to deploy Virtual Machines via powershell you must bind the default storage account. Type the following command

Get-AzureSubscription

If the currentstorage account is blank you'll need to define this.

Setting the Current Storage Account
1. From the Management portal, copy your Storage Account name (must be in the same region as your deployment)
2. Type the following Powershell


$AzSub = Get-AzureSubscription 
Set-AzureSubscription -SubscriptionName $AzSub.SubscriptionName -CurrentStorageAccountName [typenamehere]



Type Get-AzureSubscription

you should now see the CurrentStorageAccount is defined.