将if / else语句结果输出到格式化表

时间:2019-02-21 12:36:33

标签: powershell

我已经在Powershell中编写了一个环境IP检查脚本,该脚本可以工作,但是我不知道如何在格式化表格中将输出显示到屏幕上,该表格可以自动调整列的大小。

$infile = Import-Csv "~\Env_IPlist.csv" -Delimiter ","
$arr1 = @()

$IP_Addresses = $infile |
                Select-Object "Device_Type", "Device_Name", "IP_Address",
                    "IP_Role", "Location"

Write-Host "Started Ping Test ..."

foreach ($IP in $IP_Addresses) {
    if (Test-Connection $IP.("IP_Address") -Quiet -Count 1 -ErrorAction SilentlyContinue) {
        Write-Host $IP.("Device_Name") ":" $IP.("IP_Address") "Ping successful" -ForegroundColor Green
    } else {
        Write-Host $IP."Device_Name" ":" $IP.("IP_Address") "Ping failed" -ForegroundColor Red -BackgroundColor white
    }
}

Write-Host "Ping Test Completed!"

2 个答案:

答案 0 :(得分:2)

通过计算的属性添加curl https://address --user appID:appPassword -d grant_type=client_credentials -d scope=read -d format=json 结果,然后将输出通过管道传递到private func LoadKey() { let params : Parameters = ["grant_type":"client_credentials", "scope":"read", "format":"json"] let url = "https://address" Alamofire.request(url, method: .post, parameters: params, encoding: URLEncoding.httpBody).response{ response in print(response) } }

<div>
                    <select class="form-control" name="Extension for area validity sought for" onchange="CommonShowHide('txtc1opt2', this, 'States')">
                        <option value="All India">All India</option>
                        <option value="States">States</option>
                    </select>
                    <input type="text" id="txtc1opt2" style="display:none;" name="Extension for area validity sought for details" class="form-control" value="" placeholder="">
                </div>


<script>
    function CommonShowHide(ElementId, element, value) {
        document.getElementById(ElementId).style.display = element.value == value ? 'block' : 'none';
    }
</script>

答案 1 :(得分:0)

我按照最初的建议使用PSObject重写了脚本,但是现在我不知道如何在if..else语句中添加ForegroundColor。

$infile = Import-Csv "~\Env_IPlist.csv" -Delimiter ","
$arr1 = @()

$IP_Addresses = $infile |
                Select-Object Device_Type, Device_Name, IP_Address, IP_Role,
                    Location, Status

foreach ($IP in $IP_Addresses) {
    if (Test-Connection $IP.IP_Address -Quiet -Count 1 -ErrorAction SilentlyContinue) {
        $PSObject = New-Object PSObject -Property @{
            Device_Type = $IP.Device_Type
            Device_Name = $IP.Device_Name
            IP_Address  = $IP.IP_Address
            IP_Role     = $IP.IP_Role
            Location    = $IP.Location
            Status      = "Successful"
        }
    } else {
        $PSObject = New-Object PSObject -Property @{
            Device_Type = $IP.Device_Type
            Device_Name = $IP.Device_Name
            IP_Address  = $IP.IP_Address
            IP_Role     = $IP.IP_Role
            Location    = $IP.Location
            Status      = "Failed"
        }
    }
    $arr1 += $PSObject
}

$arr1 | Format-Table -AutoSize