4

I want to create an array of strings instead of a variable object so that I can use the "contains" keyword on each index of the array.

$myArray = Get-ADDomain

The above creates an object, which is not what I want. I also tried

[string[]] $myArray = Get-ADDomain

But after that, $myArray only contains one string and it is the first non-empty property of Get-ADDomain, in my case "ComputersContainer". What should I do to receive an array of strings where each string is a different property, such as

$myArray[0] = "AllowedDNSSuffixes = {}"
4
  • 2
    according to this page - Get-ADDomain (activedirectory) | Microsoft Docs — learn.microsoft.com/en-us/powershell/module/activedirectory/… - the property you want is .Name. if you are running ps5.1, you can simply use @(Get-ADDomain).Name to get the string values stored in all the .Name properties of each returned domain object. Commented Dec 27, 2018 at 3:08
  • 1
    Why do you want to do this? I use the activedirectory module in my PS scripts almost on a daily basis but never felt the need to convert a complete object to an array of strings. What is your goal? Can you give an example of that? Commented Dec 27, 2018 at 12:30
  • +1 to @GertJanKraaijeveld. This looks like an XY problem - there should be a way to get what you're after without the "array of strings" Commented Dec 27, 2018 at 12:40
  • Didn't know the term XY problem, but it is exactly what I have in mind :-) Commented Dec 27, 2018 at 12:47

2 Answers 2

3

You cannot cast a PSObject directly to a string array like that. However, this can be accomplished rather easily.

To get an array of string from the object

$myArray = Get-ADDomain
# You can use a standard array @() but these tends to be slower for bigger amount of data
$outArray = New-Object -TypeName System.Collections.Generic.List[String]

#To add just the value
$myArray.psobject.properties | Foreach { $outArray.Add($_.Value) }

# To add Name = {Value} instead
$myArray.psobject.properties | Foreach { $outArray.Add("$($_.Name) = {$($_.Value)}") }

Using an hasthable instead:

$myArray = Get-ADDomain
$hashtable = @{}
$myArray.psobject.properties | Foreach { $hashtable[$_.Name] = $_.Value }
# If you need to do something with the key
Foreach ($key in $hashtable.Keys) {
    $Value = $hashtable[$key]
    if ($value -like '*prod*') {
        Write-Host $key
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

This does not answer the question or explain what's actually happening in the OP's question.
1

PowerShell will always return objects by design of course, and specifying that [string[]], does not really change that.

For what you are trying to use, you have to force the array creation. The below is just one way, but I am sure others will have more elegant ways of doing this as well. Though I am curious why one would want to do this, this way. But, hey, that's just me.

# Create an empty array
$DomainData = @()

# Get all the data points for the utilized cmdlet, split on a common delimiter for the array
[string[]]$DomainData = (Get-ADDomain | Select *) -split ';'

# Display the array count 
$DomainData.Count
34

# validate getting a value from the array by using an index number
$Item = $DomainData[17]
NetBIOSName=CONTOSO

[array]::IndexOf($DomainData, $Item)
17

# Use that element number to validate the use of the contains comparison operator
0..($DomainData.Count - 1) | %{ If($DomainData[$_] -contains $item){"Index key is $_ contains a value of $Item"} }
Index key is 17 contains a value of  NetBIOSName=CONTOSO

# Use the previous with a partial string for a comparison, -contains cannot be used, like or match has to be used
# From the documentation:
# -Contains
# Description: Containment operator. Tells whether a collection of reference values includes a single test value.


$Item = '*domain*'
0..($DomainData.Count - 1) | %{ If($DomainData[$_] -like $item){"Index key is $_ like a value of $Item"} }
Index key is 1 like a value of *domain*
Index key is 6 like a value of *domain*
Index key is 7 like a value of *domain*
Index key is 8 like a value of *domain*
Index key is 18 like a value of *domain*
Index key is 20 like a value of *domain*

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.