Some help passing commands to a button in a gui

Welcome Forums General PowerShell Q&A Some help passing commands to a button in a gui

This topic contains 13 replies, has 4 voices, and was last updated by

 
Participant
2 months, 4 weeks ago.

  • Author
    Posts
  • #108577
    Dan

    Participant
    Points: 0
    Rank: Member

    So I have a tool I am working on for a team in the company I work for.  I get the general concept but I am running into dumb issues which I am sure are due to my own ignorance.
    First:
    I would like to start a continuos ping session in an external window.  I can get it to work for a regular session, 4 results and done then the window auto closes.  When I try to pass the -t switch is when powershell farts out an error.  The command goes like this:

    Start-Process -FilePath ping.exe -ArgumentList $WksTxtBox1.Text -t

    It works if it doesn't have the -t switch:

    Start-Process -FilePath ping.exe -ArgumentList $WksTxtBox1.Text

    I have been banging my head against the display trying to figure this out and I know it must be something really simple to get but for the life of me I cannot.

    The other issue I am having is similar in that switches are ruining my powershell sanity.
    What I have is this:

    Start-Process -FilePath powershell.exe -ArgumentList -noexit -command enter-pssession -computername $WksTxtBox2.Text

    I have tried it with quotes before -noexit and after .Text. But that obviously did not work.

    What am I missing? It has to be something simple but I cannot figure it out.

    Thank you muchly for your help!

    -Dan L

  • #108586

    Participant
    Points: 135
    Helping Hand
    Rank: Participant
    Start-Process -FilePath ping.exe -ArgumentList "$WksTxtBox1.Text -t"

    or

    Start-Process -FilePath ping.exe -ArgumentList "$WksTxtBox1.Text","-t"

    😉

    • #108592
      Dan

      Participant
      Points: 0
      Rank: Member

      Unfortunately neither worked.
      The first one cause an error.
      The second however ran but the resultant window closed immediately after opening. Too quickly to see anything inside the window.
      Sorry man. =(

  • #108598

    Participant
    Points: 135
    Helping Hand
    Rank: Participant

    So it might be the variable or object you're using ($WksTxtBox1.Text). If I run this

    Start-Process -FilePath ping.exe -ArgumentList "google.com -t"

    or this

    Start-Process -FilePath ping.exe -ArgumentList "google.com","-t"

    it works just as expected.

    • #108601
      Dan

      Participant
      Points: 0
      Rank: Member

      hmmm.

      That's unfortunate, works fine.
      Has to be the variable.

  • #108611
    Dan

    Participant
    Points: 0
    Rank: Member

    Ok, worked it out.
    I was over thinking it.

    Start-Process -FilePath ping.exe -ArgumentList $WksTxtBox1.Text,"-t"

    Works fine.

    Still having issues with the enter-pssession though.

    Thanks for your help Olaf!

  • #108617

    Participant
    Points: 135
    Helping Hand
    Rank: Participant

    Still having issues with the enter-pssession though.

    What do you try to accomplish with this code? Enter-PSSession is made to be used interactively in the console. There are other ways to remote in scripts like Invoke-Command. And you don't need to start a new Powershell process to run another script from "inside" your script.

    • #108721
      Dan

      Participant
      Points: 0
      Rank: Member

      What do you try to accomplish with this code? Enter-PSSession is made to be used interactively in the console. There are other ways to remote in scripts like Invoke-Command. And you don't need to start a new Powershell process to run another script from "inside" your script.

      Sometimes I need a remote commandline. And unfortunately PSEXEC does not work on our network.
      I have thought about the issue and, normally, invoking a single command isn't what I need in order to get the result I require. Since I am using a GUI, spawning another powershell session with a remote console connection is currently my best option.

  • #108656

    Participant
    Points: 206
    Helping Hand
    Rank: Participant

    FYI...

    None of your errors have to do with your GUI, just syntax/quoting isues.

    See this...

    https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules?view=powershell-6

    https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/start-process?view=powershell-6

    $WksTxtBox1.Text -t

    # This fails, becasue you are passing twp params singulalry vs as the expected individual.

    $WksTxtBox1.Text

    This works, because it a single param and expandable.

    "$WksTxtBox1.Text -t"

    This fails, becasue of the varible not being expanded in the. It should be this

    "$($WksTxtBox1.Text) -t"

    If you open the above in the ISE or VSCode, you'll note the color coding difference on them and the error indicators.

    $WksTxtBox1.Text,"-t"

    This works becasue of auto expansion of the var as in the previous example in the double quotes, and you setting the last param properly as a seperate item.

    • #108722
      Dan

      Participant
      Points: 0
      Rank: Member

      Thank you for the great explanation on the differences!
      I did get it to work using the last example.

  • #108728

    Participant
    Points: 158
    Helping Hand
    Rank: Participant

    Why aren't you using Powershell commands vs Start-Process to start an old command and pass parameters to it:

    Test-NetConnection -ComputerName $WksTxtBox1.Text 
    

    To make it continous, you just wrap a simple loop around it, like so:

    Powershell Tip #85: Run an infinite / endless / continuous ping

  • #108901
    Dan

    Participant
    Points: 0
    Rank: Member

    Not to be trite, but I'm still fairly new to this and it was the way I had thought I needed to do it.
    Now that being said it was also a challenge from a friend so I wanted to show him It could be done.  Thanks to you guys he -might- shutup about it now haha.

  • #108904
    Dan

    Participant
    Points: 0
    Rank: Member

    I do have another issue now though, hope not to run out my last vestiges of welcome remaining.

    I run this command in a straight console:

    Get_WmiObject -computer COMPUTERNAME Win32_ComputerSystem | Format-List Name,Domain,Manufacturer,Model,SystemType

    I get the following result, which is what I want to see:

    Name : COMPUTERNAME
    Domain : DOMAIN
    Manufacturer : LENOVO
    Model : MODEL
    SystemType : X86-based PC

    Now if I run the same command in the aforementioned gui like such:

    $Results.Text = Get-WmiObject  -computer COMPUTERNAME Win32_ComputerSystem | Format-List Name,Domain,Manufacturer,Model,SystemType

    I get the following result in Results.Text:
    \\COMPUTERNAME\root\cimv2:Win32_ComputerSystem.Name="COMPUTER"

    I am sure it's just formatting but I am unsure what it would be.
    Again, I am sorry for bothering you all as you have been nothing but nice and polite.

  • #108905

    Participant
    Points: 158
    Helping Hand
    Rank: Participant

    You have unlimited vestiges of welcomeness. Format-* commands are for making text 'pretty' in the console, so they may not always look good as outputting somewhere else. Some things you can try:

    Wrap it in $(), this will execute the code first:

    $Results.Text = $(Get-WmiObject  -computer COMPUTERNAME Win32_ComputerSystem | Format-List Name,Domain,Manufacturer,Model,SystemType)
    

    Try not to squish it into one line and\or use Out-String to make sure it just text.

    $cs = Get-WmiObject -ComputerName COMPUTERNAME -Class Win32_ComputerSystem
    $output = $cs |
              Format-List Name,Domain,Manufacturer,Model,SystemType |
              Out-String
    
    $Results.Text = $output
    

You must be logged in to reply to this topic.