我喜歡使用Job的想法,但在處理錯誤時遇到了麻煩。由於我們仍然擁有如此多的Server 2003數據中心,我只是按照最初的想法去做。這是最後的腳本。感謝您的反饋。
<#
.Synopsis
Searches ActiveDirectory and returns a user-specified list of properties
.DESCRIPTION
This script takes a user-specified list OUs and a user-specified list of desired properties.
.NOTES
Author: Mike Hashemi
V1 date: 15 August 2014
V2 date: 6 October 2014
- Converted the main part of the script, into a function.
- Added routie to gather all DCs in a domain, for the ability to return LastLogonDate.
.LINK
http://stackoverflow.com/questions/26163437/creating-objects-with-unknown-number-of-properties-powershell
.PARAMETER DomainName
Default value is 'company.local'. This parameter represents the DNS domain name, of the domain.
.PARAMETER SearchPath
Default value is 'OU=people,DC=company,DC=local'. This parameter represents a comma-separated list of OUs to search.
.PARAMETER OutputProperties
Default value is 'Name,Enabled,LastLogonDate'. This parameter represents a comma-separated list of properties to return.
.EXAMPLE
.\get-ADUserProperties-Parameterized.ps1
This example get's a list of all users in 'OU=people,DC=company,DC=local' and outputs the Name, Enabled, and LostLogonDate attributes.
.EXAMPLE
.\get-ADUserProperties-Parameterized.ps1 -SearchPath 'OU=people,DC=company,DC=local','OU=managers,DC=company,DC=local'
This example get's a list of all users in the 'OU=people,DC=company,DC=local' and 'OU=managers,DC=company,DC=local' OUs and outputs the
Name, Enabled, and LostLogonDate attributes.
.EXAMPLE
.\get-ADUserProperties-Parameterized.ps1 -SearchPath 'OU=people,DC=company,DC=local' -OutputProperties Name,telephoneNumber | Export-CSV c:\users.csv -NoTypeInformation
This example get's a list of all users in the 'OU=people,DC=company,DC=local' OU and outputs the Name and Telephone Number attributes.
The output is exported to a CSV.
#>
[CmdletBinding()]
param(
[string]$DomainName = 'managed.local',
[string[]]$SearchPath = 'OU=people,DC=company,DC=local',
[string[]]$OutputProperties = 'Name,Enabled,LastLogonDate'
)
Function Get-TheUsers {
#Create the hash table, for later.
$props = @{}
Try {
#The next two lines get the list of domain controllers, using the supplied DNS domain name.
Write-Verbose ("Getting domain controllers from {0}" -f $DomainName)
$temp = New-Object 'System.DirectoryServices.ActiveDirectory.DirectoryContext'("domain","$DomainName")
$dcs = [System.DirectoryServices.ActiveDirectory.DomainController]::FindAll($temp)
}
Catch [System.Management.Automation.MethodInvocationException] {
Write-Error ("Unable to connect to remote domains. Please run the script from a DC in {0}. " -f $DomainName)
Exit
}
Catch {
Write-Error ("There was an unexpected error. The message is: {0}" -f $_.Exception.Message)
Exit
}
Foreach ($ou in $SearchPath) {
Write-Verbose ("Getting users in {0}" -f $ou)
Foreach ($dc in $dcs) {
If ($dc.OSVersion -like '*2003*') {
Write-Warning ("Skipping {0}, because it is not a Server 2008 (or higher) DC." -f $dc)
}
Else {
Write-Verbose ("Searching {0} on {1}." -f $ou,$dc)
Try {
$users = Get-ADUser -Filter * -SearchBase $ou -Properties $OutputProperties.Split(",") -Server $dc -ErrorAction Stop | Select $OutputProperties.Split(",")
}
Catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException] {
Write-Error ("Unable to search {0}. It appears to be a non-existent OU. The specific error message is: {1}" -f $ou, $_.Exception.Message)
Exit
}
Foreach ($user in $users) {
ForEach($property in $OutputProperties.Split(",")) {
$props.$property = $user.$property
}
New-Object Psobject -Property $props
}
}
}
}
}
Try {
Import-Module ActiveDirectory -ErrorAction Stop
}
Catch [System.IO.FileNotFoundException] {
Write-Error ("Unable to load the required module. The specific message is: {0}" -f $_.Exception.Message)
Exit
}
$data = Get-TheUsers
#Takes the output of the Get-ADUser query and groups by the first property in $OutputProperties, then uses the LastLogonDate property (if present)
#to sort again and select only the last (most recent) entry.
Write-Verbose ("Sorting data.")
$data | Group-Object Name | ForEach-Object {$_.Group | Sort-Object LogonTimeDate | Select-Object -Last 1}
對於初學者,我猜你想查看所有域控制器上的所有用戶的詳細信息?如果'$ users = Get-ADUser -Filter * ....'行不在'Foreach($ dc'循環)中,你還必須將'-Server'更改爲-'Server $ dc'。您對後處理感到滿意嗎?獲取所有細節,然後爲每個用戶找到最新的一個,然後篩選出舊的? – Matt 2014-10-02 15:41:48