我目前已被分配了查找用戶在我們的系統中登錄的桌面的任務。我是相當新的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