2014-10-29 26 views
0

我試圖禁用計算機帳戶,然後將成功/失敗結果導出到文件。但我試過export-csv,out-file和其他方法。結果都是空文件。任何線索?如何從Powershell導出屏幕詳細結果

謝謝!

$MyDN= (Get-ADComputer testmachine).DistinguishedName 
Disable-ADAccount -Identity $MyDN -Verbose |Out-File c:\result.txt 

回答

0

直接從about_Redirection

Operator Description    Example 
    -------- ----------------------  ------------------------------ 
    4>  Sends verbose output to Import-Module * -Verbose 4> Verbose.txt 
      the specified file. 

    4>>  Appends verbose output  Import-Module * -Verbose 4>> Save-Verbose.txt 
      to the contents of the 
      specified file. 

    4>&1  Sends verbose output (4) Import-Module * -Verbose 4>&1 
      and success output (1)  
      to the success output 
      stream.    

編輯:你可能要考慮使用-PassThrough說法,或者如果沒有這些選項給你什麼你正在尋找一個可能的try /抓住可以提供它。

+0

第二次編輯。禁用帳戶不具有輸出。只是錯誤是錯的。它甚至不會告訴你賬戶是否已被禁用。 – Matt 2014-10-29 23:40:40

0

檢查帳戶實際上是禁用,結果寫入文件:

$MyDN= (Get-ADComputer testmachine).DistinguishedName 
Disable-ADAccount -Identity $MyDN 

$comp = Get-ADComputer -Identity $MyDN -Properties Enabled 
"{0} disabled: {1}" -f $comp.Name, (!$comp.Enabled) | Out-File 'C:\result.txt' 

-f是PowerShell的格式運算符。它需要一個字符串數組(或可以轉換爲字符串的對象),並用它們替換格式字符串中的佔位符(基本上像Python的%運算符)。

<format string with placeholders> -f <string>, <string>, ... 

佔位符是在花括號零基編號({0}{1},...)與每個數參考與替換字符串數組的索引。這樣,你可以在格式字符串的多個位置插入相同的字符串:

PS C:\>"{0}: {1}" -f 'foo', 'bar' 
foo: bar 
PS C:\>"{0}: {1}-{0}" -f 'foo', 'bar' 
foo: bar-foo

格式字符串,您還可以通過添加改性劑的佔位符,例如格式化以特定的方法參數用於打印的數字爲十六進制數字,貨幣或百分比值:

PS C:\>"0x{0:X}" -f 255 
0xFF 
PS C:\>"{0:p}" -f 0.23 
23.00 % 
PS C:\>"{0:c}" -f 42 
$ 42.00

格式化小數:

PS C:\>"{0:n4}" -f (1/7) 
0.1429

或對準輸出:

PS C:\>"{0,10:n2}`n{1,10:n2}`n{2,10:n2}" -f 13.5, 1402, 5.1491 
    13.50 
    1,402.00 
     5.15

參見here對於進一步的信息,與此格式字符串運營商。

+0

對不起,我還是個初學者,你能否點燃這句話的格式用法? 「{0}已禁用:{1}」-f $ comp.Name,(!$ comp。啓用) – ray 2014-10-30 03:38:53

+0

@ray查看更新的答案。 – 2014-10-30 09:48:27

0

儘管從Disable-Account有詳細的輸出它不提供您正在尋找的答案。一個簡單的try/catch塊可能會解決這個問題

$accounts = "jpilot","notexist" 
$results = @() 

ForEach($account in $accounts){ 
    try{ 
     Disable-ADAccount $user -ErrorAction Stop 
     $results += "$user was disabled" 
    } catch { 
     $results += "Unable to disable $($user): $($_.Exception.Message)" 
    } 
} 

$results | Out-File c:\result.txt -Encoding Ascii 

基本上嘗試和禁用建立結果數組的帳戶,因爲你去。如果沒有錯誤,您可以假定它被禁用。如果你真的想確保你可以再次檢查它Get-AduserGet-AdComputer但這似乎是多餘的。 -ErrorAction Stop將導致任何失敗被catch捕獲。如果是這種情況,則輸出故障原因,以便以後檢查。

相關問題