【发布时间】:2014-06-11 06:52:51
【问题描述】:
我目前的任务是找出用户在我们的系统中登录了哪些桌面。我对 Powershell 还很陌生,所以对于很长的帖子和凌乱的脚本,我深表歉意
有一个登录脚本可以在用户登录时将此信息放入日志文件中。如下图。
Script started: 1/01/2010 00:00:00
Information : Script Ver: 1.05
Information : LOGONSERVER: %servername%
Information : USERNAME: %username%
Information : COMPUTERNAME: %computername%
Information : COMPUTER SITE: $location%
Information : OS Version: %system%
Information : Domain: %domain%
我需要同时获取“Script started: 1/01/2010 00:00:00”和“Information : COMPUTERNAME: %computername%”,然后将这些部分导出到 CSV 文件中。我可以使用
获取信息$Str ComputerName = Get-Content -Path %log%| Select-String "COMPUTERNAME:"
对于多个用户,这只会让我最后一台计算机登录到日志文件中的并非所有桌面。有没有办法获取所有计算机名称和脚本启动并将它们导出到单独列中的 CSV 文件中?也许是一种更简单、更短的方法。
Foreach ($ObjCSVline in $(Import-csv "\\tgfilp05\hdact\DFP\Nat\Scripts\LastLogon\LastLogon.csv"))
{
$strUsername = $ObjCSVline.Lastlogon
try {
$arrUserIdOutput = Get-ADUser $strUsername -Properties homedirectory | select *
} catch {
Write-Host "User: " $strUsername" Does not exist"
}
$strUserHomeDirectory = $arrUserIdOutput.HomeDirectory
$strUserLogs = $strUserHomeDirectory + $strLogFile
if ((Test-Path $strUserLogs) -eq $true) {
$strLogs = Get-Content $strUserLogs | Select-String "Script started: " | Select-Object -Last 1
$Time = $strLogs -replace "Script started: ",""
$StrComp = Get-Content $strUserLogs | Select-String "Information : COMPUTERNAME: " | Select-Object -Last 1
$computerID = $StrComp -replace "Information : COMPUTERNAME: ",""
} else { $strUserLogs = $strUserLogs + $strOldLogFile
if ((Test-Path $strUserLogs) -eq $true) {
$strLogs = Get-Content $strUserLogs | Select-String "Script started: " | Select-Object -Last 1
$Time = $strLogs -replace "Script started: ",""
} else
{
Write-Host "unable to find log file for" $strUsername
}
}
$objOutput.desktop = $computerID
$objOutput.TimeOutput = $Time
$objOutput.Username = $strUsername
$objOutput | Select-Object Username,TimeOutput,desktop | Export-Csv "\\tgfilp05\hdact\DFP\Nat\Scripts\LastLogon\LastLogonDetails.csv" -Append -NoTypeInformation
}
【问题讨论】:
-
我不禁认为这更好地以完全不同的方式记录和查询。当人们登录时,Active Directory 无法登录,从哪里登录?然后您将查询该日志,而不是解析文本文件。
标签: powershell