Installing AzureTools Remotely via WinRM


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


1. You have Azure Powershell Cmdlets installed and set up

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

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 

            [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
} # Function GetCredential 

# AddAzureToolsToVM downloads the AzureTools using WinRM , assumes Cert installed
# AddAzureToolsToVM –VMName "SomeVM" -UserName "bob" -Password "doofer"
Function AddAzureToolsToVM
            [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 !!!"

    # 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 !!!"

    # 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 
            Write-Host "*** $LocalAzureToolsDir already exists ***"

         # 2. Download tools 
         $AzureToolsSource = ""
         $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

    } -ArgumentList $AzureToolsDir 

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



One thought on “Installing AzureTools Remotely via WinRM

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s