PowerShell脚本查找用户login到的所有计算机?

我希望能够审核我们的域名并查找特定用户login的所有机器。 有没有人有办法用PowerShell做到这一点?

这是一个脚本,我用它为一个域上的每台PC生成一个Excel工作表,目前正在login。 将其保存到一个.ps1文件并在放入OUpath进行search后运行它。 很久以前,我从网上的某处复制了大部分内容。

# This way it won't die when a machine is unavailable # It's powered off, or the machine account was left behind, etc $erroractionpreference = "SilentlyContinue" function GetList { param ([string]$base) # Give this function the LDAP search string and it will search there $blah = [ADSI]"$base" $objDomain = New-Object System.DirectoryServices.DirectoryEntry $objSearcher = New-Object System.DirectoryServices.DirectorySearcher $objSearcher.Filter = "(objectClass=computer)" $objSearcher.SearchRoot = $blah $PropList = "cn","operatingSystem" foreach ($i in $PropList){$objSearcher.PropertiesToLoad.Add($i)} $Results = $objSearcher.FindAll() foreach ($objResult in $Results) { $OS = $objResult.Properties.operatingsystem If ($OS -match "Windows") { Echo $objResult.Properties.cn | Out-File -Append -FilePath $OutFile } } } # This is for output $Outbook = New-Object -comobject Excel.Application $Outbook.visible = $True $Workbook = $Outbook.Workbooks.Add() $Worksheet = $Workbook.Worksheets.Item(1) $Worksheet.Cells.Item(1,1) = "Machine Name" $Worksheet.Cells.Item(1,2) = "Remote User" $Formatting = $Worksheet.UsedRange $Formatting.Interior.ColorIndex = 19 $Formatting.Font.ColorIndex = 11 $Formatting.Font.Bold = $True $intRow = 2 # Put the path to the OU with your computer accounts here, if you need more than one, put another GetList line GetList "LDAP://OU=Computers,dc=yourdomain,dc=name" foreach ($strComputer in Get-Content $OutFile) { $Worksheet.Cells.Item($intRow,1) = $strComputer.ToUpper() # Using .NET to ping the servers $Ping = New-Object System.Net.NetworkInformation.Ping $Reply = $Ping.send($strComputer) if($Reply.status -eq "success") { $RemoteSys = Get-WmiObject -Comp $strComputer -CL Win32_ComputerSystem If ($?) { $Worksheet.Cells.Item($intRow,2).Interior.ColorIndex = 4 $Worksheet.Cells.Item($intRow,2) = $RemoteUser = $RemoteSys.UserName } Else { $Worksheet.Cells.Item($intRow,2).Interior.ColorIndex = 3 $Worksheet.Cells.Item($intRow,2) = "Error" } } Else { $Worksheet.Cells.Item($intRow,2).Interior.ColorIndex = 3 $Worksheet.Cells.Item($intRow,2) = "Not Pingable" } $Formatting.EntireColumn.AutoFit() $Reply = "" $pwage = "" $intRow = $intRow + 1 } $Formatting.EntireColumn.AutoFit() cls 

(由PeetersOnline通过search“Powershelllogin用户”提供)

 function Get-MyLoggedOnUsers { param([string]$Computer) Get-WmiObject Win32_LoggedOnUser -ComputerName $Computer | Select Antecedent -Unique | %{“{0}{1}” -f $_.Antecedent.ToString().Split('”')[1], $_.Antecedent.ToString().Split('”')[3]} } 

我没有这个代码,但是我通过查看terminal服务会话在VBScript中做了类似的事情。 作为VBScript和无法访问.NET框架,我不得不做一个黑客到达那里。 (我想我看着explorer.exe的会话ID …)

Powershell可以访问.NET框架,所以你可以在MSDN中寻求terminal服务的帮助。