I am counting servers in three different forests and I’d like to email the results.
I have an integer ($i) that increases for each server, but I reset this number for every forest.
Is there a way that I can create a variable from the forest name ($forest) and a $i count?
I would also love if my mail would foreach the servers in forests count so this would be dynamic.
I guess I am asking if I can name a variable after a variable.. For example $forest.$i or similar…
Please help!
And happy new year!!
Edit:
# Get password (this is secure!)
$password = Read-Host "Enter password" -AsSecureString
# Create a container for your objects.
$forestContainer = @()
$forests = "corp.foresta.com","corp.forestb.com","corp.forestc.com"
foreach ($forest in $forests) {
# Create credentials for forest
$credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist "$forest\administrator",$password
# Connect to current forest
$forestconnection = Connect-QADService -Service $forest -Credential $credentials
$servers = Get-QADComputer -WarningAction SilentlyContinue -OSName *server*,*hyper*
echo $($servers.Length)
$currentForest = New-Object PsObject -Property @{
Name = $forest
Count = $($servers.Length)
}
$forestContainer += $currentForest
}
How about creating a custom object. Creating your own object is a great way to associate pieces of data together. In your case you’d like to associate a number to either a name of a forest or a forest object created by something else. Regardless if you are working with a list of forest name strings or objects you can use this approach.
Create a container (an array) for your custom objects.
As you loop through your forests, create a custom object for each and add it to the array. In this example
$myForestsis either a collection of objects or a string array of names.The benefit of this approach is you will be able to use the array filled with your new objects with other Powershell cmdlets easily. For example:
Or
Or
To access the Count property which contains the current integer when that forest was processed you can use this syntax:
Hopefully you can apply this to your code. If you post some of your code I can help you more.