【问题标题】:Powershell test multiple computer multiple conditionsPowershell测试多台电脑多条件
【发布时间】:2017-09-22 04:45:59
【问题描述】:

我想在多台计算机的列表上测试多个条件。

我有一个文件:computers.txt,其中包含:

Computer1
Computer2
Computer3
Computer4

我想要的是一个如下所示的输出文件:

COMPUTER  AD-COMPUTER_OBJECT  PING   abc.com    xyz.com
Computer1 True                True   A Record   NULL
Computer2 True                True   NULL       CNAME Record
Computer3 False               True   A Record   NULL
Computer4 False               False  NULL       NULL

其中AD-COMPUTER_OBJECT使用get-adcomputer测试看AD中是否有电脑对象,PING测试使用test-connection看电脑是否响应ping,abc.com查询abc.com zone nslookup样式, xyz.com 查询 xyz.com nslookup 样式。

目的不仅是确定计算机是否处于活动状态,而且还确定是否存在陈旧的计算机对象或非活动计算机的 dns 记录。

我知道如何使用 get-content foreach-object 循环单独测试这些条件 - 但我真的很想检查所有条件并将它们导出为电子表格样式(我将其显示为制表符分隔但 export-csv 很好)

【问题讨论】:

  • 您的代码没有问题。所以我们帮不了你。如果您搜索为您编写脚本的人,请聘请程序员。
  • 我相信他知道如何编写基本的导入和测试功能,但他不知道如何以所需的格式导出结果,我会写一些东西,@davehahn 可以添加一些这个问题的现有编码并向我们展示你到目前为止所做的尝试?

标签: powershell


【解决方案1】:

要获得所需的格式,您可以使用 select-object 这允许您定义自己的列并将其导出到例如 csv。

function test-condition {
    return 'condition-value'
}


$Computerlist = @("pc1", "pc2", "pc3")
$Results = @()

Foreach ($Computer in $Computerlist)
{

    $con1 = test-condition
    $con2 = test-condition
    $con3 = test-condition
    $con4 = test-condition


    $Results += $Computer | Select @{N='COMPUTER'; E={$_}}, @{N="Condition 1"; E={$con1}}, @{N="Condition 2"; E={$con2}}, @{N="Condition 3"; E={$con3}}, @{N="Condition 4"; E={$con4}}

}

#In the NL Excel expects the delimiter to be ';'.
$Results | export-csv -Delimiter ';' -NoTypeInformation "$env:USERPROFILE\Desktop\results.csv"

#Format-Table works to
$Results | Format-Table

格式表输出:

COMPUTER                  Condition 1               Condition 2              Condition 3              Condition 4             
--------                  -----------               -----------              -----------              -----------             
pc1                       condition-value           condition-value          condition-value          condition-value         
pc2                       condition-value           condition-value          condition-value          condition-value         
pc3                       condition-value           condition-value          condition-value          condition-value         

【讨论】:

    猜你喜欢
    • 2018-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-02
    相关资源
    最近更新 更多