A few years back a colleague of mine asked if we could run checks against a Domino Notes Database that lists servers in the organization using PowerShell.
The Notes admin was able to export the database to a CSV file, which I imported into the PowerShell script. The first part was easy, list all servers in Active Directory and see what was there or missing compared to the CSV file.
The next part got a bit more tricky, what about workgroup servers in the DMZ? Fortunately, they were all hosted in VMware so by connecting with PowerCli we could list them and again compare them to the CSV.
This script worked well until we started moving servers into the Azure Landing Zone. Some could still be picked up in AD as they are domain-joined, but how to list the workgroup VMs?
I asked the Cloud Admin what was the best way to list all the servers, which were spread across multiple subscriptions. He said the best way was to give 'Contributor Access' to a management scope.
Once the permission was added it was quite straightforward to list all the VMs:
Connect-AzAccount -ServicePrincipal -ApplicationId $clientId -Tenant $tenantId -CertificateThumbprint $thumbprint -SubscriptionId $subId
$subs = Get-AzSubscription
foreach ($sub in $subs)
{
$s_name = $sub.name
$null = Select-AzSubscription $s_name
$vms = Get-AzVM -status
foreach ($vm in $vms)
{
$v_name = $vm.name
$v_os = $vm.OsName
$v_power = $vm.PowerState
...
$subs = Get-AzSubscription
foreach ($sub in $subs)
{
$s_name = $sub.name
$null = Select-AzSubscription $s_name
$vms = Get-AzVM -status
foreach ($vm in $vms)
{
$v_name = $vm.name
$v_os = $vm.OsName
$v_power = $vm.PowerState
...