java - SBS2008 上的数据截断

标签 java powershell truncate

我正在尝试从两台 Windows 计算机的事件日志中读取事件。一种是 Windows 10 Enterprise(运行完美),一种是 Small Business Server 2008。在 SBS2008 计算机上,输出在第 78 个字符处被截断,但我不明白为什么。

这是我正在运行的 powershell 命令:

get-eventlog Application -After (Get-Date).AddDays(-10)  |
Where-Object {$_.EntryType  -like 'Error' -or $_.EntryType -like
'Information'} |  Format-Table  -Property TimeGenerated, Index,
EventID, EntryType, Source, Message -autosize | Out-String -Width 4000

它在两台机器上的 powershell 编辑窗口中都表现良好,没有截断。 如果我使用 powershell -file GetEvents.ps1 在命令窗口中运行它,输出将被截断: C:\BITS>powershell -file GetEvents.ps1 enter image description here

我无法弄清楚这是做什么的。这是Java代码:

String command = "powershell.exe  " + Cmd;
Process powerShellProcess = Runtime.getRuntime().exec(command);
powerShellProcess.getOutputStream().close();
String line;
BufferedReader stdout = new BufferedReader(new InputStreamReader(powerShellProcess.getInputStream()));
while ((line = stdout.readLine()) != null) {
    if (line.length() > 0) {
        System.out.println(line);
    }
}

任何人都可以建议一种更好的方法来从日志中获取事件,或者建议我如何读取 powershell 脚本的输出而不被回车符打断?这非常令人失望,因为我在我的机器(W10 机器)上对其进行了广泛的测试,却发现它在 SBS2008 客户机器上失败了! 我检查了不同机器上使用的库和java版本,它们是相同的。这不是 println 语句,因为我在最后的“While” block 中对字符串进行了一些解析,并且传入的行肯定被截断了。

我随后尝试使用 Get-winevent,但是当我在命令上放置 filterhashtable 标志时,在 SBS2008 上运行时会失败,并显示“参数不正确”(在 W10 上工作正常)。 我的理想是能够从给定 eventID 以来的特定日志文件中获取所有事件(因为我可以存储它),但似乎几乎不可能在所有 Windows 操作系统上以相同的格式获得相同的输出。欢迎任何建议。

最佳答案

SBS2008的答案就在这里; http://www.mcbsys.com/blog/2011/04/powershell-get-winevent-vs-get-eventlog/ SBS2008不能使用hashtable,必须使用filterxml。不幸的是,当我在 SBS2008 上使用 filterxml 时,它不会返回错误消息,其他所有内容,只是没有消息。这是使用从事件查看器 ( https://blogs.msdn.microsoft.com/powershell/2011/04/14/using-get-winevent-filterxml-to-process-windows-events/ ) 剪切和粘贴 XML 查询的规定方法。 经过更多研究后,我想出了一个脚本,它可以(某种程度上)实现我想要的功能。它缺少事件索引(这是一个遗憾),但它始终从系统和应用程序事件日志中返回事件:

$fx = '<QueryList>
  <Query Id="0" Path="Application">
    <Select Path="Application">*[System[(Level=1  or Level=2 or Level=3) and TimeCreated[timediff(@SystemTime) &lt;= 43200000]]]</Select>
  </Query>
</QueryList>'
function Set-Culture([System.Globalization.CultureInfo] $culture) { [System.Threading.Thread]::CurrentThread.CurrentUICulture = $culture ; [System.Threading.Thread]::CurrentThread.CurrentCulture = $culture } ; Set-Culture en-US ; get-winevent -FilterXml $fx | out-string -width 470

$fx = '<QueryList>
  <Query Id="0" Path="System">
    <Select Path="System">*[System[(Level=1  or Level=2 or Level=3) and TimeCreated[timediff(@SystemTime) &lt;= 43200000]]]</Select>
  </Query>
</QueryList>'
Set-Culture en-US ; get-winevent -FilterXml $fx | out-string -width 470

我希望这对其他人有用!

关于java - SBS2008 上的数据截断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41508884/

相关文章:

PowerShell 删除除根目录中的一个文件和子文件夹中的一个文件之外的所有其他文件

powershell - Azure Powershell 用于停止订阅中的 VM

数字的 C++ 精度和 fstream 的截断

string - 如何在golang中椭圆截断文本?

javascript - vue-truncate-filter 不过滤并抛出错误

java - 开始使用 Java 开发 Web 应用程序需要学习哪些知识?

java - 协助编写 Java 中对数组列表求和的递归函数

java - Play 2 JPA Oracle - 即使 ebeanEnabled 在 build.scala 中设置为 false,也会出现额外列 'ebean_intercept'

java - 如何在字符串中查找第 n 个出现的字符?

powershell Foreach 对象