function Export-AzureVMList { [cmdletbinding()] param( [switch]$CSVReport ) BEGIN{ try { Write-Host "Importing Azure PowerShell Module." -ForegroundColor Magenta Import-Module -Name 'Azure' -ErrorAction Stop Write-Host "Getting the list of all Azure VM" -ForegroundColor Magenta $VmList = Get-AzureVM -ErrorAction Stop $Subs = Get-AzureSubscription | Where-Object IsDefault -eq $true } catch { Write-Host "Exception Occured : $_.Exception" } } PROCESS{ if ( $CSVReport.IsPresent -eq $true ) { Write-Output "Exporting to CSV" $body1= "Name,DNSName,InstanceSize,PowerState,RDPPort,RDPPortDetails" #New Line $body1 += "`n" Write-Output "Processing the downloaded data." foreach ( $item in $VmList ) { $EndPoint = $item | Get-AzureEndpoint | Where-Object Name -eq "Remote Desktop" foreach($point in $EndPoint) { $body1 += $item.Name + "," + $item.DNSName ` + "," + $item.InstanceStatus ` + "," + $item.PowerState + "," + $point.Port ` + "," + $item.Dnsname.Replace("http://","rdp://") + ":" + $point.Port + "`n" } } Write-Output "Exporting file to : $env:temp\AzureVmList.csv" $body1 > "$env:temp\AzureVmList.csv" Write-Output "Opening the Exported CSV file." Invoke-Expression "$env:temp\AzureVmList.csv" } else { #Java Script for Hyperlink $jvScript = @() $jvScript = " var destination=(WScript.Arguments(0)) var search='rdp://' var rdpexe='C:\\WINDOWS\\system32\\mstsc.exe' //WScript.Echo(destination) destination=destination.replace(search, '') destination=destination.replace('/', '') var ws = new ActiveXObject('WScript.Shell') //WScript.Echo(rdpexe + ' /v:' + destination) ws.Exec(rdpexe + ' /v:' + destination)" $jvScript | Out-File -FilePath $env:SystemRoot\RDP.js -ErrorAction SilentlyContinue #Add Registry Item $regFile = @() $regFile += "Windows Registry Editor Version 5.00" $regFile += "[HKEY_CLASSES_ROOT\rdp]" $regFile += '@="URL:Remote Desktop Connection"' $regFile += '"URL Protocol"=""' $regFile += "[HKEY_CLASSES_ROOT\rdp\DefaultIcon]" $regFile += '@="C:\\WINDOWS\\System32\\mstsc.exe"' $regFile += "[HKEY_CLASSES_ROOT\rdp\shell]" $regFile += "[HKEY_CLASSES_ROOT\rdp\shell\open]" $regFile += "[HKEY_CLASSES_ROOT\rdp\shell\open\command]" $regFile += '@="wscript.exe C:\\WINDOWS\\rdp.js %1"' $regFile | Out-File $env:SystemRoot\RDP.reg -ErrorAction SilentlyContinue Start-Process -FilePath "$env:SystemRoot\system32\cmd.exe" -WindowStyle Minimized ` -ArgumentList @('/C REG IMPORT "' + "$env:Systemroot\RDP.reg" + '"') -Wait $body1 = @() $i = 1 Write-Output "Processing the downloaded data." foreach ( $item in $VmList ) { $EndPoint = $item | Get-AzureEndpoint | Where-Object Name -eq "Remote Desktop" foreach($point in $EndPoint) { $counter = $i++ $body1 += "" $body1 += "" + $counter + "" $body1 += "" +"" + $item.Name + "" + "" $body1 += "" + "" + "" + $($item.DNSName) + "" + "" + "" $body1 += "" + "" + $item.InstanceSize + "" + "" if($item.PowerState -eq "Started") { $body1 += "" + "" + $item.PowerState + "" + "" } else { $body1 += "" + "" + $item.PowerState + "" + "" } $body1 += "" + "" + $point.Port + "" + "" $body1 += "" + "" + "" + $item.Name.ToUpper() ` + "" + "" + "" $body1 += "" } } $body2 = "

List of Azure VM

" $body2 += "
" $body2 += "

SubscriptionName : $($subs.SubscriptionName)

" $body2 += "
" $body2 +="

Page generated at $(get-date) on machine $env:computername .

" $body2 += "" $body2 += "" $body2 += "" $body2 += "" $body2 += "" $body2 += "" $body2 += "" $body2 += "" $body2 += "" $body2 += "" $body2 += $body1 $body2 += "
SerialNumber Name DNS Name Instance Size PowerState RDP Port Details RDP Port Link
" Write-Host "Generating HTML file." -ForegroundColor Magenta $body2 > "$env:TEMP\AzureList.html" Write-Host "Opening the generated HTML file to : $env:TEMP\AzureList.html ." -ForegroundColor Magenta Invoke-Expression "$env:TEMP\AzureList.html" } } END{ Write-Host "Script run finishes at : $(get-date)" } }