Get-ScriptDirectory function and Join-Path in exe

This forum can be browsed by the general public. Posting is limited to current SAPIEN license holders with active maintenance and does not offer a response time guarantee.
Forum rules
DO NOT POST LICENSE NUMBERS, ACTIVATION KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.

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 4 years and 6 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.
User avatar
DanielLeber
Posts: 20
Last visit: Thu May 18, 2023 8:36 am

Get-ScriptDirectory function and Join-Path in exe

Post by DanielLeber »

To help you better we need some information from you.

*** Please fill in the fields below. If you leave fields empty or specify 'latest' rather than the actual version your answer will be delayed as we will be forced to ask you for this information. ***

Product, version and build: Powershell Studio 2019 Version 5.6.167
32 or 64 bit version of product: 64bit
Operating system: Windows 10 Build 1709
32 or 64 bit OS: 64bit

*** Please add details and screenshots as needed below. ***

DO NOT POST SUBSCRIPTIONS, KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM

Hi all :D

I have a script that I have been building into an exe with Powershell Studio 2019

The script runs without errors and the cctk file will be found.

But, when I build into an executable, the source location can’t be found.

Is this a known issue or is there an an error in the script?

Best regards,
Daniel



function Get-ScriptDirectory
{
[OutputType([string])]
param ()
if ($host.Name -eq 'PrimalScriptHostImplementation')
{
if ($null -ne $hostinvocation)
{
Split-Path $hostinvocation.MyCommand.path
}
else
{
Split-Path $script:MyInvocation.MyCommand.Path
}
}
else
{
$PSScriptRoot
}
}
[string]$PSScriptRoot = Get-ScriptDirectory


function Get-PasswordIsSet
{
if ((Test-Path (Join-Path -Path $PSScriptRoot -ChildPath "cctk\cctk.exe")) -eq $true)
{
write-host 'File exist' -ForegroundColor Red
}

$proc = (Start-Process -FilePath `"$(Join-Path -Path $PSScriptRoot -ChildPath "cctk" | Join-Path -ChildPath "cctk.exe")`" -ArgumentList "--setuppwd=" -Wait -PassThru); $proc.WaitForExit(); $ExitCode = $proc.ExitCode;
Write-Host $ExitCode -ForegroundColor Red


}
Get-PasswordIsSet
Attachments
PSScriptRoot.png
PSScriptRoot.png (69.82 KiB) Viewed 2215 times
User avatar
Alexander Riedel
Posts: 8479
Last visit: Thu Mar 28, 2024 9:29 am
Answers: 20
Been upvoted: 37 times

Re: Get-ScriptDirectory function and Join-Path in exe

Post by Alexander Riedel »

It has something to do with re-setting $PSScriptRoot. It seems Powershell in some instances does not like that.
Using a different variable name solves the problem.

Code: Select all

function Get-ScriptDirectory
{
	[OutputType([string])]
	param ()
	if ($host.Name -eq 'PrimalScriptHostImplementation')
	{
		if ($null -ne $hostinvocation)
		{
			Split-Path $hostinvocation.MyCommand.path
		}
		else
		{
			Split-Path $script:MyInvocation.MyCommand.Path
		}
	}
	else
	{
		$PSScriptRoot
	}
}
[string]$MyScriptFolder = Get-ScriptDirectory

$MyScriptFolder

function Get-PasswordIsSet
{
	if ((Test-Path (Join-Path -Path $MyScriptFolder -ChildPath "cctk\cctk.exe")) -eq $true)
	{
		write-host 'File exist' -ForegroundColor Red
	}
	
	#$proc = (Start-Process -FilePath `"$(Join-Path -Path $PSScriptRoot -ChildPath "cctk" | Join-Path -ChildPath "cctk.exe")`" -ArgumentList "--setuppwd=" -Wait -PassThru); $proc.WaitForExit(); $ExitCode = $proc.ExitCode;
	#Write-Host $ExitCode -ForegroundColor Red
	
	
}
Get-PasswordIsSet
Alexander Riedel
SAPIEN Technologies, Inc.
User avatar
DanielLeber
Posts: 20
Last visit: Thu May 18, 2023 8:36 am

Re: Get-ScriptDirectory function and Join-Path in exe

Post by DanielLeber »

it could be quite simple :roll:

Thank you for your quick response!
This topic is 4 years and 6 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.