如果允许我提出后续问题,Wuld 会使用旧问题:
我的问题与此处相同:Get Logged on Users from a List of Computer Names答案很好,但我不知道输入的computers.csv 应该如何格式化。
如果允许我提出后续问题,Wuld 会使用旧问题:
我的问题与此处相同:Get Logged on Users from a List of Computer Names答案很好,但我不知道输入的computers.csv 应该如何格式化。
它(csv 文件)的格式应如下所示:
computername
server1
server2
server3
“计算机名”是列名。这可以从代码片段中派生出来,因为 Foreach-Object 指的是 ComputerName 属性。
尝试这个
function Get-MyLoggedOnUsers
{
param([Array]$Computer)
Get-WmiObject Win32_LoggedOnUser -ComputerName $Computer |
Select __SERVER, Antecedent -Unique |
%{"{0} : {1}\{2}" -f $_.__SERVER, $_.Antecedent.ToString().Split('"')[1], $_.Antecedent.ToString().Split('"')[3]}
}
$Computers = gc C:\Temp\Computers.txt
Get-MyLoggedOnUsers $Computers
-或者-
我找到了这个并根据我的需要对其进行了修改
<#
.Synopsis
Queries a computer to check for interactive sessions
.DESCRIPTION
This script takes the output from the quser program and parses this to PowerShell objects
.NOTES
Name: Get-LoggedOnUser
Author: Jaap Brasser
Version: 1.1
DateUpdated: 2013-06-26
.LINK
http://www.jaapbrasser.com
.PARAMETER ComputerName
The string or array of string for which a query will be executed
.EXAMPLE
.\Get-LoggedOnUser.ps1 -ComputerName server01,server02
Description:
Will display the session information on server01 and server02
.EXAMPLE
'server01','server02' | .\Get-LoggedOnUser.ps1
Description:
Will display the session information on server01 and server02
#>
param(
[CmdletBinding()]
[Parameter(ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[array[]]$ComputerName = (gc C:\Temp\TSRA.txt)
)
process {
foreach ($Computer in $ComputerName) {
quser /server:$Computer | Select-Object -Skip 1 | ForEach-Object {
$CurrentLine = $_.Trim() -Replace '\s+',' ' -Split '\s'
$HashProps = @{
UserName = $CurrentLine[0]
ComputerName = $Computer
}
# If session is disconnected different fields will be selected
if ($CurrentLine[2] -eq 'Disc') {
$HashProps.SessionName = $null
$HashProps.Id = $CurrentLine[1]
$HashProps.State = $CurrentLine[2]
$HashProps.IdleTime = $CurrentLine[3]
$HashProps.LogonTime = $CurrentLine[4..6] -join ' '
} else {
$HashProps.SessionName = $CurrentLine[1]
$HashProps.Id = $CurrentLine[2]
$HashProps.State = $CurrentLine[3]
$HashProps.IdleTime = $CurrentLine[4]
$HashProps.LogonTime = $CurrentLine[5..7] -join ' '
}
New-Object -TypeName PSCustomObject -Property $HashProps |
Select-Object -Property UserName,ComputerName,SessionName,Id,State,IdleTime,LogonTime
} | Out-GridView -Title "Users Logged in $Computer"
}
}
好的...我知道这个问题是一个较老的问题,但我最近遇到了这个确切的难题,我必须从已登录到该域上所有 WORKSTATIONS 的特定域中提取所有用户。这是我从我发现并进行了相当广泛的修改的一段代码中得出的。该脚本从 Workstation.CSV 文件中读取工作站列表,并输出到格式良好的 .csv 文件,该文件可以用 excel 打开。
#
# ########################################################################################
# Script Execution is as follows
# .\LoggedOn_All.ps1
# ########################################################################################
# Script generates a list ofComputers from Active Directory and
# pulls listing of all users that have logged in to those Workstations
# Before running script, rename or delete the following files
# Workstations.csv
# Worksation_Logged_On_Users.csv
# NotPingable.csv
##########################################################################################
#Generate a listing of Workstations from Active Directory
#While this command can be run inside the script, I comment it out and copy it to powershell
#so that I can verify the contents of the file before running the script
Get-ADComputer -Filter * -SearchBase "<DISTINGUISHED NAME of the OU that contains your Workstations>" | Select -Exp Name >> Workstations.csv
#Set workstation list file (get list of workstations from file)
$list = Get-Content "<put full path here> Workstations.csv"
#Set Output file
$OUTPUT = "<put full path here> Worksation_Logged_On_Users.csv"
#set headers in CSV output file
echo "Workstation Name,User,LogonTime" | Out-File -Encoding ascii -FilePath $OUTPUT
#begin script logic
$list | ForEach-Object {
echo "Current computer $_"
#Test if system is online
Invoke-Expression "ping -n 1 -w 500 $_" | Out-Null
$ping = $LASTEXITCODE
#If Pingable perform the following collection actions
if (!$ping){
#Retrieve listings of ALL users that have logged on to the Workstation/Laptop and send results to Output File
$NetLogs = Get-WmiObject Win32_NetworkLoginProfile -ComputerName $_
foreach ($NetLog in $NetLogs) {
if ($NetLog.Name -match "<DOMAIN>*" -AND $NetLog.LastLogon -match "(\d{14})") {
$row = "" | Select Name,LogonTime
$row.Name = $NetLog.Name
$row.LogonTime=[datetime]::ParseExact($matches[0], "yyyyMMddHHmmss", $null)
foreach ($RName in $row) {
$USR = $row.Name
$LTime = $row.LogonTime
#Write "Workstation Name, User, and TimeStamp" results to Output File
echo "$_,$USR,$LTime" >> $OUTPUT }
}
}
} else {
#If System is not pingable, the System Name is written to the NotPingable.csv file
echo "$_ not pingable"
echo "$_" >> NotPingable.csv
}
}