0
我有一個powershell命令從遠程Windows服務器採取事件查看器日誌,並將其放入一個csv文件,然後顯示它,並另外添加到數據庫(sql服務器)導入自定義Windows事件查看器日誌形式csv到sql表
String command = "powershell.exe cd 'Path\\Output'; "
+ "$password = get-content 'Path\\Output\\"
+ serverClicked + "' | convertto-securestring; $username = 'administrator'; "
+ "$cred= New-Object System.Management.Automation.PSCredential($username, $password); "
+ "Invoke-Command -ComputerName " + serverClicked + " -Credential $cred -Authentication Default -ScriptBlock "
+ "{ Get-EventLog -LogName " + logName + " -EntryType " + entryType + " -Newest 50 | Select-Object -Property PSComputerName, TimeGenerated, EventID, Message, Source, EntryType}"
+ " | Out-File " + serverClicked + "." + logName + entryType + "." + curDate + ".csv; cat " + serverClicked + "." + logName + entryType + "." + curDate + ".csv";
System.out.println(command);
Process powerShellProcess;
powerShellProcess = Runtime.getRuntime().exec(command);
powerShellProcess.getOutputStream().close();
String line;
System.out.println("Output:");
BufferedReader stdout = new BufferedReader(new InputStreamReader(powerShellProcess.getInputStream()));
ShowInfoTextArea.setText("\n");
//------------------------------------
//Going to try adding the data to the database
DBConnect d = new DBConnect();
Connection con = d.getConnection();
Statement stmt = con.createStatement();
stmt.executeQuery("INSERT INTO EventViewer(TimeGenerated, EventID, Message, Source, EntryType, PSComputerName) VALUES ('" + timeGenerated + "', '" + eventID + "', '" + message + "', '" + source + "', '" + entryType2 + "', '" + pSComputerName + "');");
System.out.println("INSERT INTO EventViewer(TimeGenerated, EventID, Message, Source, EntryType, PSComputerName) VALUES ('" + timeGenerated + "', '" + eventID + "', '" + message + "', '" + source + "', '" + entryType2 + "', '" + pSComputerName + "');");
stmt.close();
stmt.close();
con.close();
//------------------------------------
此代碼運行PowerShell命令,並採取輸出到CSV文件,然後我需要的是輸出,放入單獨的字段到數據庫設置爲:
[TimeGenerated] [varchar](50) NOT NULL,
[EventID] [varchar](50) NOT NULL,
[Message] [varchar](500) NOT NULL,
[Source] [varchar](50) NOT NULL,
[EntryType] [varchar](50) NOT NULL,
[PSComputerName] [varchar](50) NOT NULL,
[LogName] [varchar] (50) NOT NULL
任我需要弄清楚Java獲取它正確地進入數據庫或更好的方式。我所面臨的主要問題是該事件的消息部分是多條線路上
感謝您的幫助提前
非常感謝你回答這個問題。我將在小時內檢查並調試。再次感謝。 – Josh
這工作修復未格式化的CSV文件,我現在的主要問題,並回到原來的是,我如何採取該CSV和輸入字段到一個SQL表,我不介意重新創建該SQL表,我只想每個事件在sql表中。請幫助我這 – Josh
並感謝您的格式化修復 – Josh