Nested scriptblocks

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 7 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
goliska
Posts: 35
Last visit: Mon Oct 09, 2023 2:07 am

Nested scriptblocks

Post by goliska »

Product, version and build: Powershell Studio 2016 5.2.120
32 or 64 bit version of product: 64
Operating system: Windows 10
32 or 64 bit OS: 64
PowerShell Version: 5

Might be the wrong forum, its not an issue with PSS, just regular powershell.
Im trying to run an external command (.exe-file) within the buttonPressed-event.
Since it runs fine in a regular powershellprompt, i realize it has to relate to that the eventhandlers aren't functions, but scriptblocks.

I used:
$output = & "psfile.exe" "$psfileargument" | Out-String
first, and by switching to creating a scriptblock my argument expands, but it still gives NativeCommandError.
Its probably a no-brainer, but any advice would be great.

$btnList_Click = {
[string]$cmd = "psfile $psfileargument"
[scriptblock]$sb = [scriptblock]::Create($cmd)

$output = Invoke-Command -ScriptBlock $sb | out-string
}


I don't want start-process since it doesn't give the possibility to fetch stdoutput to a variable (textbox) AFAIK.
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Nested scriptblocks

Post by davidc »

[POST MOVED TO THE POWERSHELL GUI FORUM BY MODERATOR]
David
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Nested scriptblocks

Post by jvierra »

User avatar
goliska
Posts: 35
Last visit: Mon Oct 09, 2023 2:07 am

Re: Nested scriptblocks

Post by goliska »

I'm aware of that one, but I need the output. Start-process can only redirect to files afaik?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Nested scriptblocks

Post by jvierra »

goliska wrote:I'm aware of that one, but I need the output. Start-process can only redirect to files afaik?
  1. $text=Invoke-Expression "cmd /c dir `"c:\program files`""
  2. $path='c:\program files'
  3. $text=Invoke-Expression "cmd /c dir `"$path`""
Without using escapes:
  1. $path='c:\program files'
  2. $cmd='CMD /C dir "{0}"' -f $path
  3. $text=Invoke-Expression $cmd
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Nested scriptblocks

Post by jvierra »

To clarify You haven't used the quotes correctly. It has nothing to do with script blocks.
My examples illustrate a method for avoiding quoting wars.
User avatar
goliska
Posts: 35
Last visit: Mon Oct 09, 2023 2:07 am

Re: Nested scriptblocks

Post by goliska »

jvierra wrote:To clarify You haven't used the quotes correctly. It has nothing to do with script blocks.
My examples illustrate a method for avoiding quoting wars.
Thanks jvierra, the quotations weren't the issue here i think, i had no spaces. but obviously whitespaces should be taken into account for, so thanks.

My guess on scriptblock-related issues came from https://stackoverflow.com/questions/255 ... criptblock , but i guess i was wrong.
Turns out It actually did work, i was simply concatenating the filepath incorrectly so i didnt get expected output from Sysinternal's PSFILE.

on the other hand, i STILL get the same errormesssage in Output-pane in PSS.
ERROR: psfile :
line (1, 1): ERROR: At Line: 1 char: 1
ERROR: + psfile "c:\temp\SysInternals\subfolder1"
ERROR: + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR:     + CategoryInfo          : NotSpecified: (:String) [], RemoteException
ERROR:     + FullyQualifiedErrorId : NativeCommandError
ERROR:
ERROR: psfile v1.02 - psfile
ERROR: Copyright ® 2001 Mark Russinovich
ERROR: Sysinternals
This doesn't happen in a regular prompt.
Not sure what the error is about, is it merely a reaction on the exitcode from the externalcommand or something?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Nested scriptblocks

Post by jvierra »

You cannot use Invoke-Command to do this. It won't work.

I showed you how to make it work. I do not know of any way to show you how this needs to work. The code I posted is guaranteed to work but will not if you insist on wrapping in in another incorrectly used command.

Your assumption about script blocks is completely faulty. You do not need a script block.
User avatar
goliska
Posts: 35
Last visit: Mon Oct 09, 2023 2:07 am

Re: Nested scriptblocks

Post by goliska »

jvierra wrote:You cannot use Invoke-Command to do this. It won't work.

I showed you how to make it work. I do not know of any way to show you how this needs to work. The code I posted is guaranteed to work but will not if you insist on wrapping in in another incorrectly used command.

Your assumption about script blocks is completely faulty. You do not need a script block.
Yes my assumption was probably wrong.
But it still, both invoke-expression and invoke-command works, your suggestion includes quotationsmarks though, which is needed when blanks exists in the pathname.
Both alternatives returns error in output though. no big deal as long as it works..
  1. $btnList_Click = {
  2.     $textbox1.Text = ""
  3.     #Alt. 1 - using invoke-expression, works
  4.     $cmd = 'psfile "{0}"' -f $psfileargument ## A System.String containing c:\temp\SysInternals\subfolder1 with quotations
  5.     $output = Invoke-Expression $cmd
  6.    
  7.     #Alt. 2 - using invoke-command, works too
  8.     [string]$cmd = "psfile $psfileargument"
  9.     [scriptblock]$sb = [scriptblock]::Create($cmd)
  10.     $output = Invoke-Command -ScriptBlock $sb | out-string
  11.    
  12.     #Output to multiline textbox.
  13.     foreach ($line in $output) {
  14.         $textbox1.Text += "$line`r`n"
  15.     }
  16.     $btnKill.Enabled = $true
  17. }
ERROR: psfile :
line (1, 1): ERROR: At Line: 1 char: 1
ERROR: + psfile c:\temp\SysInternals\subfolder1
ERROR: + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR:     + CategoryInfo          : NotSpecified: (:String) [], RemoteException
ERROR:     + FullyQualifiedErrorId : NativeCommandError
ERROR:
ERROR: psfile v1.02 - psfile
ERROR: Copyright ® 2001 Mark Russinovich
ERROR: Sysinternals
ERROR:
This topic is 7 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked