From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Use Powershell to grant LabVIEW executable (application) higher-level user or admin credentials

I had a need to grant a LV executable (EXE) and elevate its network credentials from a generic user logon to a higher-level user privilege (like mine) or admin that has the privilege to access the network share.

 

I came up on forum discussions in StackOverflow or StackExchange that Windows Powershell (came preinstalled with Win 7 and up) can do the job. Checkout: https://stackoverflow.com/questions/55188729/how-to-use-powershell-to-grant-windows-application-or-e...

 

After several forum discussions I was given a sample code to try in PS console:

$App = "c:\path\to\[app.exe]"
$Credentials = Get-Credential
$WorkingDir = Split-Path $app
$Exe = Split-Path $app -Leaf
Start-Process -FilePath $Exe -WorkingDirectory $WorkingDir -credential $Credentials

This worked inside the PS console. The LV app inherited my credentials.

 

But how to integrate this programmatically in LV so you won't have to be a console nerd every time and a lowly user happy with just the GUI? Solution is below LV code snip:

 

Use PowerShell console in VI via System Exec NodeUse PowerShell console in VI via System Exec Node

 

Note: I added the case structure to detect if I am running an EXE or VI as I needed to debug the code before building EXE. It is optional if you hard-link to your app-kind and already know in advance where it is located.

 

Also, make sure you use "cmd /k" and not "cmd /c" to keep the CMD window open when executing/passing the PS arguments - but you can try both for fun.

 

I also found out that for the "-Message" switch for [Get-Credential] cmdlet to work without error, your PS version must be 5.0 or higher. PS version 3.0 supported the "-UserName" switch.

 

Enjoy!

Message 1 of 3
(2,971 Views)

Ooops wrong purpose. Ignore

This may also be of use: https://forums.ni.com/t5/Example-Program-Drafts/Run-as-administrator-elevated-user-in-Windows-using-...



Using LV2018 32 bit

Highly recommended open source screen capture software (useful for bug reports).

https://getsharex.com/
0 Kudos
Message 2 of 3
(2,944 Views)

Thanks for the recommendation. I did try this (or one VI I got from this board) like the one below to elevate the permission of another VI using Win32 API:Grant LabVIEW VI User Credential Using Win32 API.JPG

 

I didn't like this method as it can be prone to attack and it didn't work quite as well as the Powershell method. Another, the said Windows DLL may become deprecated, renamed and unsupported in the future. Having the user logon with his higher privileged logon from the generic logon is much more robust and secure, to me.

 

Whatever case may be, using Powershell, as is now the administrator scripting language baked in .Net frameworks from Win7 to Win10. Powershell is also now an open source and available for Linux and Mac. There is so much to learn from it. Thank goodness I discovered its use. Now if NI will create a Powershell-native node instead of using the System Exec node to invoking it.

Message 3 of 3
(2,927 Views)