magritton
4/4/2015 - 11:53 AM

My Azure VM powershell module for managing VMs

My Azure VM powershell module for managing VMs

function Stop-AZVM($azurecloud,$vmname) 
{ 
    if ($vmname -eq $null) 
    { 
        Get-AzureVM -ServiceName $azurecloud |Foreach-object {Stop-AzureVM -ServiceName $_.ServiceName -Name $_.Name -Force} 
    } 
    else 
    { 
        Stop-AzureVM -ServiceName $azurecloud -Name $vmname 
    } 
}

function Stop-AZSingleVM() 
{ 
    Param(
     [Parameter(Mandatory=$True,Position=1)]
     [string]$VMName
)
    $azuresettings = "C:\Azure\AzureseetingsFile" 
    $azurecloud = "sqltestlab-corpnet"
    Import-AzurePublishSettingsFile $azuresettings

    Stop-AzureVM -ServiceName $azurecloud -Name $VMName 
}

function Start-AZVM($azurecloud,$vmname) 
{ 
    if ($vmname -eq $null) 
    { 
        Get-AzureVM -ServiceName $azurecloud |Foreach-object {Start-AzureVM -ServiceName $_.ServiceName -Name $_.Name -Force} 
    } 
    else 
    { 
        Start-AzureVM -ServiceName $azurecloud -Name $vmname 
    } 
}

function Start-AZSingleVM() 
{ 
    Param(
     [Parameter(Mandatory=$True,Position=1)]
     [string]$VMName
    )
    
    $azuresettings = "C:\Azure\AzureseetingsFile" 
    $azurecloud = "sqltestlab-corpnet"
    Import-AzurePublishSettingsFile $azuresettings

    Start-AzureVM -ServiceName $azurecloud -Name $VMName

}

function Add-AZSQLTestVM() 
{ 
    Param(
     [Parameter(Mandatory=$True,Position=1)]
     [string]$VMName
    )
    
    
    $azuresettings = "C:\Azure\AzureseetingsFile" 
    $azurecloud = "sqltestlab-corpnet" 

    write-host "Importing Azure Settings" 
    Import-AzurePublishSettingsFile $azuresettings

    #had to do this for the storeage account
    Set-AzureSubscription –SubscriptionName "Windows Azure  MSDN - Visual Studio Premium" –CurrentStorageAccount "sqltestlab"

    $vm = Get-AzureVM -ServiceName $azurecloud -Name 'DC1'
    if($vm.PowerState -eq 'Stopped')
        {Start-AzureVM -ServiceName $azurecloud -Name 'DC1'}

    New-AzureVMConfig -Name $VMName -InstanceSize "Medium" -ImageName "STDImage" | Add-AzureProvisioningConfig -WindowsDomain -AdminUsername "MikeNet" –Password "CatDog12" -JoinDomain "corp.contoso.com" -Domain "corp" -DomainUserName "Mike" -DomainPassword "CatDog12"  | New-AzureVM -ServiceName "sqltestlab-corpnet"
}

function Assert-AZVMControl
{
    ################################################################ 
    # Please Change These Variables to Suit Your Environment 
    # 
    $azuresettings = "C:\Azure\AzureseetingsFile" 
    $azurecloud = "sqltestlab-corpnet" 
    # 
    # 
    # 
    ################################################################

    write-host "Importing Azure Settings" 
    Import-AzurePublishSettingsFile $azuresettings

    write-host "Choose the options to Start and Stop your Azure VMS" 
    write-host "1. Start All VMs" 
    write-host "2. Stop All VMs" 
    write-host "3. Start One VM" 
    write-host "4. Stop One VM" 
    $answer = read-host "Please Select Your Choice"

    Switch($answer) 
    { 
        1{ $vmname = $null;Start-AZVM $azurecloud $vmname} 
        2{ $vmname = $null;Stop-AZVM $azurecloud $vmname} 
        3{ $vmname = read-host "Please Enter VM Name";Start-AZVM $azurecloud $vmname} 
        4{ $vmname = read-host "Please Enter VM Name";Stop-AZVM $azurecloud $vmname} 
    }
}
Import-Module C:\Users\mike.SQL01\OneDrive\PSModules\AZVMControl.psm1 -Force -Verbos