none
PowerShell's variable output issue RRS feed

  • General discussion


  • This is my first post and I know I am a noob concerning Powershell and it may be a very simple thing I am asking, but I could yet not find any way to solve my problem.

    I am running a tiny script to read from a server the printers and I want to export results filterd and in a specific format (CSV) with also adjusting the content while creating the output. Here is what I am doing (servername replaced due to data privacy):

    cls
    $prt = Get-WmiObject Win32_Printer -ComputerName "MYPRINTSERVER" | Where-Object {$_.PortName -match "EQ"} 
    $a = ($prt.PortName -replace "EQ_", "")
    $a
    

    In the PowerShell output I am seeing already what I want (at first look, but that's something else for later to ask maybe), IPs changed due to data privacy:

    PowerShell direct out:
    99.99.152.56
    99.99.152.35
    99.99.152.37
    99.99.152.36
    99.99.152.48
    99.99.152.46
    99.99.152.44
    

    When I am now using "Export-CSV" for this result, I am getting this in my file:

    #TYPE System.String
    "Length"
    "13"
    "13"
    "13"
    "13"
    "13"
    "13"
    "13"
    

    I am assuming that has to do with the variable format (perhaps by using "-replace")? Howsoever I am not able to get my output just as CSV values. If I am using "Out-File" as a TXT I am getting the list of IPs, though not comma sparated.

    Any help highly appreciated! Thanks everyone!

    Regards Sascha

    • Changed type Bill_Stewart Tuesday, November 7, 2017 10:59 PM
    • Moved by Bill_Stewart Tuesday, November 7, 2017 11:00 PM This is not "fix/debug/rewrite my script for me" forum
    Monday, September 25, 2017 4:49 PM

All replies

  • Alright, took my lessons and learned a bit more, though still not reached my goal. What is my goal?

    Well, I want a CSV (because it has comma separators) from my script which contains simply the printer queue name and the (filterd by EQ_) port, but with EQ_ information removed in the output data.

    Currently I am that far:

    "Name","PortName"
    "MYPRINT01-009C","EQ_99.99.152.56"
    "MYPRINT01-008B","EQ_99.99.152.35"
    "MYPRINT01-007B","EQ_99.99.152.37"
    "MYPRINT01-005B","EQ_99.99.152.36"
    "MYPRINT01-003B","EQ_99.99.152.48"
    "MYPRINT01-002B","EQ_99.99.152.46"
    "MYPRINT01-001B","EQ_99.99.152.44"
    

    By using these lines:

    $a = @(Get-WmiObject Win32_Printer -ComputerName "MYPRINTSERVER" | Select-Object Name, PortName)
    $a | Where-Object {$_ -match "EQ"} | Export-csv C:\Scripts\Ports.csv
    

    But each time I try to use "-replace" with the variables, the output is no longer a string (if that's the right term) but this so called "custom output"...

    That's where I am stuck at the moment:

    $a = @(Get-WmiObject Win32_Printer -ComputerName "MYPRINTSERVER" | 
    Select-Object Name, PortName)
    $b = $a | Where-Object {$_ -match "EQ"} 
    $b -replace "EQ_",""
    

    It ends up showing even directly in the PowerShell window the format, which in fact is almost good, because "EQ_" is removed and the name and port show in one line, separated by a delimiter. But of course it is a) not nice b) does not work with Export-csv and c) I do not want to export as a TXT and then do the cleanup in Excel.

     @{Name=MYPRINT01-001B; PortName=10.105.152.44}
    

    I tried different ways like "Select-Object -ExpandProperty" etc. but nothing brought me the result...lost now...

    Thanks for any advise! :)

    Monday, September 25, 2017 4:50 PM
  • Slightly changed the script because I found the tip to use "Select-Object", though I did not get the right usage, I think:

    cls
    $prt = Get-WmiObject Win32_Printer -ComputerName "MYPRINTSERVER" | Select-
    Object -Property PortName | Where-Object {$_.PortName -match "EQ"}
    $a = $prt -replace "EQ_" , ""
    $a
    

    That's the output I am seeing right away:

    @{PortName=99.99.152.56}
    @{PortName=99.99.152.35}
    @{PortName=99.99.152.37}
    @{PortName=99.99.152.36}
    @{PortName=99.99.152.48}
    @{PortName=99.99.152.46}
    @{PortName=99.99.152.44}
    

    • Edited by Etchell Monday, September 25, 2017 4:51 PM
    Monday, September 25, 2017 4:50 PM
  • Try replacing -Property with -ExpandProperty in your Select-Object.

    -- Bill Stewart [Bill_Stewart]

    Monday, September 25, 2017 4:56 PM
  • Thanks Bill,

    I tried that and now the output is just nothing :)

    It all starts to be converted int the "@" format when I am using the "-replace" part.

    Any thoughts?

    (it also ends up with the @ output if I use it simple like $prt -replace ... directly of course)
    • Edited by Etchell Monday, September 25, 2017 7:13 PM
    Monday, September 25, 2017 7:00 PM
  • Simple but requires objects and not strings:

    Get-WmiObject Win32_Printer -ComputerName myprintserver -Filter 'Portname LIKE "%EQ%"'|
         Select SystemName,PortName |
         Export-Csv myfile.csv 
    You need to learn the basics of PowerShell: https://mva.microsoft.com/en-us/training-courses/getting-started-with-microsoft-powershell-8276


    \_(ツ)_/



    • Edited by jrv Monday, September 25, 2017 8:15 PM
    Monday, September 25, 2017 8:13 PM
  • Thanks for the answer and help. Will check quickly. And of course I want to learn the basics of PowerShell :)
    Funny sidenote: I talked to different "experts" and yet noone was able to find an easy solution...

    :D

    Tuesday, September 26, 2017 8:53 AM
  • Then it seems that they are not 'experts' :)

    Please click on Propose As Answer or to mark this post as and helpful for other people. This posting is provided AS-IS with no warranties, and confers no rights.

    Tuesday, September 26, 2017 8:54 AM
  • Thanks for the answer and help. Will check quickly. And of course I want to learn the basics of PowerShell :)
    Funny sidenote: I talked to different "experts" and yet noone was able to find an easy solution...

    :D


    The solution is what I posted.  The "experts" did not understand how your way to using PowerShell caused the issue.  Once you learn the basics you will quickly understand what your code was doing that caused it to fail.

    \_(ツ)_/

    Tuesday, September 26, 2017 8:57 AM
  • Well, your script does already the same I was able to achive.

    What I am struggling with is not to query ports which match / include a specific character and export, but to directly modify this data, here to remove "EQ_" and keep the rest.
    This is done with my "-replace" command and when this is used in the variable, the outout alters into "@...."

    Without replace, I am getting the same results as your script.

    Of course I could modify the data in Excel or any other editor later.
    But for handover to another automation process, the exported data has to be already modified.

    :)

    Thank you anyway for your help.


    • Edited by Etchell Tuesday, September 26, 2017 9:33 AM
    Tuesday, September 26, 2017 9:32 AM
  • The result is an array.  You cannot use "replace" on an array.  If you replace on an object array you will get a bunch of strings that will export only the string length to the file.  You will need to use a computed select statement to alter the column contents.


    \_(ツ)_/

    Tuesday, September 26, 2017 9:36 AM
  • Thanks for the replies and the hint

    "You will need to use a computed select statement to alter the column contents."

    Than this is not an easy effort, since I did not just post my question without searching the Internet before
    I found many discussions going on where people struggle with getting the right output and sometimes solutions provided where even not working at all.
    With this array, I understand, the replcae does work somehow, as in the screen output the replacement was done.
    Not understanding in detail yet what a comuted select statement means or how to do that, I will try to continue my research.

    Tuesday, September 26, 2017 10:48 AM
  • "computed" should read "calculated".

    https://technet.microsoft.com/en-us/library/ff730948.aspx


    \_(ツ)_/

    Tuesday, September 26, 2017 6:40 PM
  • By today, no solution found. To me it feels like programmers sometimes like to keep things complicated ;)

    As many PS users, we need simple ways to reach the goal. So I wonder why such a simple thing like a replace alters the output completely?

    That's like

    8B 45 F8
    8B 55 FC
    01 D0
    89 45 F4

    and not BASIC (LOL)

    ;)
    Cheers

    Monday, October 9, 2017 8:54 AM
  • Unfortunately brain surgery cannot be simplified for the masses.   Perhaps a little research into the technology would help you to separate yourself from the "masses".

    You cannot build a space ship without a sufficient knowledge of physics.  Computers are no different.

    Your issue all depends on the naming convention for ports.


    \_(ツ)_/


    • Edited by jrv Monday, October 9, 2017 9:06 AM
    Monday, October 9, 2017 9:05 AM