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

User-friendly web design tools for a user-friendly website

If you want your business to succeed these days, you need a user-friendly website. Put these tools in your toolbox…

10 hours ago

New kids vs. old reliable: Can chat apps replace email?

Do businesses rely too heavily on email for communication? Yes! Is the answer to replace email with chat apps? No!…

15 hours ago

PowerShell function to standardize message box script outputs

If you got your start with Visual Basic, you remember msgbox. This PowerShell function will bring back memories and help…

18 hours ago

Microsoft Ignite 2019: Top announcements and product unveilings

Microsoft Ignite 2019 included a slew of updates on products and services. Among the announcements was Azure Arc, which may…

1 day ago

Who says configuration management can’t be fun?

Managing change in an enterprise isn’t easy and it’s usually no fun. Here’s a book on configuration management that will…

2 days ago

Choosing the right communication tools for your business

Choosing communication tools is like choosing a first progamming language. While you want easy, you also want cross-platform, security, and…

2 days ago