Quick take: Using PowerShell split method for Azure scripts

When running scripts in Microsoft Azure, the PowerShell split method can be a handy tool in your arsenal. A good example is when getting resourceID of any given object. Let’s use for example the output of Get-AZResource for any given VM in my subscription. We can see that resourceID contains all the information to find the resource including subscription, resource group, type of resource, and the resource name itself.

Using PowerShell, we can store all the output of the previous cmdlet in a variable ($vTemp), and after that, we can list only the content of the resourceID field. Here is an example:

$vTemp.ResourceID

Here comes the split method to the rescue. We are going to split that entire string with the character “/” using the following cmdlet.

$vText = $vTemp.ResourceID.Split("/")

Now the $vtext variable contains one entry for each object. Here is an example of the output by checking the $vText, and also a specific position ($vtext[1] for example).

Write-Host "Original content................: " + $vTemp.ResourceId
Write-Host "Content of variable vtext.......: "
$vText
Write-Host "Content of the first position...: " + $vText[1]
Write-Host
Write-Host "Enumerating all values within variable vtext.."
Last but not least we can list all the content, one by one using a Do loop.
$i=1
Do
{
$vText[$i]
} While ($i++ -le ($vText).Count)

Featured image: Pixabay

Anderson Patricio

Anderson Patricio is a Canadian MVP in Cloud and Datacenter Management, and Office Server and Services, besides of the Microsoft Award he also holds a Solutions Master (MCSM) in Exchange, CISSP and several other certifications. Anderson contributes to the Microsoft Community with articles, tutorials, blog posts, twitter, forums and book reviews. He is a regular contributor here at Techgenix.com, MSExchange.org, ITPROCentral.com and Anderson Patricio.org (Portuguese).

Share
Published by
Anderson Patricio

Recent Posts

Monitoring Azure Windows Virtual Desktop using PowerShell

Monitoring Azure Windows Virtual Desktop, especially keeping an eye on the health of session hosts…

11 hours ago

Moving your SQL database to Azure SQL: Using the import method

Migrating SQL data to Microsoft Azure takes planning because there are several ways to do…

15 hours ago

Gateways and routers: A head-to-head comparison

Gateways and routers perform different functions, so both are necessary for a network. Let’s look…

18 hours ago

The new brain drain: What if WFH tech employees don’t come back?

Offices are reopening, but after months of a work-from-home routine, many employees may not want…

1 day ago

Amazon Fraud Detector generally available

Online payment frauds are a threat to any company doing business on the Web. Amazon…

2 days ago

Identity and access management sector buzzes with new funding, partnerships, solutions

Because no organization wants to end up in the headlines for a data breach, there…

2 days ago