简体   繁体   中英

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

I'm trying to create a list of computer systems in AD, which I would like to save to a text file, and then using the text file retrieve the system information, ie make, model, manufacturer, serial number etc.

Rather than try and tackle this all in one go I thought I'd do the query system information first, but the first problem is I can read contents from text file, but it only displays the information from first server then stops and secondly I've set it to (or tried to set it to) export to CSV, but it creates the CSV file but no information on the CSV file. Also at some point I'm going to need to sort headers too.

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

Any help would be greatly appreciated and probably should mention I'm fairly new to PowerShell, but guessing you'll work that out reading the above :)

You need to define $Computer outside the foreach loop. In addition to that, you'd want to gather all the system information strings per computer inside the loop:

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

You should not use Write-Host as a method to produce data, it's only good for debugging purposes. Instead output a single string inside your foreach-loop as a result, this way it will be properly gathered in your results variable. Next, you want to iterate through more than a single computer, but instead you get the only file there is, c:\\computers.txt and more, you don't query Get-CIMInstance against any remote computer identified by its name. To resolve: First, get the computer name out of computers.txt content, one by one, then execute a series of remote requests to CIM instances on that computer by providing -ComputerName as an argument to Get-CIMInstance . And finally, collect the output as a single string (this is preferred to simplify further parsing, if there is any) and use it as an output of your script block.

$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

Note, I have used a "$(expression)" construction everywhere in the strings, it simplifies syntax of building a single string out of many expressions' results.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM