简体   繁体   English

PowerShell 查找域中的计算机,用户已登录

[英]PowerShell to find computers in the domain, user is logged on

I am fairly new to PowerShell.我对 PowerShell 相当陌生。 I have this script working for computer names on the domain, thanks to @Adam Bertram https://4sysops.com/archives/how-to-find-a-logged-in-user-remotely-using-powershell/ .感谢@Adam Bertram https://4sysops.com/archives/how-to-find-a-logged-in-user-remotely-using-powershell/ ,我让这个脚本适用于域上的计算机名称。 I understand it and it works fine on my domain ie I can query the computer name and it returns a list of logged users.我理解它,它在我的域上运行良好,即我可以查询计算机名称并返回登录用户列表。 I have difficulties altering the code so it could return a list of computer names for a given user, instead.我很难更改代码,因此它可以返回给定用户的计算机名称列表。 I believe the issue could be with the correct Win32 class.我相信问题可能出在正确的 Win32 class 上。

function Get-LoggedOnUser
 {
     [CmdletBinding()]
     param
     (
         [Parameter()]
         [ValidateScript({ Test-Connection -ComputerName $_ -Quiet -Count 1 })]
         [ValidateNotNullOrEmpty()]
         [string[]]$ComputerName = $env:COMPUTERNAME
     )
     foreach ($comp in $ComputerName)
     {
         $output = @{ 'ComputerName' = $comp }
         $output.UserName = (Get-WmiObject -Class win32_computersystem -ComputerName $comp).UserName
         [PSCustomObject]$output
     }
 }

thanks Tomasz谢谢托马斯

Try this:尝试这个:

function Get-LoggedOnUser
 {
     [CmdletBinding()]
     param
     (
         [ Parameter() ]
         $UserName
     )

     Get-WmiObject -Class win32_computersystem | Where-Object { $_.Username -like  "*$UserName*" } | Select-Object Name
 }

 Get-LoggedOnUser -UserName "vish"

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

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