Hi,
I have this mmc I would like to call (also some exes) in a form (via a button buttonMMC).
It launches fine locally (on my machine) but not when launched on a remote server (where it is used by several users).
This is part of begin of script:
$ScriptPath = Split-Path $hostinvocation.MyCommand.path
$Tools = $ScriptPath + "tools"
This is the actual button:
$buttonMMC_Click = {
& $Tools\MMC.msc
}
So, as said, the script locally runs ok, but on a remote server (location there is e:\scripts\MyTool\Tools\MMC.msc I get an error:
The term 'E:\Scripts\MyToolTooltools\MMC.msc' is not recognized as the name of a cmdlet, function, script file, or operable program
So it doesn't add the \ between Mytool and tools (where it does locally). Tried already to set
$Tools = $ScriptPath + "\tools" but then I get the error
The term '\Tools\MMC.msc' is not recognized as the name of a cmdlet, function
Please advise.
Steven
Call external msc/exe from within tool
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: Call external msc/exe from within tool
Yu cannot remotely launch a GUI application or an interactive application. It is just not possible.
Are you really trying to say the script doesn't work when copied to another server and a user tries to run it?
Try this with your code:
Are you really trying to say the script doesn't work when copied to another server and a user tries to run it?
Try this with your code:
- $buttonMMC_Click = {
- $ScriptPath = Split-Path $hostinvocation.MyCommand.path
- $Tools = Join=Path $ScriptPath 'tools\mmc.msc'
- & $Tools
- }
Re: Call external msc/exe from within tool
Thanks. I noticed (didn't change the code yet) an error when starting the project:
ERROR: Split-Path : Cannot bind argument to parameter 'Path' because it is null.
MainForm.psf (4, 26): ERROR: At Line: 4 char: 26
This line (4) is
$ScriptPath = Split-Path $hostinvocation.MyCommand.path
Please advise on that.
Steven
ERROR: Split-Path : Cannot bind argument to parameter 'Path' because it is null.
MainForm.psf (4, 26): ERROR: At Line: 4 char: 26
This line (4) is
$ScriptPath = Split-Path $hostinvocation.MyCommand.path
Please advise on that.
Steven
Re: Call external msc/exe from within tool
Copy this code and paste at a prompt. You will see the textbox is blank.
Place same code in a file and execute the file and you will get the path.
Place same code in a file and execute the file and you will get the path.
- #requires -Version 5
- using namespace System.Windows.Forms
- Add-Type -AssemblyName System.Windows.Forms
- $form1 = [Form]::new()
- $form1.StartPosition = 'CenterScreen'
- $textbox1 = [Textbox]::new()
- $textbox1.Width = $form1.Width
- $form1.Controls.Add($textbox1)
- $textbox1.Text = $MyInvocation.MyCommand.Path
- $form1.ShowDialog()
Re: Call external msc/exe from within tool
Thanks but when I enter the $MyInvocation.Mycommand so it becomes
$ScriptPath = Split-Path $MyInvocation.MyCommand.path
I get this at startup:
ERROR: + $ScriptPath = Split-Path $MyInvocation.MyCommand.path
ERROR: + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR: + CategoryInfo : InvalidData: (:) [Split-Path], ParameterBindingValidationException
ERROR: + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.SplitPathCommand
$ScriptPath = Split-Path $MyInvocation.MyCommand.path
I get this at startup:
ERROR: + $ScriptPath = Split-Path $MyInvocation.MyCommand.path
ERROR: + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR: + CategoryInfo : InvalidData: (:) [Split-Path], ParameterBindingValidationException
ERROR: + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.SplitPathCommand
Re: Call external msc/exe from within tool
If you want the path to work in both a PSF and a PS1 then use this:
- $buttonMMC_Click = {
- if ($hostinvocation) {
- $p=Split-Path $hostinvocation.MyCommand.path
- } else {
- $p=Split-Path $script:MyInvocation.MyCommand.Path
- }
- [System.Windows.Forms.MessageBox]::Show($p)
- }