My code below gets all the VMs that are deallocated, but when I try to output $result in my last line, $return returns null.. where is my code wrong, I cant seem to figure it out:
Foreach ($SubscriptionId in $SubscriptionIds)
{
Select-AzSubscription $subscriptionId
Get-AzSubscription -SubscriptionName $SubscriptionId.Name | Set-AzContext
$vms = Get-Azvm -Status
$nics = Get-AzNetworkInterface | ?{ $_.VirtualMachine -NE $null}
$report = @()
foreach ($nic in $nics)
{
$ReportDetails = "" | Select SubName, OsType, VmName,Hostname, Powerstate
$vm = $vms | ? -Property Id -eq $nic.VirtualMachine.id
if (!($vm.Powerstate -eq "VM running")){
$ReportDetails.Hostname= (get-azvm $vm.Name -ResourceGroupName $vm.ResourceGroupName).OSProfile.ComputerName
$ReportDetails.SubName = (Get-AzContext).Subscription.Name
$ReportDetails.OsType = $vm.StorageProfile.OsDisk.OsType
$ReportDetails.VMName = $vm.Name
$ReportDetails.Powerstate = $vm.Powerstate
$report+=$ReportDetails
}
}
Write-Host "Total Numbers of non-running VMs in"(Get-AzContext).Subscription.Name"is"$report.Count"" -ForegroundColor Green
}
$report | ft - properties * - Autosize