Console vs Script (winrs)

This topic contains 9 replies, has 4 voices, and was last updated by Profile photo of Stefan Werner Stefan Werner 1 year, 7 months ago.

  • Author
    Posts
  • #28611
    Profile photo of Stefan Werner
    Stefan Werner
    Participant

    So, when I type the following into a console session it executes without error:

    $cmd = {winrs.exe /r:$computer powershell.exe -noprofile -command {Get-ChildItem -Path 'wsman:\localhost\plugin'}}
    $Plugins = & $cmd

    But when I drop this code into a script and run it from the same console, it fails giving the following error:

    Winrs.exe: Unrecognized switch "-noninteractive"
    Use "winrs -?" to obtain the usage information

    I am not really sure how to get around this. I have used winrs in scripts in the past and have never come across this. A Google search also didn't reveal anything helpful.

    Thoughts?

    The reason I am not using Invoke-Command is simply that the particular situation which this code is intended for assumes PowerShell Remoting is not functioning.

  • #28613
    Profile photo of Robbie Courtney
    Robbie Courtney
    Participant

    Hmm that is interesting. When I try it in the ISE, I get the same thing, it seems to work in the PowerShell Console though, but seems to launch some sort of shell in the background.

    I suspect that winrs.exe is launching as an interactive console app which is not supported in the ISE when launching or while launching as a script.

    http://blogs.msdn.com/b/powershell/archive/2009/02/04/console-application-non-support-in-the-ise.aspx

    Im guessing it works in the PowerShell Console because it supports console apps. I don't know much about winrs.exe, but doing a wins.exe /? I can't see an option to force it to run in a non interactive mode.

  • #28615
    Profile photo of Stefan Werner
    Stefan Werner
    Participant

    While that sounds like a perfectly plausible explanation, the following two lines are from an older script of mine which works fine:

    winrs /r:$computer powershell -noprofile -command {register-pssessionconfiguration microsoft.powershell -NoServiceRestart -Force}
    winrs /r:$computer powershell -noprofile -command {register-pssessionconfiguration microsoft.powershell32 -NoServiceRestart -Force}

    Confusing isn't it. 🙂

  • #28617
    Profile photo of Robbie Courtney
    Robbie Courtney
    Participant

    hmm. Ok so I played around with this some. It seems like the first code you posted works fine when I launch the script from the PowerShell Console. On my test VM I am using PowerShell version 3.0. It does not work in the ISE if i put the code in the console or save it and launch it as a script, I get the same error as you, which is expected since the ISE does not work with interactive console apps. Where are you launching the script?

    This is the code i used. I saved it as a script and ran it in the PowerShell Console

    $computer = $env:COMPUTERNAME
    $cmd = {winrs.exe /r:$computer powershell.exe -noprofile -command {Get-ChildItem -Path 'wsman:\localhost\plugin'}}
    $Plugins = & $cmd
    $Plugins
    
  • #28619
    Profile photo of Stefan Werner
    Stefan Werner
    Participant

    I ran the script from the console.

    Also, here are my two functions. As you can see they pretty match what you posted above. My laptop is running PowerShell 5 and most of our workstations are on on PS4.

    Function Get-WsManPlugins
    {
        Param ( [string]$address )
        
        $cmd = { winrs.exe /r:$address powershell.exe -noprofile -command { Get-ChildItem -Path 'wsman:\localhost\plugin' } }
        $Plugins = & $cmd
        
        Return $Plugins    
    }
    
    Function Get-WsManListener
    {
        Param ( [string]$address )
        
        $cmd = { winrs.exe /r:$address powershell -noprofile -command { Get-ChildItem -Path 'wsman:\localhost\listener' } }
        $Listener = & $cmd
        
        Return $Listener
    }
  • #28620
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    It does not like the {} you are putting around the command parameter. The below works for me using PS 4.0

    $computer = "workstation"
    $cmd = {winrs.exe /r:$computer powershell.exe -noprofile -command Get-ChildItem -Path 'wsman:\localhost\plugin'}
    $Plugins = & $cmd
    $Plugins
    
  • #28621
    Profile photo of Robbie Courtney
    Robbie Courtney
    Participant

    I just spun up a vm, windows 8.1 with April preview of powershell v5, and it works as well running the script in the PowerShell Console.

    saved this as a script and ran it

    Function Get-WsManPlugins
    {
        Param ( [string]$address )
        
        $cmd = { winrs.exe /r:$address powershell.exe -noprofile -command { Get-ChildItem -Path 'wsman:\localhost\plugin' } }
        $Plugins = & $cmd
        
        Return $Plugins    
    }
    
    Function Get-WsManListener
    {
        Param ( [string]$address )
        
        $cmd = { winrs.exe /r:$address powershell -noprofile -command { Get-ChildItem -Path 'wsman:\localhost\listener' } }
        $Listener = & $cmd
        
        Return $Listener
    }
    
    Get-WsManListener $env:COMPUTERNAME
    Get-WsManPlugins $env:COMPUTERNAME
    
  • #28622
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    You can also do it this way

    $computer = "workstation"
    $cmd = {winrs.exe /r:$computer powershell.exe -noprofile -command ""}
    $Plugins = & $cmd
    $Plugins

    Hum wordpress is stripping out the code

    Inside the "" after the -command is an & followed by a space and then this:
    {Get-ChildItem -Path 'wsman:\localhost\plugin'}

  • #28624
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    When you wrap curly brackets around a command, it is converted to a ScriptBlock:

    PS C:\Users\Rob> (  {}  ).GetType()
    
    IsPublic IsSerial Name                                     BaseType                                                                                                                                      
    -------- -------- ----                                     --------                                                                                                                                      
    True     True     ScriptBlock                              System.Object  
    

    Scriptblocks are typically used with Invoke-Command. This article discusses some of the options for calling command-line tools in Powershell: http://blogs.technet.com/b/josebda/archive/2012/03/03/using-windows-powershell-to-run-old-command-line-tools-and-their-weirdest-parameters.aspx

  • #28627
    Profile photo of Stefan Werner
    Stefan Werner
    Participant

    So, I walked away for a while, came back ran the code (no changes) and it works perfectly fine now. WTF?

    Thanks for everyone's contributions. 🙂

You must be logged in to reply to this topic.