Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, June 11, 2019 5:10 PM
$MyScriptName = $MyInvocation.MyCommand.NameAbove is snippet code of powershell which works fine and result the script name in the $MyScriptName variable.once we convert .ps1 script to .exe file then execution result the null value for the variable $MyScriptName.
All replies (4)
Wednesday, June 12, 2019 11:43 AM ✅Answered
Thanks for everyone's time.
Issue got resolved by using following snippet - [Environment]::GetCommandLineArgs()[0]
NOTE - Above snippet only works properly when you have converted powershell script with above small piece snippet to .ext extension ( That what was my requirement )
[Environment]::GetCommandLineArgs()[0]
Tuesday, June 11, 2019 5:26 PM
This is only valid when calling a script file from PowerShell Most utilities that crate executables just load the text and execute it so there is no "MyInvocation".
Contact the vendor of the exe maker to find out if they have a variable defined that tells the location of the EXE or use the "application" object to get the EXE location.
\(ツ)_/
Tuesday, June 11, 2019 5:32 PM
There are two variables that will help.
[environment]::CurrentDirectory
[environment]::CommandLine
The "CurrentDirectory" variable will be set to the exe path or to the "WorkingDirectory" if one was set in a shortcut or the task scheduler or will be the folder that the exe was called from.
The "CommandLine will contain the path of the EXE.
The following will get the path to the EXE:
[environment]::CommandLine |split-path
\(ツ)_/
Wednesday, June 12, 2019 7:54 AM
Hi,
Was your issue resolved?
If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.
If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.
If no, please reply and tell us the current situation in order to provide further help.
Best Regards,
Lee
Just do it.