需要将系统数据和时间作为列添加到我的 Powershell Test-NetConnection 输出中 [英] Need To Add System Data and Time to My Powershell Test-NetConnection Output as a Column

查看:35
本文介绍了需要将系统数据和时间作为列添加到我的 Powershell Test-NetConnection 输出中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我当前的脚本(附在下面),我需要添加一个额外的列作为时间",它捕获当前系统日期和时间以及脚本已经捕获的输出.如何获得所需的输出(如链接中附加的图像所示).因为我稍后会将数据导出到 Excel 表.

For my current script (appended below), I need to add an extra column as 'Time' which captures the current system date and time along with the output which the script is already capturing. How can get the desired output (as shown in the image appended in the link). As I will be exporting the data to an excel sheet later on.

我的当前输出 VS 期望输出

My Current Output VS Desired Output

脚本:

$Target = '192.168.1.19', 'ww.google.com'

$outFilePath = 'Desktop\Ping1.txt'

$result=@()

#$getdate = Get-Date -Format "dd/MM/yyyy HH:mm"

#$getdate| Out-File $outFilePath -Encoding utf8 -Append
foreach ($t in $Target)

{



$a=Test-NetConnection -ComputerName $t -Port 5895 -WarningAction SilentlyContinue

$result+=New-Object -TypeName PSObject -Property ([ordered]@{
'Target'=$a.ComputerName;
'RemoteAddress'=$a.RemoteAddress;
'Port'=$a.RemotePort;
'Status'=$a.PingSucceeded 

})

}



$result |  Out-File $outFilePath -Encoding utf8 -Append

推荐答案

如果您指定日期格式为:dd/MM/yyyy,斜线将被识别为日期分隔符并替换为日期分隔符您当前的文化设置.就我而言,它会返回 06-04-2020

If you specify date format as: dd/MM/yyyy, slash will be recognized as date separator and replaced by date separator from your current culture settings. In my case it would return 06-04-2020

要避免这种行为,请转义分隔符.将此添加到用于生成其他属性的哈希表:

To circumvent this behaviour, escape the separators. Add this to hashtable you use for generating other properties:

Time = (get-date -Format 'dd\/MM\/yyyy hh:mm')

这篇关于需要将系统数据和时间作为列添加到我的 Powershell Test-NetConnection 输出中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆