0

Trying to input a list of servers into my script using a for loop and then would need the results in a comma separated format.

input a list of servers

$s1 = (get-content .\s1.txt)
foreach($s2 in $s1){
    $object = New-Object -TypeName PSObject

    $NumberoflogicalProcessors = (gwmi win32_processor -computername $s2 |measure -Property NumberoflogicalProcessors -Sum).Sum
    $CPUCore = (gwmi win32_processor -computername $s2 |measure -Property  numberOfCores -Sum).Sum

    $object | Add-Member -MemberType NoteProperty -Name "CPU Logical processors" -Value $NumberoflogicalProcessors
    $object | Add-Member -MemberType NoteProperty -Name "CPU Core" -Value $CPUCore
    $List+= $object
}
return $List

$str1 = "NumberoflogicalProcessors"+","+"CPUCore"
$str2 = $NumberoflogicalProcessors.ToString() + "," + $CPUCore.ToString()
return $str1 + "`n" + $str2

expected output:

row1(header)
row2(server1 values)
row2(server2 values)

Example:

NumberoflogicalProcessors, CPUCore
2, 5
4, 3
so on.......
1
  • Using convert-to csv or export-toemphasized text csv would fetch me an output in csv foramt, however I'm trying to get the values in the below format , passing them to str1(header)in row1 and str2(with respective server values) in row 2, which im unable to get, please assist Commented Apr 14, 2019 at 17:16

1 Answer 1

3

A little bit cleaner and easier to read would be something like this:

$s1 = Get-Content -Path .\s1.txt
$List = foreach ($s2 in $s1) {
    $CPUCore = Get-CimInstance -ClassName CIM_Processor  -ComputerName  $s2 

    [PSCustomObject]@{
        ComputerName = $s2
        PhysicalCores = $CPUCore.NumberOfCores
        LogicalCores  = $CPUCore.NumberOfLogicalProcessors
    }
}
$List
$List | Export-Csv -Path .\s1List.csv -Delimiter ',' -NoTypeInformation

You don't need to run the WMI or CIM cmdlet twice inside of one loop.

Sign up to request clarification or add additional context in comments.

6 Comments

-Delimiter ',' is the default, in case OP doesn't want double quotes in the csv : ($List | ConvertTo-Csv -NoTypeInformation) -Replace '"' | Set-Content .\s1List.csv
All standard-compliant tools should not have a problem with the double quotes.
@Olaf, without using export-csv, is it possible to use it this way $str1 = "NumberoflogicalProcessors"+","+"CPUCore" $str2 = $NumberoflogicalProcessors.ToString() + "," + $CPUCore.ToString() return $str1 + "`n" + $str2 in the loop and fetch the result?? trying to use this script in a tool and need the display of output in the above format, without exporting it to a file.
You can use the produced output in whatever way you need ... it's totally up to you. ;-)
:-)If yes, please let me know where to place the above variables str1 and str2 in the script to get the CSV output
|

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.