get-content 'C:\assets.txt' | % {
$computer = $_
. 'c:\PSTools\PsLoggedon.exe' -accepteula -l -x \\$Computer 3>$null |
? {$_ -match '^\s{2,}((?<domain>\w )\\(?<user>\S ))'} |
Select-Object `
@{n='Computer';e={$Computer}},
@{n='Domain';e={$matches.Domain}},
@{n='User';e={$Matches.User}} |
? user -notmatch '^Connecting$|^Users$|^NT$'
}
這是我用來獲取所有當前登錄的計算機的方法。有沒有辦法可以將它與 Get-ADUser 結合起來,這樣我就可以直接從 AD 而不是從 txt 檔案中提取?
uj5u.com熱心網友回復:
? 抱歉,目前您無法將此“Psloggedon.exe”實用程式與活動目錄命令(即“Get-AdUser”)集成。但是您可以通過執行以下powershell功能遠程檢索網路中不同計算機上當前登錄用戶的詳細資訊:-
‘ 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
}
} ‘
上面的腳本將為您提供網路中多個計算機系統上當前登錄的用戶,這些用戶將代替“計算機名稱”,如下所示。請注意,當將上述腳本用于多個計算機系統時,您必須給出一個以逗號分隔的計算機串列。
如果您的環境中有 AD,那么您可以檢查域控制器日志以查看 Active Directory 用戶帳戶何時登錄,它還會告訴計算機該用戶已登錄。有關更多資訊,請參閱以下鏈接:-

此外,請找到以下鏈接以獲取有關上述內容的更多資訊和參考:-
https://4sysops.com/archives/how-to-find-a-logged-in-user-remotely-using-powershell/
用于查看當前登錄用戶(域和機器) 狀態(活動、空閑、離開)的 Powershell 腳本
uj5u.com熱心網友回復:
您將使用 PowerShell 的Get-ADComputer來完成這項作業,而不是Get-ADUser。這是一個腳本,可以為您完成所有這些作業。下面主要是從這里的公共領域中提取出來的,只是略有修改。它將所有 AD 域計算機拉入并通過管道傳送到 C:\Computers.txt,然后 PS-remotes 進入該串列中的每臺計算機以查找登錄的互動式用戶及其上次登錄日期。以漂亮的表格格式為您提供名為 C:\LoggedOnResults.txt 的報告檔案。
# Finds and pipes all AD domain computers into Computers.txt, then PS-remotes into each computer in the list to find the logged in, interactive user, and their last login date. Generates a report file named C:\LoggedOnResults.txt, in a nice tabled format.
# Deletes the current file C:\Computers.txt (if it exists)
$FileName = "C:\Computers.txt"
if (Test-Path $FileName) {
Remove-Item $FileName
write-host "$FileName has been deleted"
}
else {
Write-host "$FileName doesn't exist"
}
# 0. Capture all AD computers into a text file named Computers.txt
# importing dependancy, assuming it's already installed.
# Install RSAT for Windows workstation, AD DS role for Windows Server if missing
Import-Module "ActiveDirectory"
Get-ADComputer -Filter {(OperatingSystem -like "*windows*") -and (Enabled -eq "True")} | Select -Expand Name | Out-File "C:\Computers.txt"
# 1. Create scriptblock to target computer will execute
$SB = {
$explorerprocesses = @(Get-WmiObject -Query "Select * FROM Win32_Process WHERE Name='explorer.exe'" -ErrorAction SilentlyContinue)
if ($explorerprocesses.Count -eq 0) {
New-Object -TypeName PSObject -Property @{
ComputerName = $env:COMPUTERNAME;
Username = [string]::Empty
LoggedOnSince = [string]::Empty
}
} else {
foreach ($i in $explorerprocesses) {
$Username = $i.GetOwner().User
$Domain = $i.GetOwner().Domain
New-Object -TypeName PSObject -Property @{
ComputerName = $env:COMPUTERNAME ;
Username = '{0}\{1}' -f $Domain,$Username ;
LoggedOnSince = ($i.ConvertToDateTime($i.CreationDate)) ;
}
}
}
} # endof scriptblock
# 2. Create an empty array to store results
$results = @()
# 3. Query target computers using PSRemoting
Get-content "C:\Computers.txt" | ForEach-Object -Process {
$computer = $_
try {
$results = Invoke-Command -ComputerName $Computer -ScriptBlock $SB -ErrorAction Stop
} catch {
Write-Warning -Message "Faild to use PSremoting on $Computer because $($_.Exception.Message)"
}
}
# 4. Display the results
$results | Select ComputerName,Username,LoggedOnSince | ft -AutoSize
# 5. Send results to a text file
$results | Select ComputerName,Username,LoggedOnSince | ft -AutoSize | Out-File -FilePath "C:\LoggedOnResults.txt"
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/401124.html
上一篇:動態引數的最簡單形式是什么?
