簡體   English   中英

從AD創建txt文件並查詢列出的服務器以獲取系統信息並導出到csv

[英]Create txt file from AD and query servers listed to get system information and export to csv

我正在嘗試在AD中創建計算機系統列表,我想將其保存到文本文件中,然后使用該文本文件檢索系統信息,即品牌,型號,制造商,序列號等。

我不是想一勞永逸地解決所有這些問題,而是以為我會先處理查詢系統信息,但是第一個問題是我可以從文本文件中讀取內容,但是它僅顯示來自第一個服務器的信息,然后停止,然后我會已將其設置為(或嘗試將其設置為)導出為CSV,但它創建了CSV文件,但沒有有關CSV文件的信息。 同樣在某些時候,我也需要對標題進行排序。

New-Item -ItemType Directory -Force -Path C:\Computer_Details
set-location C:\Computer_Details
$results = ForEach ($Computersystem in $Computer)
{
$Computer = Get-Content -path C:\computers.txt
$computerSystem = Get-CimInstance CIM_ComputerSystem
$computerBIOS = Get-CimInstance CIM_BIOSElement
$computerOS = Get-CimInstance CIM_OperatingSystem
$computerCPU = Get-CimInstance CIM_Processor
$computerHDD = Get-CimInstance Win32_LogicalDisk -Filter "DeviceID = 'C:'"}
Write-Host "System Information for: " $computerSystem.Name -BackgroundColor DarkCyan
"Manufacturer: " + $computerSystem.Manufacturer
"Model: " + $computerSystem.Model
"Serial Number: " + $computerBIOS.SerialNumber
"CPU: " + $computerCPU.Name
"HDD Capacity: "  + "{0:N2}" -f ($computerHDD.Size/1GB) + "GB"
"HDD Space: " + "{0:P2}" -f ($computerHDD.FreeSpace/$computerHDD.Size) + " Free (" + "{0:N2}" -f ($computerHDD.FreeSpace/1GB) + "GB)"
"RAM: " + "{0:N2}" -f ($computerSystem.TotalPhysicalMemory/1GB) + "GB"
"Operating System: " + $computerOS.caption + ", Service Pack: " + $computerOS.ServicePackMajorVersion
"User logged In: " + $computerSystem.UserName
"Last Reboot: " + $computerOS.LastBootUpTime 
$results | Export-Csv ComputerDetails.csv

任何幫助將不勝感激,也許應該提到我對PowerShell還是相當陌生的,但是猜測您將通過閱讀上面的內容來完成工作:)

您需要在foreach循環之外定義$Computer 除此之外,您還希望在循環內收集每台計算機的所有系統信息字符串:

New-Item -ItemType Directory -Force -Path C:\Computer_Details
set-location C:\Computer_Details

# Get computer names from file
$Computers = Get-Content -path C:\computers.txt

# Loop over each computer name
$results = foreach($Computer in $Computers)
{
    # Set up a remote session to the machine in question
    try{
        $ComputerSession = New-CimSession -ComputerName $Computer

        # Fetch all the information from it
        $computerSystem = Get-CimInstance CIM_ComputerSystem -CimSession $ComputerSession
        $computerBIOS = Get-CimInstance CIM_BIOSElement -CimSession $ComputerSession
        $computerOS = Get-CimInstance CIM_OperatingSystem -CimSession $ComputerSession
        $computerCPU = Get-CimInstance CIM_Processor -CimSession $ComputerSession
        $computerHDD = Get-CimInstance Win32_LogicalDisk -Filter "DeviceID = 'C:'" -CimSession $ComputerSession
        $Sucess = $true
    } catch {
        Write-Warning "Unable to fetch information from $Computer"
        $Success = $false
    }
    if($Sucess){
        # Create a new object containing all the details you want
        New-Object psobject -Property @{
            "ComputerName"     = $Computer
            "Manufacturer"     = $computerSystem.Manufacturer
            "Model"            = $computerSystem.Model
            "Serial Number"    = $computerBIOS.SerialNumber
            "CPU"              = $computerCPU.Name
            "HDD Capacity"     = "{0:N2}GB" -f ($computerHDD.Size/1GB)
            "HDD Space"        = "{0:P2}" -f ($computerHDD.FreeSpace/$computerHDD.Size) 
            "HDD Free"         = "{0:N2}GB" -f ($computerHDD.FreeSpace/1GB)
            "RAM"              = "{0:N2}GB" -f ($computerSystem.TotalPhysicalMemory/1GB)
            "Operating System" = "{0}, Service Pack: {1}" -f $computerOS.caption,$computerOS.ServicePackMajorVersion
            "User logged In"   = $computerSystem.UserName
            "Last Reboot"      = $computerOS.LastBootUpTime
        }
    } else {
        New-Object psobject -Property @{
            "ComputerName"     = $Computer
        }
    }        
}
# $results is now an array of the objects created above, export it to a CSV file!
$results | Export-Csv ComputerDetails.csv

您不應該使用Write-Host作為產生數據的方法,它僅用於調試目的。 而是在您的foreach循環內輸出一個字符串,這樣,它將被正確地收集到您的results變量中。 接下來,您要遍歷一台以上的計算機,但要獲取的唯一文件是c:\\computers.txt等,您不會針對由其名稱標識的任何遠程計算機查詢Get-CIMInstance 解決方法:首先,從computers.txt內容中逐一獲取計算機名稱,然后通過提供-ComputerName作為Get-CIMInstance的參數,對該計算機上的CIM實例執行一系列遠程請求。 最后,將輸出收集為單個字符串(如果有的話,最好使用它來簡化進一步的解析),並將其用作腳本塊的輸出。

$computers=get-content -path C:\computers.txt
$results = ForEach ($Computer in $Computers)
{
    try {
    # remote computer might not be accessible! 
    $computerSystem = Get-CimInstance CIM_ComputerSystem -computername $computer
    $computerBIOS = Get-CimInstance CIM_BIOSElement -computername $computer
    $computerOS = Get-CimInstance CIM_OperatingSystem -computername $computer
    $computerCPU = Get-CimInstance CIM_Processor -computername $computer
    $computerHDD = Get-CimInstance Win32_LogicalDisk -computername $computer -Filter "DeviceID = 'C:'"}
    # once you've collected the data, prepare output string
    $output="System Information for: $($computerSystem.Name)`r`n"
    $output+="Manufacturer: $($computerSystem.Manufacturer)`r`n"
    $output+="Model: $($computerSystem.Model)`r`n"
    $output+="Serial Number: $($computerBIOS.SerialNumber)`r`n"
    $output+="CPU: $($computerCPU.Name)`r`n"
    $output+="HDD Capacity: $("{0:N2}" -f ($computerHDD.Size/1GB)) GB`r`n"
    $output+="HDD Space: $("{0:P2}" -f ($computerHDD.FreeSpace/$computerHDD.Size)) Free ($({0:N2}" -f ($computerHDD.FreeSpace/1GB)) GB)"
    $output+="RAM: $({0:N2}" -f ($computerSystem.TotalPhysicalMemory/1GB)) GB`r`n"
    $output+="Operating System: $($computerOS.caption), Service Pack: $($computerOS.ServicePackMajorVersion)`r`n"
    $output+="User logged In: $($computerSystem.UserName)`r`n"
    $output+="Last Reboot: $($computerOS.LastBootUpTime)`r`n"
    } catch {
    $output="$computer is not accessible!"
    }
    # once built, output the string into the variable
    $output
}
$results | Out-File ComputerDetails.txt -Encoding UTF8

注意,我在字符串的任何地方都使用了“ $(表達式)”構造,它簡化了從許多表達式的結果中構造單個字符串的語法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM