我有一个程序根据文本文件select要连接的计算机。 该列表只需要计算机名称,每行1个。 没有其他的。
我需要一个命令或脚本,我可以运行,产生这一点。 我想每周自动运行一次这个脚本,用新的计算机更新列表。 服务器正在运行Windows Server 2008 R2。 我已经安装了Quest AD Module,使用它我没有find太多的帮助。
任何帮助,将不胜感激! 谢谢 :)
我认为是正确的,从一台没有域名的计算机上进行了一些改变来testing。
# a PowerShell script, licensed under GPL ;) # # importing dependancy, assuming it's already installed. # Install RSAT for Windows workstation, AD DS role for Windows Server if missing Import-Module "ActiveDirectory" # an array containing the OU paths we'll enumerate $OUpaths = @("OU=Allocated,OU=Workstations,OU=WDS Org,DC=wds,DC=wdsgroup,DC=local","OU=Available,OU=Workstations,OU=WDS Org,DC=wds,DC=wdsgroup,DC=local") # loop though the array of OUs, adding the computers to a list ('Object' really) foreach ($iOUpath in $OUpaths) { ($objComputers += Get-ADComputer -SearchBase $iOUpath -Filter *) #You might need to refine the query witha 'Filter' depending on your AD structure } # dump the list to a file $objComputers | Select name | Export-Csv -LiteralPath "C:\Temp\ComputerNames.txt" -NoTypeInformationA
查看下面的PowerShell单行示例,您可以更改和testing它的filter部分,以满足您从AD Operating System等查询属性的需求。
确保将输出文本文件的名称和位置更改为Out-File C:\Test\Test.txt零件文件的位置。
用于非服务器列表
Get-ADComputer -Filter {OperatingSystem -NotLike "*Server*"} | Select -Expand Name | Out-File C:\Test\Test.txt
用于服务器列表
Get-ADComputer -Filter {OperatingSystem -Like "*Server*"} | Select -Expand Name | Out-File C:\Test\TestServers.txt
用于自定义描述列表
(您可以从特定OU中的AD Users and Computers中select所有AD计算机对象,然后right-click并select所有计算机对象被选中时的Properties ,然后为Description值添加与OU相关的自定义唯一string(见下面的屏幕截图),然后你可以细化下面的searchfilter,find每个OU具有这个自定义Description唯一值的AD计算机)
Get-ADComputer -Filter {Description -Like "*CustomTestString*"} | Select -Expand Name | Out-File C:\Test\Custom.txt
我使用这一个class轮导出AD中的活动电脑:
Get-ADComputer -filter {Enabled -eq $True} -Properties cn -SearchBase "OU=servers,OU=computers,DC=example,DC=example" | select cn | ConvertTo-Csv -NoTypeInformation | Select-Object -skip 1 | Out-File d:\output.csv
您可以使用.txt而不是.csv,如果您需要它作为文本文件,并将脚本configuration为按计划任务运行。
希望这可以帮助你。
对于过去7天内创build的计算机,您可以这样做:
$d=[DateTime]::Today.AddDays(-7) $comps=Get-ADComputer -filter { (whencreated -le $d) } -searchbase "OU=,OU= ,DC= ,DC= " -properties whencreated, Description $comps | sort-object -Descending whencreated |ft Name -A write-host 'PCs=' $comps.Count