Home > Software engineering >  Using Array objects later on without knowing it's position
Using Array objects later on without knowing it's position

Time:12-22

I'm running following line of code to loop through all my Azure subscription saving vms in all subscription to array. Can I somehow query data from that array based on vm name so it would display all available data? Would need save that specific vm data on variable so I can use and parse it later.

 $VM = @()
    $AllSubscriptions = Get-AzSubscription
    foreach ($Subscription in $AllSubscriptions) {
        write-host "Count of subscriptions to loop:" $AllSubscriptions.length
        Set-AzContext $Subscription.Id | Out-Null
        Write-Host "Checking subscription $($Subscription.Name) for vm details"
        $VM  = Get-AzVM -Status 
    }

    $VM

$vm results format $vm results

Tried following commands but did not work for me:

$vm -like '*server01*'

This displayed only vm name, no additional information was available

$vm.name -like '*nalle45*'

This displayed right information but position can change and there can be several vms to be queried. So maybe somehow put '6' to variable..?

$vm[6]

CodePudding user response:

Try the below, it will filter the object by the name and output the whole object.

$vm | Where-Object {$_.name -like '*nalle45*'} | Select *
  • Related