Posted On December 26, 2019

PowerShell: Working With Arrays

kimconnect 0 comments
blog.KimConnect.com >> Codes >> PowerShell: Working With Arrays

Problem: elements of a System.Array couldn’t be counted

# Example output when an object is a Hashtable type
PS C:\Users\tola> $arr.gettype()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Hashtable System.Object

Resolution: convert a Hashtable to an Array type

# Perform explicit casting
PS C:\Users\tola> $x=[Array]$arr

# Example output when an object is an Array type
PS C:\Users\tola> $arr.gettype()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array

# Validate that the casting has succeeded
PS C:\Users\tola> $x.gettype()

IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array

# Since System Array type can contain objects, its elements can be counted
PS C:\Users\tola> $x.count
1

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

PowerShell: Email Users with Expiring Passwords

# PasswordExpirationNotification.ps1 # Description: # This script performs the following tasks # a. Query Active…

PowerShell: Some Tricks in Using the Here-String to Declare Blocks of Code

Trigger COMMAND CLI from within PowerShell # Fastest way to list all files in a…

PowerShell: Remove IP Address Assignment Using Bluecat API

$bluecatUri='http://bluecat.kimconnect.com/Services/API' $bluecatUsername='svc-bluecat-api' $bluecatPassword='PASSWORD' $configId=17 $ipv4Address='10.10.162.54' $marker='toBeDeleted-' function confirmation($content,$testValue="I confirm",$maxAttempts=3){ $confirmed=$false; $attempts=0; $content|write-host write-host "Please review…