2014-07-16 165 views
1

我是新來的PowerShell。下面是我正在嘗試寫的一個腳本,簡而言之,它將把AD中的計算機變成一個變量。就拿列表,並通過每一個迭代,並做到以下幾點:PowerShell - 將變量導出到csv文件

  • 測試連接(在線,離線)
  • 測試OSArchitecture
  • 測試的子項
  • 寫出來的電腦,連接狀態,子項值csv文件

每次我嘗試輸出到文件,無論是out-file或export-csv,它都不能正確輸出。它要麼把'長度'和價值或一些長字符串。我最好喜歡導出到csv,以便我可以操縱數據。函數'DoesItemExist'是測試註冊表是否存在的一個函數。下面的代碼:

#find computers names that start with H or D 
$computers=Get-ADComputer -Filter {(name -like "H*") -or (name -like "D*")} -Properties  
Name | select name 
#save path to csv file 
$SaveAs = 'c:\msolhelp\edocs.csv' 
#loop through the computers collection 
foreach($line in $computers) 
{ 
    #test if computer is online 
    if(Test-Connection -Cn $line -BufferSize 16 -Count 1 -ea 0 -quiet) 
    { 
    #write computer name to file 

    #test registry path for 64 bit machine 
    $OS=((Get-WmiObject Win32_Operatingsystem -ComputerName $line).OSArchitecture) 
    if ($OS = '64-bit') 

    #if 64 bit check for correct regkey and grab value and appends to file 
    { 
     $regpath = 'SOFTWARE\Wow6432Node\' 
     #check for subkey and append value to file 
     $val = (DoesItemExist -path $regpath -regEntry "PatchLevel") 
     If ($val) { 
      Get-ItemProperty "HKLM:SOFTWARE\Wow6432Node\" | Select- 
      Object -Property PatchLevel | Export-Csv -Path $SaveAs -Append - 
      NoTypeInformation } 

     else {Get-ItemProperty "HKLM:SOFTWARE\Wow6432Node\" | 
     Select-Object -Property CurrentVersion | Export-Csv -Path $SaveAs -Append - 
     NoTypeInformation} 


    } 

    #if false, it must be a 32 bit machine (different registry path) 
    else 
    { 
     #set path for 32 bit machine 
     $regpath = 'HKLM\SOFTWARE\' 
     #check for correct subkey 
     $val = (DoesItemExist -path $regpath -regEntry "PatchLevel") 
     If ($val) { 
      Get-ItemProperty "HKLM:SOFTWARE\" | Select-Object - 
      Property PatchLevel | Export-Csv -Path $SaveAs -Append -NoTypeInformation } 

     else { 
      Get-ItemProperty "HKLM:SOFTWARE\" | Select-Object - 
      Property CurrentVersion | Export-Csv -Path $SaveAs -Append - 
      NoTypeInformation} 


    } 
} 
#if test-connect fails, append file with 'offline' 
else { 
     "$line, offline" 
     continue 
    } 

}

+0

似乎基本思想是,運行此腳本,將接觸到的信息,計算機和信息將被寫入文件。 DoesItemExist函數會很好,因爲它沒有出現您的腳本正在檢查$計算機中的計算機的遠程註冊表。就是運行該腳本的計算機。 你可以顯示一些看起來不對的示例輸出,以便我們知道你想要實現什麼嗎? – Matt

+0

「不能正確顯示」是不充分的問題描述。你得到了什麼輸出,這與你所期望的輸出有什麼不同? –

回答