Installing AzureTools Remotely via WinRM

Saw http://blogs.msdn.com/b/kwill/archive/2013/08/26/azuretools-the-diagnostic-utility-used-by-the-windows-azure-developer-support-team.aspx?Redirected=true

and so wanted to add to our standard Powershell for building VMs that does various standard config. However discovered that BITS doesnt work / isnt supported via WinRM so used alternative method as using Net.WebClient namespace as per below

Pre-Reqs

1. You have Azure Powershell Cmdlets installed and set up

http://msdn.microsoft.com/en-us/library/windowsazure/jj554332.aspx

2. Have installed Cert for WinRM as per below (InstallWinRMCert.ps1) for the VMs you want to update

http://michaelwasham.com/2013/04/16/windows-azure-powershell-updates-for-iaas-ga/

Do it :

Run below in Powershell which will create funcs and then call for your VM changing params below

AddAzureToolsToVM –VMName SomeVM -UserName “bob” -Password “doofus”

# Returns a PSCredential, ie for use with WinRM etc
Function GetCredential 
{
        Param 
        (

            [Parameter(Mandatory=$true)] [string]$UserName,
            [Parameter(Mandatory=$true)] [string]$Password
        )

            $pass = ConvertTo-SecureString -AsPlainText $Password -Force
            $Credential = New-Object System.Management.Automation.PSCredential -ArgumentList $UserName , $pass
            $Credential
} # Function GetCredential 


# AddAzureToolsToVM downloads the AzureTools using WinRM , assumes Cert installed
# AddAzureToolsToVM –VMName "SomeVM" -UserName "bob" -Password "doofer"
Function AddAzureToolsToVM
{
    Param 
        (
            [Parameter(Mandatory=$true)] [string]$VMName,
            [Parameter(Mandatory=$false)] [string]$VMServiceName = $VMName,
            [Parameter(Mandatory=$true)] [string]$UserName,
            [Parameter(Mandatory=$true)] [string]$Password, 
            [Parameter(Mandatory=$false)] [string]$AzureToolsDir = "c:\install\Azuretools" 
        )
 
    Write-Host "*** Getting VM = $VMName ***"
    $VM = Get-AzureVM -Name $VMName -ServiceName $VMName
     If($VM -eq $Null)
            {
               Write-Host "!!! Error Getting VM = $VMName , quitting !!!"
               Break 
            }

    
    # Uses GetCredential to set Credential object
    $Credential = GetCredential -UserName $UserName  -Password $Password
    $uri = Get-AzureWinRMUri -ServiceName $VMName -Name $VMName
    Write-Host "*** Connecting to $VMName, running hostname.exe to test WinRM ***"
    
    # Quick test to prove WinRM is working
    $Results = Invoke-Command -ConnectionUri $uri.ToString() -Credential $credential -ScriptBlock {HostName}
    If($Results -eq $Null)
        {
             Write-Host "!!! Error Running Hostname.,exe on VM = $VMName , quitting !!!"
             Break
        }

    
    # Main doing part , output downloaded file object into $WinRMResults
    $WinRMResults = Invoke-Command -ConnectionUri $uri.ToString() -Credential $credential -ScriptBlock {
        # assign passed in arguments to friendly variable names
        $LocalAzureToolsDir = $args[0]
        
        # 1. Create Dir
        If (!(test-path $LocalAzureToolsDir))
         {
             Write-Host "*** Creating directory $LocalAzureToolsDir ***"
             mkdir $LocalAzureToolsDir 
         }
        Else
         {
            Write-Host "*** $LocalAzureToolsDir already exists ***"
         }

         # 2. Download tools 
         $AzureToolsSource = "http://dsazure.blob.core.windows.net/azuretools/AzureTools.exe"
         $AzureToolsFullPath = $LocalAzureToolsDir + "\AzureTools.exe"

         Write-Host "*** Creating Object Net.WebClient ***"
         $WebClient = New-Object Net.WebClient 
         Write-Host "*** Starting download of $AzureToolsSource to $AzureToolsFullPath ***"
         $WebClient.DownloadFile($AzureToolsSource, $AzureToolsFullPath)
         
        
         # Finally get and return File object 
         $DownloadedFile = Get-Item $AzureToolsFullPath
         $DownloadedFile 

    } -ArgumentList $AzureToolsDir 


    
     If ($WinRMResults -eq $Null)
            {
                Write-Error "!!! $AzureToolsDir\AzureTools.exe not found !!!" 
            }
        Else
            {
                 # If Works downloaded file details
                $WinRMResults 
            }

}

Advertisements

One thought on “Installing AzureTools Remotely via WinRM

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s