Author Posts

August 15, 2018 at 2:47 pm

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

August 15, 2018 at 2:58 pm

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

or

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

😉

August 15, 2018 at 3:29 pm

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. =(

August 15, 2018 at 4:22 pm

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.

August 15, 2018 at 4:32 pm

hmmm.

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

August 15, 2018 at 5:05 pm

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!

August 15, 2018 at 5:33 pm

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.

August 15, 2018 at 7:54 pm

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.

August 16, 2018 at 4:31 pm

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.

August 16, 2018 at 4:34 pm

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

August 16, 2018 at 6:19 pm

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

August 17, 2018 at 5:40 pm

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.

August 17, 2018 at 5:50 pm

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.

August 17, 2018 at 7:25 pm

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