Windows Vista Forums

select-string in a function not working
  1. #1


    Jason Guest

    select-string in a function not working

    All,



    I'm trying to run a function to assist in mapping printers. Running the
    following command outside of the function works fine:

    net view \\servername | select-string "print", "comment"

    This gives me only the printers on that server (the "comment" ensures that I
    get the table header)

    However, when I put that line in a function, the pipe to select-string
    breaks it, and I get null output; no errors. Any suggestions?

    function add-printer {
    write-host Current Mapped Printers:
    get-wmiobject win32_printer | select sharename, servername | where
    {$_.servername -ne $null}
    $printserver = Read-Host "Enter the servername without slashes"
    net view "\\$printserver" | select-string "print", "Comment"
    $printer = Read-Host "Type in the exact share name"
    $printerpath = "\\$printserver\$printer"
    $net = new-object -com Wscript.network
    $net.addwindowsprinterconnection($printerpath)
    get-wmiobject win32_printer | select sharename, servername | where
    {$_.servername -ne $null}
    }

      My System SpecsSystem Spec

  2. #2


    RickB Guest

    Re: select-string in a function not working

    On Oct 1, 10:44*am, Jason <Ja...@xxxxxx> wrote:

    > All,
    >
    > I'm trying to run a function to assist in mapping printers. *Running the
    > following command outside of the function works fine:
    >
    > net view \\servername | select-string "print", "comment"
    >
    > This gives me only the printers on that server (the "comment" ensures that I
    > get the table header)
    >
    > However, when I put that line in a function, the pipe to select-string
    > breaks it, and I get null output; no errors. *Any suggestions?
    >
    > function add-printer {
    > write-host Current Mapped Printers:
    > get-wmiobject win32_printer | select sharename, servername | where
    > {$_.servername -ne $null}
    > $printserver = Read-Host "Enter the servername without slashes"
    > net view "\\$printserver" | select-string "print", "Comment"
    > $printer = Read-Host "Type in the exact share name"
    > $printerpath = "\\$printserver\$printer"
    > $net = new-object -com Wscript.network
    > $net.addwindowsprinterconnection($printerpath)
    > get-wmiobject win32_printer | select sharename, servername | where
    > {$_.servername -ne $null}
    >
    >
    >
    > }- Hide quoted text -
    >
    > - Show quoted text -
    That is definitely weird.
    It happens on my CTP2 machine too.
    Fixing it is easy.
    net view "\\$printserver" | select-string "print", "Comment" | write-
    host
    Explaining it is a different story.

    It can be reproduced like this

    function test ($val) {
    get-wmiobject win32_printer |
    select sharename, servername |
    where {$_.servername -ne $null}
    net view "$val"| select-string "server"
    }

    What is totally weird is that this totally locks up PowerShell.
    I had to kill the instance.

    function test ($val) {
    get-wmiobject win32_printer |
    select sharename, servername |
    where {$_.servername -ne $null}
    net view "$val"| select-string "server"|ft -a
    }

      My System SpecsSystem Spec

  3. #3


    Jason Guest

    Re: select-string in a function not working

    Thanks. I didn't even think about sending it to write-host.

    It's the easy solutions that I forget..

    I'll use this work-around.

    "RickB" wrote:

    > On Oct 1, 10:44 am, Jason <Ja...@xxxxxx> wrote:

    > > All,
    > >
    > > I'm trying to run a function to assist in mapping printers. Running the
    > > following command outside of the function works fine:
    > >
    > > net view \\servername | select-string "print", "comment"
    > >
    > > This gives me only the printers on that server (the "comment" ensures that I
    > > get the table header)
    > >
    > > However, when I put that line in a function, the pipe to select-string
    > > breaks it, and I get null output; no errors. Any suggestions?
    > >
    > > function add-printer {
    > > write-host Current Mapped Printers:
    > > get-wmiobject win32_printer | select sharename, servername | where
    > > {$_.servername -ne $null}
    > > $printserver = Read-Host "Enter the servername without slashes"
    > > net view "\\$printserver" | select-string "print", "Comment"
    > > $printer = Read-Host "Type in the exact share name"
    > > $printerpath = "\\$printserver\$printer"
    > > $net = new-object -com Wscript.network
    > > $net.addwindowsprinterconnection($printerpath)
    > > get-wmiobject win32_printer | select sharename, servername | where
    > > {$_.servername -ne $null}
    > >
    > >
    > >
    > > }- Hide quoted text -
    > >
    > > - Show quoted text -
    >
    > That is definitely weird.
    > It happens on my CTP2 machine too.
    > Fixing it is easy.
    > net view "\\$printserver" | select-string "print", "Comment" | write-
    > host
    > Explaining it is a different story.
    >
    > It can be reproduced like this
    >
    > function test ($val) {
    > get-wmiobject win32_printer |
    > select sharename, servername |
    > where {$_.servername -ne $null}
    > net view "$val"| select-string "server"
    > }
    >
    > What is totally weird is that this totally locks up PowerShell.
    > I had to kill the instance.
    >
    > function test ($val) {
    > get-wmiobject win32_printer |
    > select sharename, servername |
    > where {$_.servername -ne $null}
    > net view "$val"| select-string "server"|ft -a
    > }
    >

      My System SpecsSystem Spec

  4. #4


    Shay Levy [MVP] Guest

    Re: select-string in a function not working


    I have no problems getting output with these in v1:

    # 1
    $printserver="serverName"
    net view "\\$printserver" | select-string "print", "Comment"



    # 2
    function net-view($printserver){
    net view "\\$printserver" | select-string "print", "Comment"
    }

    net-view serverName


    BTW, the WMI call be optimized to:

    PS > get-wmiobject win32_printer -filter "servername is not null" | select
    sharename, servername


    ---
    Shay Levy
    Windows PowerShell MVP
    http://blogs.microsoft.co.il/blogs/ScriptFanatic
    PowerShell Toolbar: http://tinyurl.com/PSToolbar



    R> On Oct 1, 10:44 am, Jason <Ja...@xxxxxx> wrote:
    R>

    >> All,
    >>
    >> I'm trying to run a function to assist in mapping printers. Running
    >> the following command outside of the function works fine:
    >>
    >> net view \\servername | select-string "print", "comment"
    >>
    >> This gives me only the printers on that server (the "comment" ensures
    >> that I get the table header)
    >>
    >> However, when I put that line in a function, the pipe to
    >> select-string breaks it, and I get null output; no errors. Any
    >> suggestions?
    >>
    >> function add-printer {
    >> write-host Current Mapped Printers:
    >> get-wmiobject win32_printer | select sharename, servername | where
    >> {$_.servername -ne $null}
    >> $printserver = Read-Host "Enter the servername without slashes"
    >> net view "\\$printserver" | select-string "print", "Comment"
    >> $printer = Read-Host "Type in the exact share name"
    >> $printerpath = "\\$printserver\$printer"
    >> $net = new-object -com Wscript.network
    >> $net.addwindowsprinterconnection($printerpath)
    >> get-wmiobject win32_printer | select sharename, servername | where
    >> {$_.servername -ne $null}
    >> }- Hide quoted text -
    >>
    >> - Show quoted text -
    >>
    R> That is definitely weird.
    R> It happens on my CTP2 machine too.
    R> Fixing it is easy.
    R> net view "\\$printserver" | select-string "print", "Comment" | write-
    R> host
    R> Explaining it is a different story.
    R> It can be reproduced like this
    R>
    R> function test ($val) {
    R> get-wmiobject win32_printer |
    R> select sharename, servername |
    R> where {$_.servername -ne $null}
    R> net view "$val"| select-string "server"
    R> }
    R>
    R> What is totally weird is that this totally locks up PowerShell. I had
    R> to kill the instance.
    R>
    R> function test ($val) {
    R> get-wmiobject win32_printer |
    R> select sharename, servername |
    R> where {$_.servername -ne $null}
    R> net view "$val"| select-string "server"|ft -a
    R> }
    R>



      My System SpecsSystem Spec

  5. #5


    RickB Guest

    Re: select-string in a function not working

    On Oct 1, 2:17*pm, Shay Levy [MVP] <n...@xxxxxx> wrote:

    > I have no problems getting output with these in v1:
    >
    > # 1
    > $printserver="serverName"
    > net view "\\$printserver" | select-string "print", "Comment"
    >
    > # 2
    > function net-view($printserver){
    > * * * * net view "\\$printserver" | select-string "print", "Comment"
    >
    > }
    >
    > net-view serverName
    >
    > BTW, the WMI call be optimized to:
    >
    > PS > get-wmiobject win32_printer -filter "servername is not null" | select
    > sharename, servername
    >
    > ---
    > Shay Levy
    > Windows PowerShell MVPhttp://blogs.microsoft.co.il/blogs/ScriptFanatic
    > PowerShell Toolbar:http://tinyurl.com/PSToolbar
    >
    > R> On Oct 1, 10:44 am, Jason <Ja...@xxxxxx> wrote:
    > R>
    >
    >
    >

    > >> All,
    >

    > >> I'm trying to run a function to assist in mapping printers. *Running
    > >> the following command outside of the function works fine:
    >

    > >> net view \\servername | select-string "print", "comment"
    >

    > >> This gives me only the printers on that server (the "comment" ensures
    > >> that I get the table header)
    >

    > >> However, when I put that line in a function, the pipe to
    > >> select-string breaks it, and I get null output; no errors. *Any
    > >> suggestions?
    >

    > >> function add-printer {
    > >> write-host Current Mapped Printers:
    > >> get-wmiobject win32_printer | select sharename, servername | where
    > >> {$_.servername -ne $null}
    > >> $printserver = Read-Host "Enter the servername without slashes"
    > >> net view "\\$printserver" | select-string "print", "Comment"
    > >> $printer = Read-Host "Type in the exact share name"
    > >> $printerpath = "\\$printserver\$printer"
    > >> $net = new-object -com Wscript.network
    > >> $net.addwindowsprinterconnection($printerpath)
    > >> get-wmiobject win32_printer | select sharename, servername | where
    > >> {$_.servername -ne $null}
    > >> }- Hide quoted text -
    >

    > >> - Show quoted text -
    >
    > R> That is definitely weird.
    > R> It happens on my CTP2 machine too.
    > R> Fixing it is easy.
    > R> net view "\\$printserver" | select-string "print", "Comment" | write-
    > R> host
    > R> Explaining it is a different story.
    > R> It can be reproduced like this
    > R>
    > R> function test ($val) {
    > R> get-wmiobject win32_printer |
    > R> select sharename, servername |
    > R> where {$_.servername -ne $null}
    > R> net view "$val"| select-string "server"
    > R> }
    > R>
    > R> What is totally weird is that this totally locks up PowerShell. I had
    > R> to kill the instance.
    > R>
    > R> function test ($val) {
    > R> get-wmiobject win32_printer |
    > R> select sharename, servername |
    > R> where {$_.servername -ne $null}
    > R> net view "$val"| select-string "server"|ft -a
    > R> }
    > R>- Hide quoted text -
    >
    > - Show quoted text -

    You didn't include the get-wmiobject statement.

    This, for some reason, has something to do with the problem.
    To reproduce, you must have both statements.

      My System SpecsSystem Spec

  6. #6


    Shay Levy [MVP] Guest

    Re: select-string in a function not working


    I did now


    PS C:\> function net-view($printserver){

    >> get-wmiobject win32_printer | select sharename, servername | where
    {$_.servername -ne $null}

    >> net view "\\$printserver" | select-string "print", "Comment"
    >> }
    >>
    PS C:\> net-view serverName

    Fax & Print Server
    Share name Type Used as Comment
    Client PrintToFax Disk
    CPFS07 Print CPFS07
    HP 4200IT Print HP 4200 IT
    HP650cTest Print HP DesignJet 650C(C2859B) by HP




    ---
    Shay Levy
    Windows PowerShell MVP
    http://blogs.microsoft.co.il/blogs/ScriptFanatic
    PowerShell Toolbar: http://tinyurl.com/PSToolbar



    R> On Oct 1, 2:17 pm, Shay Levy [MVP] <n...@xxxxxx> wrote:
    R>

    >> I have no problems getting output with these in v1:
    >>
    >> # 1
    >> $printserver="serverName"
    >> net view "\\$printserver" | select-string "print", "Comment"
    >> # 2
    >> function net-view($printserver){
    >> net view "\\$printserver" | select-string "print", "Comment"
    >> }
    >>
    >> net-view serverName
    >>
    >> BTW, the WMI call be optimized to:
    >>
    >> PS > get-wmiobject win32_printer -filter "servername is not null" |
    >> select sharename, servername
    >>
    >> ---
    >> Shay Levy
    >> Windows PowerShell
    >> MVPhttp://blogs.microsoft.co.il/blogs/ScriptFanatic
    >> PowerShell Toolbar:http://tinyurl.com/PSToolbar
    >> R> On Oct 1, 10:44 am, Jason <Ja...@xxxxxx> wrote:
    >> R>
    >>

    >>>> All,
    >>>>
    >>>> I'm trying to run a function to assist in mapping printers.
    >>>> Running the following command outside of the function works fine:
    >>>>
    >>>> net view \\servername | select-string "print", "comment"
    >>>>
    >>>> This gives me only the printers on that server (the "comment"
    >>>> ensures that I get the table header)
    >>>>
    >>>> However, when I put that line in a function, the pipe to
    >>>> select-string breaks it, and I get null output; no errors. Any
    >>>> suggestions?
    >>>>
    >>>> function add-printer {
    >>>> write-host Current Mapped Printers:
    >>>> get-wmiobject win32_printer | select sharename, servername | where
    >>>> {$_.servername -ne $null}
    >>>> $printserver = Read-Host "Enter the servername without slashes"
    >>>> net view "\\$printserver" | select-string "print", "Comment"
    >>>> $printer = Read-Host "Type in the exact share name"
    >>>> $printerpath = "\\$printserver\$printer"
    >>>> $net = new-object -com Wscript.network
    >>>> $net.addwindowsprinterconnection($printerpath)
    >>>> get-wmiobject win32_printer | select sharename, servername | where
    >>>> {$_.servername -ne $null}
    >>>> }- Hide quoted text -
    >>>> - Show quoted text -
    >>>>
    >> R> That is definitely weird.
    >> R> It happens on my CTP2 machine too.
    >> R> Fixing it is easy.
    >> R> net view "\\$printserver" | select-string "print", "Comment" |
    >> write-
    >> R> host
    >> R> Explaining it is a different story.
    >> R> It can be reproduced like this
    >> R>
    >> R> function test ($val) {
    >> R> get-wmiobject win32_printer |
    >> R> select sharename, servername |
    >> R> where {$_.servername -ne $null}
    >> R> net view "$val"| select-string "server"
    >> R> }
    >> R>
    >> R> What is totally weird is that this totally locks up PowerShell. I
    >> had
    >> R> to kill the instance.
    >> R>
    >> R> function test ($val) {
    >> R> get-wmiobject win32_printer |
    >> R> select sharename, servername |
    >> R> where {$_.servername -ne $null}
    >> R> net view "$val"| select-string "server"|ft -a
    >> R> }
    >> R>- Hide quoted text -
    >> - Show quoted text -
    >>
    R> You didn't include the get-wmiobject statement.
    R>
    R> This, for some reason, has something to do with the problem. To
    R> reproduce, you must have both statements.
    R>



      My System SpecsSystem Spec

  7. #7


    Jason Guest

    Re: select-string in a function not working

    CTP2 bug?

      My System SpecsSystem Spec

  8. #8


    Shay Levy [MVP] Guest

    Re: select-string in a function not working

    Hi Jason,

    Something has changed in CTP2. I get the first three items (as PSCustomObject)
    and the rest are all blank (as MatchInfo).
    However, if I move the select-string call outside the function then I get
    all results as expected.

    function net-view($printserver){
    get-wmiobject win32_printer -filter "servername is not null" | select
    sharename, servername
    net view "\\$printserver"
    }

    net-view serverName | select-string "print", "Comment"



    BTW, instead of using a legacy application, you can use WMI to get the shares:

    PS > gwmi win32_share -computer computerName





    ---
    Shay Levy
    Windows PowerShell MVP
    http://blogs.microsoft.co.il/blogs/ScriptFanatic
    PowerShell Toolbar: http://tinyurl.com/PSToolbar



    J> CTP2 bug?
    J>



      My System SpecsSystem Spec

select-string in a function not working problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Logon script - function array and select case not working Nex6 VB Script 6 09 Jun 2009
Select first 16 characters in a string Steven PowerShell 4 12 Nov 2008
select-string substitute Calin Iaru PowerShell 2 22 Jul 2008
problems with $var | select-string -pattern $string -q Ben Christian PowerShell 3 08 Feb 2008
select-string question JW PowerShell 6 24 Oct 2006