我正在尝试修改我发现的Powershell脚本。该脚本在导入包含主机名,端口以运行测试的csv时执行Test-NetConnection。原始脚本仅输出端口是否打开。我希望它导出完整的结果。以下是我到目前为止的内容:
[CmdletBinding()]
Param(
[Parameter(
Mandatory = $true,
Position = 1)]
[ValidateScript({
Test-Path $_ -PathType Leaf
})]
[string]$Path,
)
$checkList=Import-Csv -Path $Path -Delimiter "," -ErrorAction Stop
$checkList| ForEach-Object {
try{
if (Test-NetConnection -ComputerName $_.HostName -Port $_.RemotePort -InformationLevel Quiet -ErrorAction Stop){
$_.Open=$true
}else{
$_.Open=$false
}
}
catch{
#Nothing we can do because the -ErrorAction is ignored
}
finally{
#Nothing currently set
}
}
$outputObject=Get-Item $Path
#Build file name for the report
$exportPath= Join-Path -Path $outputObject.DirectoryName -ChildPath $("Report_"+ $outputObject.BaseName + $outputObject.Extension)
$checklist| Out-File -FilePath $exportPath