【发布时间】:2016-02-21 20:46:12
【问题描述】:
我有一个 powershell 命令从远程 Windows 服务器获取事件查看器日志并将其放入 csv 文件中,然后显示它并将其添加到数据库(sql server)中
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 以将其正确输入数据库,要么需要更好的方法。我面临的主要问题是事件的消息部分位于多行
提前感谢您的帮助
【问题讨论】:
标签: java sql-server powershell windows-server-2012-r2 event-viewer