我目前已被分配了查找用户在我们的系统中登录的桌面的任务。我是相当新的PowerShell的,所以我在很长的职位和凌乱的脚本从文本文件修剪并导出到CSV 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%
我需要同时获得“启动脚本:2010年1月1日00:00:00”和“信息:计算机:%计算机%”,然后将这些零部件出口到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无法登录,并从何处登录?然后你会查询该日志,而不是解析文本文件。 – alroc