我必须编写一个脚本来获得所有的EoL Windows机器。但我得为一些例外做个白名单。
首先,我得到了所有的旧电脑:
$getad = Get-ADComputer -Filter {
(operatingsystem -like "*Windows 10*" -and OperatingSystemVersion -notlike "*16299*" -and OperatingSystemVersion -notlike "*14393*" -and OperatingSystemVersion -notlike "*15063*"`) `
-or (operatingsystem -like "*Windows Vista*") `
-or (operatingsystem -like "*Windows XP*") `
-or (operatingsystem -like "*95*") `
-or (operatingsystem -like "*94*") `
-or ( operatingsystem -like "*Windows 8*" -and OperatingSystemVersion -notlike "*9600*") `
-or (operatingsystem -like "*2000 Professional*") `
-or (operatingsystem -like "*2000 Server*") `
-or (operatingsystem -like "*2003*") `
-or (operatingsystem -like "*Windows NT*") `
-or ( operatingsystem -like "*Windows 7*" -and OperatingSystemVersion -notlike "*7601*")
} `
-Properties ('Name', 'operatingsystem', 'DistinguishedName', 'description', 'lastlogondate', 'OperatingsystemVersion', 'Created', 'Enabled', 'SamAccountName')
$selectobj = $getad | Select-Object Name, Operatingsystem, DistinguishedName, Description, Lastlogondate, OperatingSystemVersion, Created, Enabled, SamAccountName然后我将白名单加载到一个变量中:
$whitelisted = Get-Content "C:\Users\example\Desktop\whitelistedpcs.txt"然后,我首先清空一个变量(可以肯定),然后比较我得到的计算机和我的白名单。
$AlertoldComputers = $null
$alertcompdist = Compare-Object -ReferenceObject $selectobj.DistinguishedName -DifferenceObject $whitelisted现在,我的结果是列出了所有不在白名单上的计算机,但我刚刚得到了杰出的名称。我需要所有的信息:
名称,操作系统,DistinguishedName,描述,Lastlogondate,OperatingSystemVersion,已创建,启用,SamAccountName
我尝试过用foreach来完成这个任务,但是却没有得到任何输出:
foreach ($alertcomputer in $alertcompdist) {
$AlertoldComputers += Get-ADComputer -Filter {(DistinguishedName -like "*$alertcomputer*")} -Properties ('Name', 'operatingsystem', 'DistinguishedName', 'description', 'lastlogondate', 'OperatingsystemVersion', 'Created', 'Enabled', 'SamAccountName') | Select-Object Name, Operatingsystem, DistinguishedName, Description, Lastlogondate, OperatingSystemVersion, Created, Enabled, SamAccountName
}输出应该如下所示:
名称: Examplename 操作系统: Windows 10企业 DistinguishedName : CN=Examplename,OU=Bla,OU=Da,OU=computers,OU=dadada,OU=gugu,DC=tra,DC=la,DC=la 描述:这只是一个例子。 最后期限: 01.01.1000 OperatingSystemVersion : 10.0 (10586) 创建: 01.01.1000 启用:真 SamAccountName :示例
发布于 2017-11-15 14:10:02
你能试试这个吗?
其目的是显示计算机的AD信息,并使用if ($whitelisted -match $_.DistinguishedName)检查是否在已白名单的主机名列表中找到一个值:
$getad = Get-ADComputer -Filter {(operatingsystem -like "*Windows 10*" -and OperatingSystemVersion -notlike "*16299*" -and OperatingSystemVersion -notlike "*14393*" -and OperatingSystemVersion -notlike "*15063*") -or (operatingsystem -like "*Windows Vista*") -or (operatingsystem -like "*Windows XP*") -or (operatingsystem -like "*95*") -or (operatingsystem -like "*94*") -or ( operatingsystem -like "*Windows 8*" -and OperatingSystemVersion -notlike "*9600*") -or (operatingsystem -like "*2000 Professional*") -or (operatingsystem -like "*2000 Server*") -or (operatingsystem -like "*2003*") -or (operatingsystem -like "*Windows NT*") -or ( operatingsystem -like "*Windows 7*" -and OperatingSystemVersion -notlike "*7601*")} -Properties ('Name', 'operatingsystem', 'DistinguishedName', 'description', 'lastlogondate', 'OperatingsystemVersion', 'Created', 'Enabled', 'SamAccountName')
$whitelisted = Get-Content "C:\Users\example\Desktop\whitelistedpcs.txt"
$getad | Select-Object Name, Operatingsystem, DistinguishedName, Description, Lastlogondate, OperatingSystemVersion, Created, Enabled, SamAccountName | ForEach-Object {
if ($whitelisted -match $_.DistinguishedName) {
Write-Host "$($_.DistinguishedName) is whitelisted"
}
else{
Write-Host "$($_.DistinguishedName) is not whitelisted" -ForegroundColor Yellow
$_
}
}https://stackoverflow.com/questions/47309155
复制相似问题