none
PowerShell Scripting output issue RRS feed

  • Question

  • Hello All ,

    I have created below Script, the script is working fine and display the correct output with NumberofCores on the Screen. like NumberofCores             : {12, 12}

    But when I export it into CSV, under the NumberofCores colum, it display "System.Object[] "

    Here is my script"
    $servers = Get-Content C:\temp\servers.txt
    $Output = @()
    foreach ($server in $servers) {

    if(test-path \\$server\C$) 
    {
    $os = Get-WmiObject -Class win32_operatingsystem -ComputerName $server 
    $cpu = get-wmiobject Win32_ComputerSystem -ComputerName $server 
    $cores = get-wmiobject Win32_Processor -ComputerName $server
    $data = @{
        "ServerName" = $os.PSComputerName
        "Model" = $cpu.model
        "OS" = $os.caption
        "OSArchitecture"  = $os.OSArchitecture
        "NumberOfProcessors" = $cpu.NumberOfProcessors
        "NumberofCores" = $cores.NumberOfCores
        "NumberOfLogicalProcessors" = $cpu.NumberOfLogicalProcessors
            
    }
    $output= New-Object -TypeName psobject -Property $data
    $output | Export-Csv C:\temp\Info.CSV -Append -NoTypeInformation

    }
    else
    {
    write-output "$server" | out-file c:\temp\errors.txt
    }
    }


    Regards Baneesh Pal Singh

    Wednesday, January 15, 2020 4:05 PM

Answers

  • I am able to resolve my issue.

    the output of the No.ofcores are in string array format. So I have used Join function and I am getting the desired output in the CSV..

    "NumberofCores" = $cores.NumberOfCores -join ";"

    Hope this may help other people facing similar issue.


    Regards Baneesh Pal Singh

    • Marked as answer by Baneesh Singh Wednesday, January 15, 2020 4:13 PM
    Wednesday, January 15, 2020 4:13 PM

All replies

  • I am able to resolve my issue.

    the output of the No.ofcores are in string array format. So I have used Join function and I am getting the desired output in the CSV..

    "NumberofCores" = $cores.NumberOfCores -join ";"

    Hope this may help other people facing similar issue.


    Regards Baneesh Pal Singh

    • Marked as answer by Baneesh Singh Wednesday, January 15, 2020 4:13 PM
    Wednesday, January 15, 2020 4:13 PM
  • You need to use PowerShell to do this and not old VBScript methods.

    Get-Content C:\temp\servers.txt |
        ForEach-Object{
            if(test-path \\$_\C$){
                $os = Get-WmiObject -Class win32_operatingsystem -ComputerName $_ 
                $cpu = Get-WmiObject Win32_ComputerSystem -ComputerName $_ 
                $processors = Get-WmiObject Win32_Processor -ComputerName $_          
                [pscustomobject]@{
                     ServerName = $os.PSComputerName
                     Model = $cpu.model
                     OS = $os.caption
                     OSArchitecture  = $os.OSArchitecture
                     NumberOfProcessors = $cpu.NumberOfProcessors
                     NumberofCores =  $processors.NumberOfCores -join '|'
                     NumberOfLogicalProcessors = $cpu.NumberOfLogicalProcessors       
                }
            }else{
                $_ | out-file c:\temp\errors.txt -Append
            }
        } |
        Export-Csv C:\temp\Info.CSV -NoTypeInformation
    
    Much simpler, faster and easier to understand.


    \_(ツ)_/


    • Edited by jrv Wednesday, January 15, 2020 6:24 PM
    Wednesday, January 15, 2020 6:23 PM
  • Thank you for your expert advice and help.

    Much appreciated


    Regards Baneesh Pal Singh

    Monday, February 3, 2020 12:55 PM