RunSpacePool输出CSV包含空行

时间:2016-03-19 23:53:23

标签: csv powershell runspace

我正在使用here并让RunSpacePools输出一个CSV文件,但我的CSV文件有空白行,而我无法弄清楚空白行的来源。

空白行在记事本中显示为// What should happen (this is when catch contains input.next() rather than nextLine) /* Please enter a month in numeric form 8 Please enter a day in numeric form 2 Please enter a two-digit year badinput java.util.InputMismatchException One of your inputs was not valid. badinput Please enter a month in numeric form <------------- prompts for input, as expected */ // What happens when I have nextLine in the catch block (code above) /* Please enter a month in numeric form 8 Please enter a day in numeric form 2 Please enter a two-digit year badinput java.util.InputMismatchException One of your inputs was not valid. <------------ leftover newline printed, as expected Please enter a month in numeric form <---------------- does not prompt for input due to another leftover newline (why?) java.util.InputMismatchException One of your inputs was not valid. badinput <----------------------- prints badinput even though it should've been consumed on the last iteration (why?) Please enter a month in numeric form */

,,,

1 个答案:

答案 0 :(得分:0)

经过反复试验,我最终找到了工作with this method of run space pools。仔细观察,我发现输出被WMI的额外空格污染了。

为了解决这个问题,我最终在ScriptBlock的Try语句中使用了以下内容。

$LastReboot = [Management.ManagementDateTimeConverter]::ToDateTime `
($operatingSystem.LastBootUpTime).ToString().Trim()

现在返回的数据都是所需的单行。

-Edit评论WMI在输出中的额外空格。 See this question for more details

请考虑以下方法返回计算机的上次重新启动时间戳。请注意,您可以根据需要格式化字符串see this library page for more info

$os = (gwmi -Class win32_operatingsystem).LastBootUpTime
[Management.ManagementDateTimeConverter]::ToDateTime($os)

Whitespaces in WMI output in PowerShell

观察空格,可以通过将输出转换为字符串然后使用Trim()删除空格来删除空格。

WMI output without extra WhiteSpaces