Force PStudio EXE to run from specified location only

Ask your PowerShell-related questions, including questions on cmdlet development!
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.
This topic is 2 years and 11 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.
Locked
ClipperXTP
Posts: 55
Last visit: Thu Jun 24, 2021 3:05 am

Force PStudio EXE to run from specified location only

Post by ClipperXTP »

I wish to stop my users from copying my Powershell Studio .exe from the network location where I update it, so I can control the version.

To do this I would like to do a check when the .exe is launched that the present working directory is the network share where it should be.

If it is not where it should be, I will display a message box saying please run from \\server\path and the form will close.

The problem I have is when I get the $pwd or get-directory I just get the drive letter and not the UNC path and the drive letter will vary depending on how the user has mapped it.

Can anyone suggest a way to check for the UNC path of the drive from which the powershell form is running?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Force PStudio EXE to run from specified location only

Post by jvierra »

Use Get-PsDrive
ClipperXTP
Posts: 55
Last visit: Thu Jun 24, 2021 3:05 am

Re: Force PStudio EXE to run from specified location only

Post by ClipperXTP »

Thanks. Ill look into how i can use that. Right now I can see that it shows a drive is mapped by not that my form is running from there.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Force PStudio EXE to run from specified location only

Post by jvierra »

You have the PWD so just use that to get the drive.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Force PStudio EXE to run from specified location only

Post by jvierra »

Actually your question is a bit ambiguous. Are you asking about the PowerShell Studio application or any application?

To easily find the executable of any application use the following.

[System.Windows.Forms.Application]::ExecutablePath
ClipperXTP
Posts: 55
Last visit: Thu Jun 24, 2021 3:05 am

Re: Force PStudio EXE to run from specified location only

Post by ClipperXTP »

Jvierra thanks for your reply.

I am referring to the .exe I build from PowerShell Studio.

I share the .exe (Which is the compiled Windows Form App) to my users in different regions.

They have access to the .exe on a network share say \\MyServer\MyShare$

This share may have different drive letters for different users, depending on how they map it in Windows.

So for User1, they may have mapped it to I:\
For user2, they may have mapped the network share to G:\

Both $PWD and [System.Windows.Forms.Application]::ExecutablePath return just the drive letter.

So if the .exe is is a folder \\MyServer\MyShare$\MyTool, $PWD and [System.Windows.Forms.Application]::ExecutablePath both return for example I:\MyTool or G:\MyTool which means I am not capturing the UNC path of the share.

I don't want users copying the .exe to another share as they will not be running the latest version.

I want the form /.exe to close if they are not running it from \\MyServer\MyShare$\MyTool

Does this make sense?

thanks in advance
User avatar
Alexander Riedel
Posts: 8473
Last visit: Tue Mar 19, 2024 1:15 am
Answers: 19
Been upvoted: 37 times

Re: Force PStudio EXE to run from specified location only

Post by Alexander Riedel »

Alexander Riedel
SAPIEN Technologies, Inc.
ClipperXTP
Posts: 55
Last visit: Thu Jun 24, 2021 3:05 am

Re: Force PStudio EXE to run from specified location only

Post by ClipperXTP »

Alexander / Jvierra many thanks for your help!

For anyone browsing this post, I was able to do what I wanted as follows:

$drive = Get-PSDrive -Name (get-location).Drive.Name

$root = if ($drive.DisplayRoot -ne $null) { $drive.DisplayRoot }
else { $drive.Root }


if($($root) -notlike "*MyUNCShare*") {$form1.Close()}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Force PStudio EXE to run from specified location only

Post by jvierra »

The current standard method for accessing an executable on a UNC path is to not use a mapped drive. Just specify the UNC path in the shortcut and deploy the shortcut to the “public Desktop” or to the users private desktop. This method is always best and prevents the issues you are having.

In modern Windows we have not used mapped drives for more than 10 years except with legacy apps that are no updated to modern Windows methods.

I also strongly recommend deploying the shortcuts using Group Policy as it is easier to maintain even when the user’s desktop is turned off. GP can finely target per user or per machine, so it eliminates all ambiguity in management of the Windows desktop.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Force PStudio EXE to run from specified location only

Post by jvierra »

Here is the fast way to get the PsDrive

Get-PsDrive "$([System.Windows.Forms.Application]::ExecutablePath[0])"

The following is teh easy way to get the root:

(Get-PsDrive "$([System.Windows.Forms.Application]::ExecutablePath[0])").Root
This topic is 2 years and 11 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.
Locked