$requestBody = Get-Content $req -Raw | ConvertFrom-Json | |
$subscriptionId = $requestBody.subscriptionid | |
if ($req_query_subscriptionid) | |
{ | |
$subscriptionId = $req_query_subscriptionid | |
} | |
if ($subscriptionId -eq $null) | |
{ | |
Throw 'Azure subscription Id not specified.' | |
exit -1 | |
} | |
$FunctionName = 'GetAzureVMs' | |
#load modules | |
#AzureRM.Profile | |
Import-module "D:\home\site\wwwroot\$FunctionName\bin\AzureRM.Profile\2.2.0\AzureRM.Profile.psd1" | |
#AzureRM.Compute | |
Import-module "D:\home\site\wwwroot\$FunctionName\bin\AzureRM.Compute\2.2.0\AzureRM.Compute.psd1" | |
#credential | |
#define AzureCredUserName and AzureCredPassword in Azure Functions App Settings | |
$username = $env:AzureCredUserName | |
$pw = $env:AzureCredPassword | |
$keypath = "D:\home\site\wwwroot\$FunctionName\bin\keys\PassEncryptKey.key" | |
$secpassword = $pw | ConvertTo-SecureString -Key (Get-Content $keypath) | |
$credential = New-Object System.Management.Automation.PSCredential ($username, $secpassword) | |
Add-AzureRMAccount -Credential $credential -WarningAction SilentlyContinue | out-null | |
#set subscriptions | |
$sub = Set-AzureRmContext -SubscriptionId $subscriptionId -ErrorAction Stop | |
$Vms = @() | |
foreach ($vm in (Get-AzureRMVm)) | |
{ | |
$vmStatus = $vm | get-AzureRMVM -status | |
$powerStatus = ($vmstatus.Statuses | where-object {$_.Code -match '^PowerState'}).code.split("/")[1] | |
$VmProperties = @{ | |
'Name' = $vm.Name | |
'ComputerName' = $vm.OSProfile.ComputerName | |
'Location' = $vm.location | |
'ResourceGroup' = $vm.ResourceGroupName | |
'AdminUserName' = $vm.OSProfile.AdminUsername | |
'Size' = $vm.HardwareProfile.VmSize | |
'ImagePublisher' = $vm.StorageProfile.ImageReference.Publisher | |
'OSType' = $vm.StorageProfile.ImageReference.Offer | |
'OSSku' = $vm.StorageProfile.ImageReference.Sku | |
'OSVersion' = $vm.StorageProfile.ImageReference.Version | |
'ProvisioningState' = $vm.ProvisioningState | |
'StatusCode' = $vm.statusCode | |
'PowerStatus' = $powerStatus | |
} | |
$VMs += New-object psobject -Property $VmProperties | |
} | |
$HTMLOutput = ($VMs | ConvertTo-Html -Title 'Azure VMs') | out-string | |
Out-file -encoding Ascii -FilePath $res -InputObject $HTMLOutput |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment