Solved

Add a variable to property in Powershell

Posted on 2016-09-21
3
91 Views
Last Modified: 2016-09-21
Good Day,

This should be very quick and easy.

I have a powershell script and we have multiple child domains.  I would like to include a new property for which domain the ADUser object is in as part of the output.

Below is my script:
__________________________________________________________
$z='y'
$domains = (Get-ADForest).Domains

While ($z -eq 'y')
    {
    $userx = @()
    $nameregex = Read-Host "put in part or al of a user's name"
    $userd = '*'+$nameregex+'*'
        foreach ($domain in $domains) {
            if ($domain -ne "cst.is.local" ) {
                Write-Output "Searching Domain " $domain
               $userx += Get-ADUser -Filter {name -like $userd} -server $domain | Select-Object name,samaccountname,enabled,DistinguishedName
             }
        }
        Write-output $userx | ft -AutoSize
    $z = Read-Host 'do you want to find another user?'
    $z = $z.ToLower()
    }

________________________________________________________________
The script works great but I would like to add the $domain variable to the Select-object output.

Thanks in advance.
0
Comment
Question by:mrfite
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 34

Accepted Solution

by:
it_saige earned 500 total points
ID: 41809023
Like this:
$z='y'
$domains = (Get-ADForest).Domains

While ($z -eq 'y')
    {
    $userx = @()
    $nameregex = Read-Host "put in part or al of a user's name"
    $userd = '*'+$nameregex+'*'
        foreach ($domain in $domains) {
            if ($domain -ne "cst.is.local" ) {
                Write-Output "Searching Domain " $domain
               $userx += Get-ADUser -Filter {name -like $userd} -server $domain | Select-Object name,samaccountname,enabled,DistinguishedName,@{l="Domain";e={$domain}}
             }
        }
        Write-output $userx | ft -AutoSize
    $z = Read-Host 'do you want to find another user?'
    $z = $z.ToLower()
    }

Open in new window


-saige-
0
 

Author Comment

by:mrfite
ID: 41809047
Thanks,

I knew it was something simple.
0
 

Author Closing Comment

by:mrfite
ID: 41809051
Thanks again
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

626 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question