Share via


PS Object to CSV file

Question

Tuesday, December 24, 2013 4:33 AM

I want all the server name and the process information in CSV file but please see below the result is coming wrongIt brings only server info but not the process information. I am using PS V2Function GetStatusCodeFunction Get-ProcessData
{
Param([string] $ComputerName)
 $ProcessData=Get process | Select Name
    
    return $ProcessData
    
}
$ServerList = Get-Content "C:\temp\Report\ServerList.txt"
$Result1 = @()
$oArray01=@()
$ProcessData=$null
Foreach($ServerName in $ServerList)
{
    
    
    
        $OperatingSystem = Get-WmiObject Win32_OperatingSystem -ComputerName $ServerName -ErrorAction SilentlyContinue
        $ProcessData=Get-ProcessData $ServerName
        $NewObject01 = new-object PSObject 
        $NewObject01 | add-member -membertype NoteProperty -name "Server Name" -Value $ServerName 
        $NewObject01 | add-member -membertype NoteProperty -name "Process Data" -Value $ProcessData | % {$_.Name}
        $oArray01 += $NewObject01 
    
    
    $oArray01 | Export-Csv -Path "c:\temp\report\shortexport01.csv" 
    $Result1 += New-Object PSObject -Property @{
        ServerName = $ServerName
        Process=$ProcessData | % {$_.Name}
 
    }
     
}Result from CSV file the process comes empty. What am I doing wrong?"Server Name","Process Data"
"00113E",
"00114E",
"00115E",
"00136E",
"00170E",
"00005E",
"18001Q",
"18101Q",
"28001Q",
"28002Q",
"28101Q",
"28102Q",
"28201Q",
"28202Q",

All replies (2)

Wednesday, December 25, 2013 12:59 PM âś…Answered

Hi asif300aaa,

To get process from remote computer, you can also try Get-WmiObject Win32_Process.

The script below may be helpful for you, which can list both severnames and processnames:

$ServerList = Get-Content "d:\servername.txt"
$NewObject02=$null
$NewObject02=@()
Foreach($ServerName in $ServerList){
$Process = Get-WmiObject Win32_Process -ComputerName $ServerName -ErrorAction SilentlyContinue 
$Process|foreach{
        $NewObject01 = new-object PSObject 
        $NewObject01 | add-member -membertype NoteProperty -name "Server Name" -Value $ServerName 
        $NewObject01 | add-member -membertype NoteProperty -name "Process Data" -Value $_.name
        $NewObject02 += $NewObject01 
    }
}
$NewObject02 | export-csv d:\processes.csv

I hope this helps.

We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time.
Thanks for helping make community forums a great place.


Tuesday, December 24, 2013 5:24 AM

Your Function declared above is a little buggy:

Function Get-ProcessData
{
Param([string] $ComputerName)
 Get-Process -ComputerName $Computername | Select -ExpandProperty Name
    
    return $ProcessData
    
}

You are not calling Get-Process with the ComputerName argument......see above fixed it and posted.

and I think while creating the Custom PS Object, you use:

 $NewObject01 | add-member -membertype NoteProperty -name "Process Data" -Value $ProcessData | % {$_.Name}

No need to pipe the output further......to foreach loop (at the end) as you are returning the Array back already from your function. But as you see the output  will not be formatted properly.

Hope this helps

Knowledge is Power{Shell}.