'Appending suffix to an array list in PowerShell [duplicate]
I have an array list of hostnames that I need to do two things with. The first is to take the list of server names and make them lowercase. The second thing I need to do is append a domain suffix. I've gotten the array list created and made the list lowercase, but I'm having issues appending the domain suffix. Example of what I have: HOST1 HOST2 HOST3
$Stringarray = [System.Collections.ArrayList]@(“HOST1" ,"HOST2" , "HOST3", "HOST4")
$Stringarray.tolower()
$Stringarray
I can create an arraylist and then use .tolower() to get it lowercase, but I need each of the items in the list to include a domain name as well. For Example:
host1.contoso.com
host2.contoso.com
host3.contoso.com
host4.contoso.com
etc.
I think a foreach command would work, I'm just not sure how to pass the argument.
Solution 1:[1]
Unless you have a specific need to use an ArrayList, you can simply construct your Array in one go using a loop of your choice, ForEach-Object in the example below.
You can use the + arithmetic operator to concatenate strings.
$domain = 'contoso.com'
$Stringarray = "HOST1" ,"HOST2" , "HOST3", "HOST4" | ForEach-Object {
$_.ToLower() + ".$domain"
}
# $Stringarray is now the expected result:
#
# host1.contoso.com
# host2.contoso.com
# host3.contoso.com
# host4.contoso.com
Solution 2:[2]
Try to do this :
$Stringarray = [System.Collections.ArrayList]@(“HOST1" ,"HOST2" , "HOST3", "HOST4")
$Stringarray | % { "$($_.tolower()).contoso.com" }
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Santiago Squarzon |
| Solution 2 | Romylussone |
