简体   繁体   English

Powershell 在计算机上搜索特定描述

[英]Powershell searching for specific description on computers

in my organisation the "description" attribute on computers automatically updates who logged on last and when, so it looks like this "OU,USERNAME,DATE AND TIME" Example: IT-afdelingen,chrijen,28-07-2021 08:16:12在我的组织中,计算机上的“描述”属性会自动更新上次登录的用户和时间,因此看起来像这样“OU、用户名、日期和时间”示例:IT-afdelingen,chrijen,28-07-2021 08:16: 12

i want to search for the USERNAME in the description so i can see what pc the users logged on last.我想在描述中搜索 USERNAME,以便我可以看到用户最后登录的电脑。


this does not work because it seems like i cant but $hanne between **这不起作用,因为看起来我不能但是 $hanne 介于 **

$hanne = "dthamyl","chrijen","bjokjer"
foreach ($item in $hanne) {Get-ADComputer -filter "description -like '*$hanne*'"}

if i put the usernames in the variable like this it works如果我像这样将用户名放在变量中,它会起作用

    $hanne = "*dthamyl*","*chrijen*","*bjokjer*"

basically i want to take a bunch of displaynames and find their usernames and use the usernames to see which computer they use基本上我想拿一堆显示名并找到他们的用户名并使用用户名查看他们使用的计算机

$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'"}

If I understand correctly, the Description property contains the SamAccountName of the logged on user and you want to find these computers based on an array of SamAccountNames of the users.如果我理解正确,Description 属性包含登录用户的 SamAccountName,并且您想根据用户的 SamAccountNames 数组查找这些计算机。

In that case, this should get you going:在这种情况下,这应该可以帮助您:

# 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]}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM