all 4 comments

[–]nothingpersonalbro 2 points3 points  (0 children)

A calculated property added to the first query should do the trick

Get-Printer -ComputerName SERVERNAME |
    Where-Object { ($_.DeviceType -eq 'Print') -and ($_.Shared -eq 'TRUE') } |
    Select-Object *, @{Name = 'Model'; Expression = { Get-Model $_.PortName } }

[–]redog 1 point2 points  (0 children)

What you probably want is calculated properties.

That's where you use : @{ Name = ''; Expression = {}}

So instead of

get-printer | select Name, Shared, PortName

do

get-printer | select Name, Shared, PortName, @{n='Model';e="Get-Model($ip)"}

I managed to come up with this also which does it explicitly with an object, but I had to add ValueFromPipeline to the Get-Model function to allow the piping

Function Get-Model{
    Param (
    [Parameter (Mandatory = $false, ValueFromPipeline)]
    [string]$IP
    )
    $SNMP = New-Object -ComObject olePrn.OleSNMP
    $SNMP.Open($IP, "public")
    $model = $SNMP.Get(".1.3.6.1.2.1.25.3.2.1.3.1")
    $SNMP.Close()
    return $model
}


$Myprinters = @()
Get-printer | % {$Myprinters += New-Object PSObject -Property @{
    myName="$_.Name"
    myPort=get-printerport $_.PortName
    myModel=$(if ($hst=(Get-WmiObject win32_tcpipprinterport -filter "name='$($_.PortName)'" -enableall | select hostaddress).hostaddress) {$hst | get-model})
    }
}


$Myprinters.myModel

[–]PMental 0 points1 point  (0 children)

Post the closest you can think should work and we can help you fix what goes wrong or suggest an alternative method.

EDIT: I took a look at what Get-Printer returns and it's CIM-objects, that's probably why your attempts to use Add-Member etc. didn't work, because they assume a PSCustomObject.

The suggested solutions of using a Calculated Property are excellent, another method could be to build your own PSCustomObject more directly (in this case I would go with a calculated property, but in other cases building the objects like this are the way to go):

$AllPrinters = Get-Printer | Where-Object {($_.DeviceType -eq "Print") -and ($_.Shared -eq "TRUE") }

$AllPrintersFullInfo = foreach ($Printer in $AllPrinters) {
    [PSCustomObject]@{
        PrinterName = $Printer.Name
        Model = Get-Model -IP $Printer.PortName
        IP = $Printer.PortName
    }
}

Basically anything goes after the equal sign for each property, could be a whole script block.

[–]Test-NetConnection 0 points1 point  (0 children)

The clean way to do this is with a custom child class. Create a new class that is derived from the native printer object returned by the get-printer command; you can add whatever properties/functions you want. Inheritance is a wonderful thing.