Specify user when building exe

Tool: Visual Studio Code
Version: 5.29.4

Is there a way to specify a RunAs user when packaging the exe? So that when the EXE is run it is always run as that specific user.

Also, in the Resources = [string[]]@() section of the package.psd1 do all files have to be in the root directory or can you specify sub folders and files ie. Resources = [string[]]@("file1.jpg","folder\file2.jpg")

There isn’t a way to specify a user for an exe. You’d have to store the credentials in it somehow, which I would advise against.

For resources, you should be able to do nested directories. I’m about 99% sure I tested this but I’d be curious if you are running into problems.

Not to worried about stored credentials for our purpose. Possible feature in the future? :slight_smile:

Haven’t tried the nested directories yet, just thought i’d ask.

Sure. It’s something we can look into. We would likely be able to do some impersonation within the process to achieve this. I’ll open a feature request for it.

Awesome. Thanks.

Another question about including resources when packaging. The documentation shows how to reference the resource in XAML. How would you reference these files in the PowerShell script?

Specifically, I am creating a WPF UI for a script and need to Load some DLLs (ie [void][System.Reflection.Assembly]::LoadFrom('assembly\MaterialDesignThemes.Wpf.dll'). But i can’t seem to get the sourcing correct in the script for it to work in an exe.

You’ll need to read the assembly embedded resource directly and then use Assembly.Load to load the bytes.

Here’s some C# code that you should be able to adapt to PowerShell.

   using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("EmbedAssembly.EmbeddedAssemblies.Newtonsoft.Json.dll"))
        var assemblyData = new Byte[stream.Length];
        stream.Read(assemblyData, 0, assemblyData.Length);
        return Assembly.Load(assemblyData);

Sometimes the hardest part is finding the proper manifest resource name. You can list those with this method call.

    foreach ($resource in [Assembly]::GetExecutingAssembly().GetManifestResourceNames()) {
        Write-Host "Resource: $resource"

Thanks. I’ll have to look into that a little more. This is the first time I have had to work with DLLs in a script.