get-printer results

%3CLINGO-SUB%20id%3D%22lingo-sub-1440956%22%20slang%3D%22en-US%22%3Eget-printer%20results%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1440956%22%20slang%3D%22en-US%22%3E%3CP%3EHello%2C%3C%2FP%3E%3CP%3EI%20have%20below%20code%20to%20get%20printers%20list%20printer%20port%20%3D%20xxx.xxx.xxx.xxx%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24ports1%20%3D%20Get-Printer%20-ComputerName%20%24pcname1%20%7C%20Select-Object%20-Property%20name%20%7C%20Where-Object%20portname%20-like%20%24txtOld%0Aforeach%20%20(%24item%20in%20%24ports1)%20%7B%0A%0Ado%20something%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhen%20I%20run%20this%3B%20%24ports1%20doesn't%20have%20any%20value%20in%20it.%20So%2C%20when%20the%20execution%20process%20hits%20for%20each%20command%20%24ports1%20is%20showing%20empty%20and%20not%20doing%20anything.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ein%20the%20terminal%20when%20I%20run%20this%20command%20with%20the%20return%20value.%20it%20works%20fine%20and%20shows%20me%20a%20list%20of%20printers%3C%2FP%3E%3CP%3Eany%20idea%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1440956%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1440974%22%20slang%3D%22en-US%22%3ERe%3A%20get-printer%20results%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1440974%22%20slang%3D%22en-US%22%3E%3CP%3EI%20changed%20the%20code%20to%20this%20and%20worked%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24ports1%20%3D%20Get-Printer%20-ComputerName%20%24pcname1%20%7C%20Select-Object%20-Property%20portname%20%7C%20Where-Object%20portname%20-like%20%24OldIP%0A%24ports1%20%7C%20ForEach-Object%7B%20%20%23(%24item%20in%20%24ports1)%20%7B%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
New Contributor

Hello,

I have below code to get printers list printer port = xxx.xxx.xxx.xxx

 

 

$ports1 = Get-Printer -ComputerName $pcname1 | Select-Object -Property name | Where-Object portname -like $txtOld
foreach  ($item in $ports1) {

do something
}

 

 

When I run this; $ports1 doesn't have any value in it. So, when the execution process hits for each command $ports1 is showing empty and not doing anything. 

 

in the terminal when I run this command with the return value. it works fine and shows me a list of printers

any idea?

1 Reply

I changed the code to this and worked

$ports1 = Get-Printer -ComputerName $pcname1 | Select-Object -Property portname | Where-Object portname -like $OldIP
$ports1 | ForEach-Object{  #($item in $ports1) {}