powershell split multiple lines

Xu, Tao 45 Reputation points
2023-09-13T05:01:54.7533333+00:00

I am runing

$vn = Get-AzVirtualNetwork -Name mynet
$vn.Subnets | Select Name, AddressPrefix,id | sort AddressPrefix

How may I list resourcegroup name which is in id?

PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,502 questions
0 comments No comments
{count} votes

Accepted answer
  1. anza 836 Reputation points
    2023-09-13T07:26:57.9766667+00:00

    are you asking for resource group of vn?
    $vn.ResourceGroupName

    or do you expect subnets to have different resourcegroupname?
    in that case by string manipulation you get the ResourceGroupNameFromId via

    $vn.Subnets | Select Name, AddressPrefix,@{n='ResourceGroupNameFromId';e={((($_.Id -split '/resourceGroups/')[1]) -split '/providers/')[0]}} | sort AddressPrefix

    property 'ID' was replaced by 'ResourceGroupNameFromId'

    regards

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.