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

Top 7 open source intelligence tools lighting it up in 2019

As open source becomes the norm, open source intelligence tools are in demand. Here are a few that are already…

13 hours ago

Land of the dinosaurs: Is CapEx IT about to go extinct?

As businesses head to the cloud, will CapEx IT be a thing of the past? Or will there always be…

18 hours ago

Exchange DAG: Set it up before a disaster takes you down

Don’t be this IT admin: Disaster hits and all you can say is, “I should have set up a database…

21 hours ago

8 tips for protecting online privacy and maintaining anonymity

In protecting online privacy, the actions you take are far more important than any legal framework that could be put…

4 days ago

Cryptomining arrest only the start of problems for these nuke plant workers

Cryptomining on the job behind the boss’s back is bad enough, but when the employees work in a nuclear power…

4 days ago

Does it make sense to purchase a lifetime VPN subscription?

Cybersecurity experts recommend setting up a VPN to secure your data and privacy. But is purchasing a lifetime VPN subscription…

4 days ago