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 SUBSCRIPTION NUMBERS, LICENSE 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.
User avatar
212307
Posts: 7
Joined: Wed Feb 07, 2018 11:40 am

Get-ScriptDirectory function and Join-Path in exe

Post by 212307 » Fri Sep 06, 2019 12:45 am

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 353 times

User avatar
Alexander Riedel
Posts: 7023
Joined: Tue May 29, 2007 4:43 pm

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

Post by Alexander Riedel » Fri Sep 06, 2019 7:15 am

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
212307
Posts: 7
Joined: Wed Feb 07, 2018 11:40 am

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

Post by 212307 » Fri Sep 06, 2019 7:37 am

it could be quite simple :roll:

Thank you for your quick response!