PowerShell脚本,可在使用import-csv时将完整输出写入文件

时间:2019-12-10 20:36:40

标签: export-to-csv import-csv

我正在尝试修改我发现的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

0 个答案:

没有答案