powershell命令查詢域使用者所登入的計算機
阿新 • • 發佈:2020-08-18
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