How to out-excel on ONE instance

Guys, i am new to powershell and i hope to learn and help others in future. I need help to out my results in only one excel instance.Now if i run it on 10 machines it opens 10 different instances. thanks!

HERE IS MY CODE:

function Out-Excel
{
              param($Path = "$env:temp\$(Get-Date -Format yyyyMMddHHmmss).csv")
             $input | Export-CSV -Path $Path -UseCulture -Encoding UTF8 -NoTypeInformation
             Invoke-Item -Path $Path
}            
 


Function Get-MonitorInfo{
    [CmdletBinding()]
    Param
    (
        [Parameter(
        Position=0,
        ValueFromPipeLine=$true,
        ValueFromPipeLineByPropertyName=$true)]
        [alias("CN","MachineName","Name","Computer")]
        [string[]]$ComputerName = $ENV:ComputerName
    )


    Begin {
        $pipelineInput = -not $PSBoundParameters.ContainsKey('ComputerName')
    }

    Process
    {
        Function DoWork([string]$ComputerName) {
            $ActiveMonitors = Get-WmiObject -Namespace root\wmi -Class wmiMonitorID -ComputerName $ComputerName 
            $monitorInfo = @()

            foreach ($monitor in $ActiveMonitors)
            {
                $mon = $null

                $mon = New-Object PSObject -Property @{
                ManufacturerName=($monitor.ManufacturerName | % {[char]$_}) -join ''
                ProductCodeID=($monitor.ProductCodeID | % {[char]$_}) -join ''
                SerialNumberID=($monitor.SerialNumberID | % {[char]$_}) -join ''
                UserFriendlyName=($monitor.UserFriendlyName | % {[char]$_}) -join ''
                ComputerName=$ComputerName
                WeekOfManufacture=$monitor.WeekOfManufacture
                YearOfManufacture=$monitor.YearOfManufacture}

                $monitorInfo += $mon   
                
            }
            Write-Output $monitorInfo
            #$MonitorInfo |Out-Excel
            #$MonitorInfo |Out-GridView
        }

        if ($pipelineInput) {
            DoWork($ComputerName)
        } else {
            foreach ($item in $ComputerName) {
                DoWork($item)
            }
        }
    }
}

July 21st, 2015 12:15am

The script doesn't make much sense.  Perhaps you should get the author to fix it for you.

Why would anyone place a function inside of a loop?  Why recreate the sam function over and over.

Yes. Export-Csv will create separate files.  Just add -append too get one file.

I think this is what you are trying to do:

$computers |
    ForEach-Object{
        Get-WmiObject -Namespace root\wmi -Class wmiMonitorID -Computer $_ |
        select  @{ N = 'ManufacturerName';E={ ($_.ManufacturerName | % { [char]$_ }) -join '' }},
                @{ N = 'ProductCodeID';E={ ($_.ProductCodeID | % { [char]$_ }) -join '' }},
                @{ N = 'SerialNumberID';E={ ($_.SerialNumberID | % { [char]$_ }) -join '' }},
                @{ N = 'UserFriendlyName';E={ ($_.UserFriendlyName | % { [char]$_ }) -join '' }},
                @{ N = 'ComputerName';E={ $_.PsComputerName }},
                @{ N = 'WeekOfManufacture';E={ $_.WeekOfManufacture }},
                @{ N = 'YearOfManufacture';E={ $_.YearOfManufacture }}
    } |
    Export-Csv outfile.csv
    

Free Windows Admin Tool Kit Click here and download it now
July 21st, 2015 5:18am

If you must have a function then here is how you would do it: (although a function is totally unnecessary)

Function Get-MonitorInfo {
    Param(
        [Parameter(
            Position = 0,
            ValueFromPipeLine = $true,
            ValueFromPipeLineByPropertyName = $true
        )][string[]]$ComputerName=$ENV:ComputerName
    )
    
    Begin {
        Function DoWork([string]$ComputerName) {
            $ActiveMonitors = Get-WmiObject -Namespace root\wmi -Class wmiMonitorID -ComputerName $ComputerName
            foreach ($monitor in $ActiveMonitors) {
                New-Object PSObject -Property @{
                    ManufacturerName = ($monitor.ManufacturerName | % { [char]$_ }) -join ''
                    ProductCodeID = ($monitor.ProductCodeID | % { [char]$_ }) -join ''
                    SerialNumberID = ($monitor.SerialNumberID | % { [char]$_ }) -join ''
                    UserFriendlyName = ($monitor.UserFriendlyName | % { [char]$_ }) -join ''
                    ComputerName = $ComputerName
                    WeekOfManufacture = $monitor.WeekOfManufacture
                    YearOfManufacture = $monitor.YearOfManufacture
                }
            }
        }
    }
    
    Process {
        foreach ($computer in $ComputerName) {
            DoWork $computer
        }
    }
}

$computers | Get-MonitorInfo
Get-MonitorInfo somepc
July 21st, 2015 5:26am

@jrv

thanks for your response.

Free Windows Admin Tool Kit Click here and download it now
July 22nd, 2015 10:00pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics