2017-09-22 28 views
0

我想測試多個計算機的列表上的多個條件。Powershell測試多個計算機的多個條件

我有一個文件: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測試以查看計算機是否響應ping命令,abc.com查詢abc.com區域nslookup樣式,xyz.com查詢xyz.com nslookup樣式。

其目的是不僅確定計算機是否處於活動狀態,還會確定是否存在過時的計算機對象或不活動的計算機的dns記錄。

我知道如何單獨使用get-content foreach-object循環測試這些條件 - 但我真的想檢查所有條件並將它們導出電子表格樣式(我顯示它是製表符分隔的,但export-csv是罰款)

+0

你不必與您的代碼的問題。所以我們不能幫你。如果你搜索一個寫你的腳本,僱傭一個程序員。 – guiwhatsthat

+0

我相信他知道如何編寫基本的導入和測試函數,但他不知道如何以期望的格式導出結果,我會寫出一些東西,@davehahn能不能給這個問題添加一些現有的編碼,向我們展示你到目前爲止所嘗試過的? – SteloNLD

回答

0

要獲得所需的格式,您可以使用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