0

im doing some testing with powershell to list hardware information of each computer in my domain. my script will get a list of computer from a text file that i created using powershell.

$arrComputers = get-Content -Path "C:\powershell\List.txt"
foreach ($strComputer in $arrComputers)
{
    $colItemsOs = get-wmiobject -class "Win32_OperatingSystem" -namespace "root\CIMV2" `
    -computername $strComputer
   foreach ($objItem in $colItemsOs)
   {
    Write-host  "Computer Name: " $strComputer
    Write-host  "OsName: " $objItem.Name
    Write-host  "Version: " $objItem.Version
   }

    $colItemsCompsys = get-wmiobject -class "Win32_ComputerSystem" -namespace "root\CIMV2" `
    -computername $strComputer
   foreach ($objItem in $colItemsCompsys)
   {
    Write-host  "Memory: " $objItem.TotalPhysicalMemory
    Write-host  "Manufacturer: " $objItem.Manufacturer
    Write-host  "Model: " $objItem.Model
    Write-host  "Domain: " $objItem.Domain
   }



     $colItemsSysEnc = get-wmiobject -class "Win32_SystemEnclosure" -namespace "root\CIMV2" `
    -computername $strComputer
   foreach ($objItem in $colItemsSysEnc)
   {
    Write-host  "SerialNumber: " $objItem.SerialNumber
   }

     $colItemsProcessor = get-wmiobject -class "Win32_Processor" -namespace "root\CIMV2" `
    -computername $strComputer
   foreach ($objItem in $colItemsProcessor)
   {
    Write-host  "Processor: " $objItem.Name
   }

    $colItemsIP = get-wmiobject -class "Win32_NetworkAdapterConfiguration " -namespace "root\CIMV2" `
    -computername $strComputer -Filter "IpEnabled = TRUE"
   foreach ($objItem in $colItemsIP)
   {
    Write-host  "IPAddress: " $objItem.IpAddress
   }

    Write-host | Out-File c:\powershell\reportbeta.csv

}

however, i am unable to do so. how do i produce the result in csv file which will be able to be uploaded to my database using sqlcmd later on.

what am i missing out?

thank you


ive managed to display output in text file. however, only 1 record is displayed.

$arrComputers = get-Content -Path "C:\powershell\List.txt"
foreach ($strComputer in $arrComputers)
{
    $colItemsOs = get-wmiobject -class "Win32_OperatingSystem" -namespace "root\CIMV2" `
    -computername $strComputer
   foreach ($objItem in $colItemsOs)
   {
      $strComputer
      $Name =$objItem.Name
      $Version = $objItem.Version
   }

    $colItemsCompsys = get-wmiobject -class "Win32_ComputerSystem" -namespace "root\CIMV2" `
    -computername $strComputer
   foreach ($objItem in $colItemsCompsys)
   {
      $TotalMemory = $objItem.TotalPhysicalMemory
      $Manufacturer = $objItem.Manufacturer
      $Model = $objItem.Model
      $Domain = $objItem.Domain
   }



     $colItemsSysEnc = get-wmiobject -class "Win32_SystemEnclosure" -namespace "root\CIMV2" `
    -computername $strComputer
   foreach ($objItem in $colItemsSysEnc)
   {
      $SerialNUmber = $objItem.SerialNumber
   }

     $colItemsProcessor = get-wmiobject -class "Win32_Processor" -namespace "root\CIMV2" `
    -computername $strComputer
   foreach ($objItem in $colItemsProcessor)
   {
     $Processor = $objItem.Name
   }

    $colItemsIP = get-wmiobject -class "Win32_NetworkAdapterConfiguration " -namespace "root\CIMV2" `
    -computername $strComputer -Filter "IpEnabled = TRUE"
   foreach ($objItem in $colItemsIP)
   {
      $IPV4 = $objItem.IpAddress
   }

    $OutputObj  = New-Object -Type PSObject
    $OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $strComputer.ToUpper()


    $OutputObj | Add-Member -MemberType NoteProperty -Name Name -Value $Name 
    $OutputObj | Add-Member -MemberType NoteProperty -Name Version -Value $Version
    $OutputObj | Add-Member -MemberType NoteProperty -Name TotalPhysicalMemory -Value $TotalMemory 
    $OutputObj | Add-Member -MemberType NoteProperty -Name Manufacturer -Value $Manufacturer
    $OutputObj | Add-Member -MemberType NoteProperty -Name Model -Value $Model

    $OutputObj | Add-Member -MemberType NoteProperty -Name Processor -Value $ProcessorSpeed
    $OutputObj | Add-Member -MemberType NoteProperty -Name SerialNumber -Value $SerialNo
    $OutputObj | Add-Member -MemberType NoteProperty -Name Domain -Value $Domain
    $OutputObj | Add-Member -MemberType NoteProperty -Name IPAddress -Value $IPV4 
    $OutputObj | Export-CSV -NoTypeInformation "c:\powershell\report.txt"

}

im out of ideas now. should i make it into array like?

please advise.

Thank you

2 Answers 2

1

The problem is that you are exporting to csv within the loop, so the last item will be the only one showing. Try something like this.

Before the loop:

# Create empty array
$report = @()

At the end of the loop, in place of where you are currently exporting to CSV:

#Add item to report
$report += $OutputObj

Finally, after the end of the loop:

$report | Export-CSV -NoTypeInformation "c:\powershell\report.txt"
Sign up to request clarification or add additional context in comments.

1 Comment

thanks alot kevin. u just save me a couple of hours figuring out how to list pso object using add-content to generate my list.
0

I suggest you assign the foreach result to a variable then pipe it to Export-Csv. It should perform faster and is much efficient than recreating, and updating, an array in each loop iteration.

$result = foreach($strComputer in $arrComputers)...

$result | Export-CSV ...

If you used the Foreach-Object' cmdlet to loop over computers you could skip the whole assignment or array creation, processing one object at a time (streamlined) and pipe directly toExport-CSV`:

Get-Content -Path C:\powershell\List.txt | Foreach-Object{
    ...
} | Export-Csv ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.