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.
Nested scriptblocks
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.
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.
Re: Nested scriptblocks
[POST MOVED TO THE POWERSHELL GUI FORUM BY MODERATOR]
David
SAPIEN Technologies, Inc.
SAPIEN Technologies, Inc.
Re: Nested scriptblocks
I'm aware of that one, but I need the output. Start-process can only redirect to files afaik?
Re: Nested scriptblocks
goliska wrote:I'm aware of that one, but I need the output. Start-process can only redirect to files afaik?
- $text=Invoke-Expression "cmd /c dir `"c:\program files`""
- $path='c:\program files'
- $text=Invoke-Expression "cmd /c dir `"$path`""
- $path='c:\program files'
- $cmd='CMD /C dir "{0}"' -f $path
- $text=Invoke-Expression $cmd
Re: Nested scriptblocks
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.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.
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: SysinternalsThis 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?
Re: Nested scriptblocks
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.
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.
Re: Nested scriptblocks
Yes my assumption was probably wrong.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.
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..
- $btnList_Click = {
- $textbox1.Text = ""
- #Alt. 1 - using invoke-expression, works
- $cmd = 'psfile "{0}"' -f $psfileargument ## A System.String containing c:\temp\SysInternals\subfolder1 with quotations
- $output = Invoke-Expression $cmd
- #Alt. 2 - using invoke-command, works too
- [string]$cmd = "psfile $psfileargument"
- [scriptblock]$sb = [scriptblock]::Create($cmd)
- $output = Invoke-Command -ScriptBlock $sb | out-string
- #Output to multiline textbox.
- foreach ($line in $output) {
- $textbox1.Text += "$line`r`n"
- }
- $btnKill.Enabled = $true
- }
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: