我正在尝试将powershell的结果导出到csv,其中$ server作为第一列,而online / offline作为第二列。我想实际定义许多列。我认为这是一个好的开始。请让我知道如何实现这一目标。
$ErrorActionPreference = 'SilentlyContinue'
$ServerName = "TDL498", "TDL498123", "TDL498456"
foreach ($Server in $ServerName)
{
if (test-Connection -ComputerName $Server -Count 2 -Quiet)
{
write-Host "$Server Online" -ForegroundColor Green
}
else
{
Write-Host "$Server Offline"
}
}
答案 0 :(得分:1)
您可以通过制作PSCustomObject来实现。据我所知,[ grin ]不需要使用Test-Connection
cmdlet设置任何特殊的错误处理,因为它会返回False/True
用于与{ {1}}参数。
-Quiet
输出到屏幕...
$ComputerNameList = @(
'LocalHost'
'10.0.0.1'
'127.0.0.1'
'BetterNotBeThere'
$env:COMPUTERNAME
)
$Results = foreach ($CNL_Item in $ComputerNameList)
{
[PSCustomObject]@{
ComputerName = $CNL_Item
Online = Test-Connection -ComputerName $CNL_Item -Count 1 -Quiet
}
}
# on screen
$Results
# to csv file
$Results |
Export-Csv -LiteralPath "$env:TEMP\MuhammadSuhailAsrulsani_ComputerStatus.csv" -NoTypeInformation
CSV文件内容...
ComputerName Online
------------ ------
LocalHost True
10.0.0.1 False
127.0.0.1 True
BetterNotBeThere False
[MySysName] True