1. 程式人生 > 實用技巧 >powershell命令查詢域使用者所登入的計算機

powershell命令查詢域使用者所登入的計算機

1.在C:\Program Files\WindowsPowerShell\Modules\Get-UserLogon,新建Get-UserLogon資料夾,並新建Get-UserLogon.psm1檔案內容如下

function Get-UserLogon {
 
[CmdletBinding()]
 
param
 
(
 
[Parameter ()]
[String]$Computer,
 
[Parameter ()]
[String]$OU,
 
[Parameter ()]
[Switch]$All
 
)
 
$ErrorActionPreference="SilentlyContinue"
 
$result=@()
 
If ($Computer) {
 
Invoke-Command -ComputerName $Computer -ScriptBlock {quser} | Select-Object -Skip 1 | Foreach-Object {
 
$b=$_.trim() -replace '\s+',' ' -replace '>','' -split '\s'
 
If ($b[2] -like 'Disc*') {
 
$array= ([ordered]@{
'User' = $b[0]
'Computer' = $Computer
'Date' = $b[4]
'Time' = $b[5..6] -join ' '
})
 
$result+=New-Object -TypeName PSCustomObject -Property $array
 
}
 
else {
 
$array= ([ordered]@{
'User' = $b[0]
'Computer' = $Computer
'Date' = $b[5]
'Time' = $b[6..7] -join ' '
})
 
$result+=New-Object -TypeName PSCustomObject -Property $array
 
}
}
}
 
If ($OU) {
 
$comp=Get-ADComputer -Filter * -SearchBase "$OU" -Properties operatingsystem
 
$count=$comp.count
 
If ($count -gt 20) {
 
Write-Warning "Search $count computers. This may take some time ... About 4 seconds for each computer"
 
}
 
foreach ($u in $comp) {
 
Invoke-Command -ComputerName $u.Name -ScriptBlock {quser} | Select-Object -Skip 1 | ForEach-Object {
 
$a=$_.trim() -replace '\s+',' ' -replace '>','' -split '\s'
 
If ($a[2] -like '*Disc*') {
 
$array= ([ordered]@{
'User' = $a[0]
'Computer' = $u.Name
'Date' = $a[4]
'Time' = $a[5..6] -join ' '
})
 
$result+=New-Object -TypeName PSCustomObject -Property $array
}
 
else {
 
$array= ([ordered]@{
'User' = $a[0]
'Computer' = $u.Name
'Date' = $a[5]
'Time' = $a[6..7] -join ' '
})
 
$result+=New-Object -TypeName PSCustomObject -Property $array
}
 
}
 
}
}If($All){ $comp=Get-ADComputer-Filter*-Properties operatingsystem $count=$comp.count If($count -gt 20){Write-Warning"Search $count computers. This may take some time ... About 4 seconds for each computer ..."}foreach($u in $comp){Invoke-Command-ComputerName $u.Name-ScriptBlock{quser}|Select-Object
-Skip1|ForEach-Object{ $a=$_.trim()-replace '\s+',' '-replace '>',''-split '\s'If($a[2]-like '*Disc*'){ $array=([ordered]@{'User'= $a[0]'Computer'= $u.Name'Date'= $a[4]'Time'= $a[5..6]-join ' '}) $result+=New-Object-TypeNamePSCustomObject-Property $array }else{ $array=([ordered]@{'User'=
$a[0]'Computer'= $u.Name'Date'= $a[5]'Time'= $a[6..7]-join ' '}) $result+=New-Object-TypeNamePSCustomObject-Property $array }}}}Write-Output $result }指令碼中含有三個引數分別是-computer –all –ou。 -computer引數中是獲取特定電腦的登陸使用者,-ou是搜尋此OU執行中所有計算機的所有登入使用者,最後一個引數-all提供包含前兩個所有的資訊。

Get-userlogon -ou 'OU=test,DC=test,DC=cn'|export-csv E:\computer.csv –NoTypeInformation -encoding "utf8"