Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7023869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:45:58+00:00 2026-05-27T23:45:58+00:00

I am counting servers in three different forests and I’d like to email the

  • 0

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

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:45:58+00:00Added an answer on May 27, 2026 at 11:45 pm

    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.

    $forestContainer = @()
    

    As you loop through your forests, create a custom object for each and add it to the array. In this example $myForests is either a collection of objects or a string array of names.

    $i = 1
    foreach ($forest in $myForests) {
        # Create a custom object associating all the data you want.
        $currentForest = New-Object PsObject -Property @{
                Forest = $forest
                Count = $i
                # Note: You can add as many properties to the object here as you like.
        }
        $forestContainer += $currentForest
        $i += 1
    }
    

    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:

    $forestContainer | where {$_.Count -lt 2}
    

    Or

    $forestContainer | format-table
    

    Or

    $forestContainer | Out-GridView
    

    To access the Count property which contains the current integer when that forest was processed you can use this syntax:

    $forestContainer[0].Count
    

    Hopefully you can apply this to your code. If you post some of your code I can help you more.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an server (available via ssh) on the internet that my friend and
I have an auction site that has a javascript timer counting down. For some
In SQL Server 2005 I have a table cm_production that lists all the code
I have an SQL Server query that needs to count the number of rows
I have a UDP server that reflects every ping message it receives (this works
I'm counting the right answers field of a table and saving that calculated value
I have one question about searching and counting files in directories and subdirectories. I
I've got data where I'm counting episodes of care (like ER visits). The trick
On my application I have a timer that checks for configuration updates from the
I have a sql server 2005 table called ZipCode, which has all the US

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.