How to add status (powerstate) with the script

How to add status (powerstate) with the script
i need to get-azvm -status with below script

how i can add it ?

#Provide the subscription Id where the VMs reside
$subscriptionId = "******"

#Provide the name of the csv file to be exported
$reportName = "****"

Select-AzSubscription $subscriptionId
$report = @()
$vms = Get-AzVM
$publicIps = Get-AzPublicIpAddress 
$nics = Get-AzNetworkInterface | ?{ $_.VirtualMachine -NE $null} 
foreach ($nic in $nics) { 
    $info = "" | Select VmName, ResourceGroupName, Region, VmSize, VirturalNetwork, Subnet, PrivateIpAddress, OsType, PublicIPAddress, Version, Sku, VMStatus
    $vm = $vms | ? -Property Id -eq $nic.VirtualMachine.id 
    foreach($publicIp in $publicIps) { 
        if($nic.IpConfigurations.id -eq $publicIp.ipconfiguration.Id) {
            $info.PublicIPAddress = $publicIp.ipaddress
            } 
        } 
        $info.OsType = $vm.StorageProfile.OsDisk.OsType 
        $info.VMName = $vm.Name 
        $info.ResourceGroupName = $vm.ResourceGroupName 
        $info.Region = $vm.Location 
        $info.VmSize = $vm.HardwareProfile.VmSize
        $info.VirturalNetwork = $nic.IpConfigurations.subnet.Id.Split("/")[-3] 
        $info.Subnet = $nic.IpConfigurations.subnet.Id.Split("/")[-1] 
        $info.PrivateIpAddress = $nic.IpConfigurations.PrivateIpAddress
        $info.Version = $vm.storageprofile.imagereference.Version
        $info.Sku = $vm.storageprofile.imagereference.SKU
        $info.VMStatus = $vm  (need Help here)
        $report+=$info 
    } 
$report | ft VmName, ResourceGroupName, Region, VmSize, VirturalNetwork, Subnet, PrivateIpAddress, OsType, PublicIPAddress, Version, Sku, VMStatus
$report | Export-CSV "$home/$reportName"

Can you add some more content to the post. I believe just with the title, people wont be able to understand it properly.

1 Like

@keshav-prasad Is this a script you wrote, or just one you found online? Have you tried adding -Status to your Get-AzVM call?

i did it won’t report me…
just by get-azvm -status it reports me… i need to include in script to get PowerState of each vm in the subscription.

this script i got from MS site, i have customized it.

So you added -Status. What then happens when you call $vm.PowerState along with all the other properties in the For Loop?

i get this

VMStatus
System.Object

Please post the code, exactly as you have it, so I can be sure I am testing with the same code you’re testing with.

#Provide the subscription Id where the VMs reside
$subscriptionId = "*****"

#Provide the name of the csv file to be exported
$reportName = "*****"

Select-AzSubscription $subscriptionId
$report = @()
$vms = Get-AzVM
$vmstatus = Get-Azvm -status
$publicIps = Get-AzPublicIpAddress 
$nics = Get-AzNetworkInterface | ?{ $_.VirtualMachine -NE $null} 
foreach ($nic in $nics) { 
    $info = "" | Select VmName, ResourceGroupName, Region, VmSize, VirturalNetwork, Subnet, PrivateIpAddress, OsType, PublicIPAddress, Version, Sku, VMStatus
    $vm = $vms | ? -Property Id -eq $nic.VirtualMachine.id 
    foreach($publicIp in $publicIps) { 
        if($nic.IpConfigurations.id -eq $publicIp.ipconfiguration.Id) {
            $info.PublicIPAddress = $publicIp.ipaddress
            } 
        } 
        $info.OsType = $vm.StorageProfile.OsDisk.OsType 
        $info.VMName = $vm.Name 
        $info.ResourceGroupName = $vm.ResourceGroupName 
        $info.Region = $vm.Location 
        $info.VmSize = $vm.HardwareProfile.VmSize
        $info.VirturalNetwork = $nic.IpConfigurations.subnet.Id.Split("/")[-3] 
        $info.Subnet = $nic.IpConfigurations.subnet.Id.Split("/")[-1] 
        $info.PrivateIpAddress = $nic.IpConfigurations.PrivateIpAddress
        $info.Version = $vm.storageprofile.imagereference.Version
        $info.Sku = $vm.storageprofile.imagereference.SKU
        $info.VMStatus = $vmstatus.Powerstate
        $report+=$info 
    } 
$report | ft VmName, ResourceGroupName, Region, VmSize, VirturalNetwork, Subnet, PrivateIpAddress, OsType, PublicIPAddress, Version, Sku, VMStatus
$report | Export-CSV "$home/$reportName"

HI [jlogan3o13

have you figure out anything ?