Powershell 在计算机上搜索特定描述
Powershell searching for specific description on computers
在我的组织中,计算机上的“描述”属性会自动更新上次登录的用户和登录时间,因此它看起来像这样“OU、用户名、日期和时间”
例子:
IT-afdelingen,chrijen,28-07-2021 08:16:12
我想在描述中搜索用户名,这样我就可以看到用户最后登录的电脑。
这行不通,因为看起来我不能,但 $hanne 介于 **
$hanne = "dthamyl","chrijen","bjokjer"
foreach ($item in $hanne) {Get-ADComputer -filter "description -like '*$hanne*'"}
如果我像这样将用户名放入变量中,它就可以工作
$hanne = "*dthamyl*","*chrijen*","*bjokjer*"
基本上我想获取一堆显示名称并找到他们的用户名并使用用户名来查看他们使用的是哪台计算机
$navne = "DISPLAYNAME","DISPLAYNAME","DISPLAYNAME","DISPLAYNAME","DISPLAYNAME","DISPLAYNAME","DISPLAYNAME"
$usernames = foreach ($user in $navne) {get-aduser -filter "displayname -eq '$user'" | select samaccountname}
foreach ($item in $usernames) {
Get-ADComputer -Filter "description -like '$item'"}
如果我理解正确,描述 属性 包含登录用户的 SamAccountName,您希望根据用户的 SamAccountName 数组查找这些计算机。
在那种情况下,这应该让你继续:
# create an array of user SamAccountnames to look for in the Description property of the computers
$navne = "dthamyl","chrijen","bjokjer"
# create a regex from all usernames in $navne combined with the regex 'OR' character
# for safety, use [regex]::Escape() on each to escape characters that have special meaning in regex.
$users = ($navne | ForEach-Object { [regex]::Escape($_) }) -join '|'
# get the computer objects and use a Where-Object clause to filter on computers
# where the Description matches the users regex
Get-ADComputer -Filter * -Properties Description | Where-Object { $_.Description -match $users } |
Select-Object @{Name = 'ComputerName'; Expression = {$_.Name}},
@{Name = 'User'; Expression = { ($_.Description -split ',')[1]}}
在我的组织中,计算机上的“描述”属性会自动更新上次登录的用户和登录时间,因此它看起来像这样“OU、用户名、日期和时间” 例子: IT-afdelingen,chrijen,28-07-2021 08:16:12
我想在描述中搜索用户名,这样我就可以看到用户最后登录的电脑。
这行不通,因为看起来我不能,但 $hanne 介于 **
$hanne = "dthamyl","chrijen","bjokjer"
foreach ($item in $hanne) {Get-ADComputer -filter "description -like '*$hanne*'"}
如果我像这样将用户名放入变量中,它就可以工作
$hanne = "*dthamyl*","*chrijen*","*bjokjer*"
基本上我想获取一堆显示名称并找到他们的用户名并使用用户名来查看他们使用的是哪台计算机
$navne = "DISPLAYNAME","DISPLAYNAME","DISPLAYNAME","DISPLAYNAME","DISPLAYNAME","DISPLAYNAME","DISPLAYNAME"
$usernames = foreach ($user in $navne) {get-aduser -filter "displayname -eq '$user'" | select samaccountname}
foreach ($item in $usernames) {
Get-ADComputer -Filter "description -like '$item'"}
如果我理解正确,描述 属性 包含登录用户的 SamAccountName,您希望根据用户的 SamAccountName 数组查找这些计算机。
在那种情况下,这应该让你继续:
# create an array of user SamAccountnames to look for in the Description property of the computers
$navne = "dthamyl","chrijen","bjokjer"
# create a regex from all usernames in $navne combined with the regex 'OR' character
# for safety, use [regex]::Escape() on each to escape characters that have special meaning in regex.
$users = ($navne | ForEach-Object { [regex]::Escape($_) }) -join '|'
# get the computer objects and use a Where-Object clause to filter on computers
# where the Description matches the users regex
Get-ADComputer -Filter * -Properties Description | Where-Object { $_.Description -match $users } |
Select-Object @{Name = 'ComputerName'; Expression = {$_.Name}},
@{Name = 'User'; Expression = { ($_.Description -split ',')[1]}}