#My $report variable is returning no results

1 messages ยท Page 1 of 1 (latest)

storm oriole
#

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

meager kiln
#

Without knowing how you're setting $SubscriptionIds up front it's a bit tough to be certain of your issue. I tested your code by setting it myself like this:
$SubscriptionIds = "SubID1","SubID2"

and then changed your code to be less API calls (faster):
I commented out:

# Get-AzSubscription -SubscriptionName $SubscriptionId.Name | Set-AzContext```
and replace it with
```Set-AzContext -Subscription $SubscriptionId```

Then running this block gave me results in $report as you expected:
```Foreach ($SubscriptionId in $SubscriptionIds) 
{
# Select-AzSubscription $subscriptionId
# Get-AzSubscription -SubscriptionName $SubscriptionId.Name | Set-AzContext
Set-AzContext -Subscription $SubscriptionId
$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
     }```
storm oriole
#

Full Code:

$report = @()
$SubscriptionIds = Get-AzSubscription
$SubscriptionIds.Subscription.Name
Foreach ($SubscriptionId in $SubscriptionIds)
{
Select-AzSubscription $subscriptionId
Get-AzSubscription -SubscriptionName $SubscriptionId.Name | Set-AzContext
$vms = Get-Azvm -Status
$nics = Get-AzNetworkInterface | ?{ $_.VirtualMachine -NE $null}
Write-Host "Total Numbers of VMs in"(Get-AzContext).Subscription.Name"is"$vms.Count"" -ForegroundColor Green
$report = @()
foreach ($nic in $nics)
{

$ReportDetails = "" | Select SubName, OsType, VmName,Hostname, Powerstate
$vm = $vms | ? -Property Id -eq $nic.VirtualMachine.id
#$count=$vm.Powerstate -eq "VM deallocated" -or $vm.Powerstate -eq "VM Stopped"

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 -Property * -AutoSize

storm oriole
#

Its as if $result is getting emptied, and not savings my results from each subscription, depending on my if condition

#

But $ report will save results if I hardcode a single sub

meager kiln
#

well.. you have report being defined inside your loop over your subs... so this is expected

#

if you remove it from inside the loop that will help and just keep the one you have outside the main foreach

#

line 1 is enough

storm oriole
#

ok let me try that

meager kiln
#

remove line 11

storm oriole
#

trying it out now

meager kiln
#

line 3 also doesnt do anything, if you were trying to get an output of all Subscription names inside of $subscriptionids it would just be $subscriptionids.Name (but this isnt related to your main problem)

storm oriole
#

One issue Im already seeing, if my previous result count was 0 for the non running VMs, and the next result was 1, and the following was 0, the third count is showing the result 1 from its previous count

#

which is why I put report=@() on line 11 to reset count and save new result

#

ok, I get the result I want now ๐Ÿ™‚

#

any way to fix my Write-Host "Total Numbers of non-running VMs in"(Get-AzContext).Subscription.Name"is"$report.Count"" -ForegroundColor Green

meager kiln
#

whats your end-goal with this data?

storm oriole
#

Get info on all non running VMs

meager kiln
#

ahh ok.. then we can jsut get the non-running VMs to start and ignore the running ones, right?

#

or do you need count of both?

storm oriole
#

nah, only non running count for each sub

meager kiln
#

ok then we can do that easily.

storm oriole
#

Maybe I need to place "Write-Host "Total Numbers of non-running VMs in"(Get-AzContext).Subscription.Name"is"$report.Count"" -ForegroundColor Green" inside the loop?

#

I get the count, but its not accurate, if the result is 0, seems like it overwrites it with the previous value

meager kiln
#

Get-Azvm -Status | ?{$_.PowerState -ne "VM Running"}

storm oriole
#

but the "Write-Host "Total Numbers of VMs in"(Get-AzContext).Subscription.Name"is"$vms.Count"" -ForegroundColor Green" works perfectly fine for each sub

meager kiln
#

this would only return VMs where the powerstate notequals "VM Running"

storm oriole
#

right

meager kiln
#

so you can change line 8 to that and then not worrk about the logic from 15ish to see if its running or not. t hen you dont need to have a count going

storm oriole
#

one example here:

Total Numbers of VMs in sub10 is 0
Total Numbers of non-running VMs in sub10 is 27

Total Numbers of VMs in sub11 is 0
Total Numbers of non-running VMs in sub11 is 27

#

if sub 10 and sub11 have no VMs, its grabbing the result count of the non running VM from the previous sub

#

Count is there to double check if the non running VMs actually is correct or not, just as a safety measure, and make it easy for mgmt to read the report

meager kiln
#

i would start to simplify your code to start

#

for example, its not clear why you're getting all the nics inside the sub and looping thru these...

#

just get all the non-running vms and loop thru them

storm oriole
#

$report = @()
$SubscriptionIds = Get-AzSubscription
$SubscriptionIds.Subscription.Name
Foreach ($SubscriptionId in $SubscriptionIds)
{
Select-AzSubscription $subscriptionId
Get-AzSubscription -SubscriptionName $SubscriptionId.Name | Set-AzContext
$vms = Get-Azvm -Status | ?{$.PowerState -ne "VM Running"}
$nics = Get-AzNetworkInterface | ?{ $
.VirtualMachine -NE $null}
Write-Host "Total Numbers of VMs in"(Get-AzContext).Subscription.Name"is"$vms.Count"" -ForegroundColor Green
foreach ($nic in $nics)
{

$ReportDetails = "" | Select SubName, OsType, VmName,Hostname, Powerstate
$vm = $vms | ? -Property Id -eq $nic.VirtualMachine.id
#$count=$vm.Powerstate -eq "VM deallocated" -or $vm.Powerstate -eq "VM Stopped"

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 -Property * -AutoSize

storm oriole
meager kiln
#

doesnt look like it.. everything was from the $vm object...

#

unless there is more code not being shared

storm oriole
#

yes, I didnt share the whole code, due to limitation on words here

#

but that isnt a worry, I was trying to fix my output

#

$.PowerState : The term '$.PowerState' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

#

$vms = Get-Azvm | ?{$.PowerState -ne "VM Running"}

meager kiln
#

?{$_.Powerstate -ne "VM Running"}

#

forgot the _

#

this should do it:

#

$report = @()
$Subscriptions = Get-AzSubscription
Foreach ($Subscription in $Subscriptions)
{
Set-AzContext $Subscription.Id
$vms = Get-Azvm -Status | ?{$_.Powerstate -ne "VM Running"}
if($vms.Count -gt 0)
{
Write-Host "Total Numbers of Not Running VMs in"$Subscription.Name"is"$vms.Count"" -ForegroundColor Green
foreach ($vm in $vms)
{

        $ReportDetails = "" | Select SubName, OsType, VmName,Hostname, Powerstate
        $ReportDetails.Hostname= $vm.OSProfile.ComputerName
        $ReportDetails.SubName = $Subscription.Name
        $ReportDetails.OsType = $vm.StorageProfile.OsDisk.OsType 
        $ReportDetails.VMName = $vm.Name 
        $ReportDetails.Powerstate = $vm.Powerstate
        $report+=$ReportDetails         
    }

}
}

#

line 7 checks if we got more than 0 non-running VMs and if we did we do the loop over those VMs. If we got 0 then we loop to the next subscription

#

if you now need to get the NIC info for the non-running VMs do it inside of this loop, not before. no need to find NICs for VMs you dont care about.

#

and just expand your $reportdetails data as needed

#

feel free to ask if something doesnt make sense

storm oriole
#

I was reading that += is expensive and slow

#

anyways to make the script faster?

meager kiln
#

the only performance-impacting lines are the Get-AzSubscription (if you have a lot of subs) and the get-azvm (if you have a lot of VMs)

#

i'll run this over my account right now moment

#

the += isnt interesting until you're talking about 10's of thousands and a lot more complicated arrays than yours. wouldnt sweat it. ive done it over crazy amounts ๐Ÿ™‚

#

but you cant make the Azure API calls quicker., you just need to use them wisely ๐Ÿ™‚

#

in your original code you were calling them a lot of times, which made it slow. I'm relying on the data we already got for the report outputs (for example)

#

ya the real impact is my get-azvm on my subs with 100's of VMs inside

storm oriole
#

performance isnt that bad tbh

meager kiln
#

but this is MS, cant do much with that ๐Ÿ˜„

storm oriole
#

just curious on other ways if possible to make it quicker haha

meager kiln
#

you could use Azure Resource Graph maybe to do better

#

but this is a topic a bit more advnaced than you're ready for now. dont take that the wrong way, i just dont suspect your environment is large enough to stress about the performance right now.

#

if you're doing a lot of reporting maybe a nightly export of get-azvm to a CSV and inmport into a SQL table could be helpful

#

so you already have all the data

#

I think we should drop the -Status on the Get-AzVm call as well, this pulls a lot less data. let me test

#

oh powerstate duh sorry

#

thats not returned by default, sorry.

#

yep this is as good as you're going to get ๐Ÿ™‚

storm oriole
# meager kiln yep this is as good as you're going to get ๐Ÿ™‚

I made some changes, you'll hate me :

$report = @()
$Subscriptions = Get-AzSubscription
Foreach ($Subscription in $Subscriptions)
{
Set-AzContext $Subscription.Id
$vms = Get-Azvm -Status | ?{$_.Powerstate -ne "VM Running"}
$count = (Get-Azvm).count
if($vms.Count -gt 0)
{
Write-Host "Total Numbers of VMs in"(Get-AzContext).Subscription.Name"is"$count"" -ForegroundColor Green
Write-Host "Total Numbers of Not Running VMs in"$Subscription.Name"is"$vms.Count"" -ForegroundColor Green
foreach ($vm in $vms)
{

        $ReportDetails = "" | Select SubName, OsType, VmName,Hostname, Powerstate
        $ReportDetails.Hostname= $vm.OSProfile.ComputerName
        $ReportDetails.SubName = $Subscription.Name
        $ReportDetails.OsType = $vm.StorageProfile.OsDisk.OsType 
        $ReportDetails.VMName = $vm.Name 
        $ReportDetails.Powerstate = $vm.Powerstate
        $report+=$ReportDetails
    }

}
}
$report| ft -Property * -AutoSize

#

lol

#

I got the total VM count and number of non running count now, if powerstate is not running

meager kiln
#

ok so you can speed that up. first in your write-host yyoure doing an API call (get-azcontext)

#

but we already have the subscriptionname (see my example)

#

and if you want all VMs we can get them all once then filter that list for running and not running to get the separate counts like this:

#

(I assume you also always want to output the total count of VMs in a Subscription? or only when there are non-running VMs? how you have it written now that Write-Host only happens when there are more than 0 not-running VMs)

storm oriole
#

Yes, your correct.. only count total VMs and then the non running VMs if found

#

you're*

#

Final code:

$report = @()
$Subscriptions = Get-AzSubscription
Foreach ($Subscription in $Subscriptions)
{
Set-AzContext $Subscription.Id
$vms = Get-Azvm -Status | ?{$_.Powerstate -ne "VM Running"}
$count = (Get-Azvm).count
if($vms.Count -gt 0)
{
Write-Host "Total Numbers of VMs in"$Subscription.Name"is"$count"" -ForegroundColor Green
Write-Host "Total Numbers of Not Running VMs in"$Subscription.Name"is"$vms.Count"" -ForegroundColor Green
foreach ($vm in $vms)
{

        $ReportDetails = "" | Select SubName, OsType, VmName,Hostname, Powerstate
        $ReportDetails.Hostname= $vm.OSProfile.ComputerName
        $ReportDetails.SubName = $Subscription.Name
        $ReportDetails.OsType = $vm.StorageProfile.OsDisk.OsType 
        $ReportDetails.VMName = $vm.Name 
        $ReportDetails.Powerstate = $vm.Powerstate
        $report+=$ReportDetails
    }

}
}
$report| ft -Property * -AutoSize

#

works like a charm

meager kiln
#

can make it faster ๐Ÿ˜‰

storm oriole
#

haha sure

meager kiln
#

but you only want to give the Total count of VMs IF there are non-running, or do you always want the total count ?

#

$report = @()
$Subscriptions = Get-AzSubscription
Foreach ($Subscription in $Subscriptions)
{
Set-AzContext $Subscription.Id
$vms = Get-AzVm -Status
$notrunningvms = $vms | ?{$_.Powerstate -ne "VM Running"}
Write-Host "Total Numbers of VMs in"$Subscription.Name"is"$vms.Count"" -ForegroundColor Green
if($notrunningvms.Count -gt 0)
{
Write-Host "Total Numbers of Not Running VMs in"$Subscription.Name"is"$notrunningvms.Count"" -ForegroundColor Green
foreach ($vm in $notrunningvms)
{
$ReportDetails = "" | Select SubName, OsType, VmName,Hostname, Powerstate
$ReportDetails.Hostname= $vm.OSProfile.ComputerName
$ReportDetails.SubName = $Subscription.Name
$ReportDetails.OsType = $vm.StorageProfile.OsDisk.OsType
$ReportDetails.VMName = $vm.Name
$ReportDetails.Powerstate = $vm.Powerstate
$report+=$ReportDetails
}
}
}

#

this always outputs the total count, and outputs the non-running count if any are found.

#

PLUS at the top you can see I'm only doing one Get-AzVM and using that output to filter for non-running, rather than get-azvm'ing two times

storm oriole
meager kiln
#

ok got it. you dont care about a sub that has all running VMs at all in this report

storm oriole
#

right, and that makes it faster too

meager kiln
#

then it goes inside the if loop:
$report = @()
$Subscriptions = Get-AzSubscription
Foreach ($Subscription in $Subscriptions)
{
Set-AzContext $Subscription.Id
$vms = Get-AzVm -Status
$notrunningvms = $vms | ?{$_.Powerstate -ne "VM Running"}
if($notrunningvms.Count -gt 0)
{
Write-Host "Total Numbers of VMs in"$Subscription.Name"is"$vms.Count"" -ForegroundColor Green
Write-Host "Total Numbers of Not Running VMs in"$Subscription.Name"is"$notrunningvms.Count"" -ForegroundColor Green
foreach ($vm in $notrunningvms)
{
$ReportDetails = "" | Select SubName, OsType, VmName,Hostname, Powerstate
$ReportDetails.Hostname= $vm.OSProfile.ComputerName
$ReportDetails.SubName = $Subscription.Name
$ReportDetails.OsType = $vm.StorageProfile.OsDisk.OsType
$ReportDetails.VMName = $vm.Name
$ReportDetails.Powerstate = $vm.Powerstate
$report+=$ReportDetails
}
}
}

storm oriole
#

if no results found

meager kiln
#

nah not really

#

it still has to get-azvm to check

#

doing the count takes so little time as to not be interesting

#

but sure, if your goal is only to output when there are non-running VMs then keep the output inside that loop. all depends on your needs ๐Ÿ™‚

storm oriole
#

Thanks alot, this really helps

meager kiln
#

you're welcome, any time!

storm oriole
#

$report = @()
$Subscriptions = Get-AzSubscription
Foreach ($Subscription in $Subscriptions)
{
Set-AzContext $Subscription.Id
$vms = Get-AzVm -Status
$notrunningvms = $vms | ?{$.Powerstate -ne "VM Running"}
$nics = Get-AzNetworkInterface | ?{ $
.VirtualMachine -NE $null}
foreach ($nic in $nics)
{
if($notrunningvms.Count -gt 0)
{
Write-Host "Total Numbers of VMs in"$Subscription.Name"is"$vms.Count"" -ForegroundColor Green
Write-Host "Total Numbers of Not Running VMs in"$Subscription.Name"is"$notrunningvms.Count"" -ForegroundColor Green
foreach ($vm in $notrunningvms)
{
$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
$ReportDetails.ResourceGroupName = $vm.ResourceGroupName
$ReportDetails.Region = $vm.Location
$VmSize = $vm.HardwareProfile.VmSize
$ReportDetails.VmSize = $vm.HardwareProfile.VmSize
$ReportDetails.NumberOfCores = Get-AzVMSize -VMName $vm.Name -ResourceGroupName $vm.ResourceGroupName | where{$_.Name -eq $vmsize} | Select-Object -ExpandProperty NumberOfCores
$ReportDetails.PrivateIpAddress = $nic.IpConfigurations.PrivateIpAddress
$ReportDetails.Subnet = $nic.IpConfigurations.Subnet.Id.Split("/")[10]
$ReportDetails.VNet = $nic.IpConfigurations.Subnet.Id.Split("/")[8]
$report+=$ReportDetails
}
}
}
}

meager kiln
#

$ReportDetails.Hostname= (get-azvm $vm.Name -ResourceGroupName $vm.ResourceGroupName).OSProfile.ComputerName
$ReportDetails.SubName = (Get-AzContext).Subscription.Name
you have this data already ๐Ÿ™‚ dont go get it again, save some API calls

storm oriole
#

Errors:

Exception setting "ResourceGroupName": "The property 'ResourceGroupName' cannot be found on this object. Verify that the property exists and can be set."
At line:22 char:9

  •     $ReportDetails.ResourceGroupName = $vm.ResourceGroupName
    
  •     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
    • FullyQualifiedErrorId : ExceptionWhenSetting

Exception setting "Region": "The property 'Region' cannot be found on this object. Verify that the property exists and can be set."
At line:23 char:9

  •     $ReportDetails.Region = $vm.Location
    
  •     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
    • FullyQualifiedErrorId : ExceptionWhenSetting

Exception setting "VmSize": "The property 'VmSize' cannot be found on this object. Verify that the property exists and can be set."
At line:25 char:9

  •     $ReportDetails.VmSize = $vm.HardwareProfile.VmSize
    
  •     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
    • FullyQualifiedErrorId : ExceptionWhenSetting

Exception setting "NumberOfCores": "The property 'NumberOfCores' cannot be found on this object. Verify that the property exists and can be set."
At line:27 char:9

  •     $ReportDetails.NumberOfCores = Get-AzVMSize -VMName $vm.Name  ...
    
  •     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
    • FullyQualifiedErrorId : ExceptionWhenSetting

Exception setting "PrivateIpAddress": "The property 'PrivateIpAddress' cannot be found on this object. Verify that the property exists and can be set."
At line:28 char:9

  •     $ReportDetails.PrivateIpAddress = $nic.IpConfigurations.Priva ...
    
meager kiln
#

$ReportDetails = "" | Select SubName, OsType, VmName,Hostname, Powerstate

#

you removed this line..

#

add it back and add the addiotnal properties you want to add

#

the only reason the other fields are working is because you still have the variable defined with its properties in your powershell session

storm oriole
#

added it back, but it keeps looping to the one sub it finds the result for, and doesnt move onto the next sub

meager kiln
#

I guess its related to the NICs again

#

you're looping thru all nics with no relation to the VM you're reporting on

#

dont get all NICs and loop thru them, only get a NIC for the VM you care about

storm oriole
#

Yup, my placement for my nics wasnt in the right spot.. this is running:

Foreach ($Subscription in $Subscriptions)
{
Set-AzContext $Subscription.Id
$vms = Get-AzVm -Status
$notrunningvms = $vms | ?{$.Powerstate -ne "VM Running"}
if($notrunningvms.Count -gt 0)
{
Write-Host "Total Numbers of VMs in"$Subscription.Name"is"$vms.Count"" -ForegroundColor Green
Write-Host "Total Numbers of Not Running VMs in"$Subscription.Name"is"$notrunningvms.Count"" -ForegroundColor Green
foreach ($vm in $notrunningvms)
{
$nics = Get-AzNetworkInterface | ?{ $
.VirtualMachine -NE $null}
$ReportDetails = "" | Select SubName, OsType, VmName,Hostname, Powerstate, ResourceGroupName, Region, VmSize, NumberOfCores, PrivateIpAddress, Subnet, VNet
$ReportDetails.Hostname= $vm.OSProfile.ComputerName
$ReportDetails.SubName = $Subscription.Name
$ReportDetails.OsType = $vm.StorageProfile.OsDisk.OsType
$ReportDetails.VMName = $vm.Name
$ReportDetails.Powerstate = $vm.Powerstate
$ReportDetails.ResourceGroupName = $vm.ResourceGroupName
$ReportDetails.Region = $vm.Location
$VmSize = $vm.HardwareProfile.VmSize
$ReportDetails.VmSize = $vm.HardwareProfile.VmSize
$ReportDetails.NumberOfCores = Get-AzVMSize -VMName $vm.Name -ResourceGroupName $vm.ResourceGroupName | where{$_.Name -eq $vmsize} | Select-Object -ExpandProperty NumberOfCores
$ReportDetails.PrivateIpAddress = $nic.IpConfigurations.PrivateIpAddress
$ReportDetails.Subnet = $nic.IpConfigurations.Subnet.Id.Split("/")[10]
$ReportDetails.VNet = $nic.IpConfigurations.Subnet.Id.Split("/")[8]
$report+=$ReportDetails
}
}
}

meager kiln
#

that wont work im sure

#

because you dont set $nic anywhere, just $nics

storm oriole
#

lol its running and gives me the results

#

no errors

meager kiln
#

no errors but the $nic its outpuitting isnt correct

storm oriole
#

ah ok

#

so I'

#

I'll change it to $nic

meager kiln
#

no no

#

you cant just get-aznetworkinterface for a single VM somehow? let me look up that command

storm oriole
#

hmm

#

you're right, the output for Nic isnt correct

#

output:

meager kiln
#

yep this is anoying ๐Ÿ˜„ hehe almost got it

#

replace $nics = Get-AzNetworkInterface | ?{ $.VirtualMachine -NE $null} with the above

storm oriole
#

ok

#

running, no errors so far

#

waiting for $result to output lol

meager kiln
#

๐Ÿ˜„

#

i dont think you can have a VM object with 0 NICs so that should work

#

that would be the only problem

storm oriole
#

yah, maybe we can add another condition lol

#

if it can ping the nic lol

#

oh wait, we are looking for not running VMs

#

so ping wont even work

meager kiln
#

no no, you'd set a default of $ip = "no nic" for example and only overwrite it if you find a NIC

#

so the output is never null

#

i think the Get-AzVm -Status command will throttle you to 100 VMs so if you have a sub with more this wont work. workaround (which sucks) is to get-azvm without status first

#

and then loop thru each of those VMs to get-azvm -vmname xxxx -status

#

THEN check if its powered on or off ;...

#

when you hit that limit though you should be considering a better way to monitor ๐Ÿ™‚

storm oriole
#

I think the max I saw for a single sub was 96

#

96 Vms

meager kiln
#

ok.. if they still have the 100 cap you'll see the message and the hint how to workaround it

storm oriole
#

couple of VMs show Info Not Available

#

but they are running in Azure lol

#

Info Not Available under powerstate

meager kiln
#

ya could be

#

go check in Portal

#

or you dont have the access to read that data, this is possible too

storm oriole
#

portal is showing those 2 VMs as running lol

#

weird

meager kiln
#

-status output i think also relies on the Guest agents being running?

storm oriole
#

so maybe remove -status?

#

dont think its really needed

#

$vms = Get-AzVm

meager kiln
#

it is, without you cant see powerstate

storm oriole
#

ah ok

meager kiln
#

for the ones you're getting no status on you can try deallocating (stop) and start them

#

it is probably an azure api issue

storm oriole
#

i wish I could

#

maybe later

meager kiln
#

so probably running for too long ๐Ÿ˜‰

#

do a deallocate, not in-guest reboot

#

(stop from portal)

#

then start

storm oriole
#

weird these two VMS never shows up before

#

only after we added nic portion to the script lol

meager kiln
#

if it still has a problem then a MS case is the next step

#

thats strange

storm oriole
#

ok, ran script again, and this time they didnt show up

#

prolly a bug

#

that was before

#

now all good

#

ok I think Im done for the day

meager kiln
#

same! tgif

storm oriole
#

Thank you so much @meager kiln

meager kiln
#

any time!

#

good luck!