0

I built a simple GUI powershell script that runs exactly how I want it on many different computers (after installing all the right modules I also tried installing them with the -scope allusers).

when I compile the script using ps2exe it throws errors as if I haven't installed some of the modules I can use on the computer.

I also made sure to import the modules at the top of the script.

enter image description here

I assume this has something todo with scope but I am not sure how to configure the module/exe to be able to see Az.Accounts

Note: the powershell module sqlserver seems to work just fine no issues for the exe to find and use that one.

Edit: I just tested the script with no code other than just importing the module Az.Accounts and Connect-AzAccount. again this script runs just fine before converting with ps2exe

  • 1
    do not post images, instead give the text of your code. See SO guideline on [Why should I not upload images of code](https://meta.stackoverflow.com/questions/285551/)? –  Jan 13 '23 at 07:18
  • Could you provide that GUI Script code you're using because I have created a simple cmdlet of Connect-AzAccount with tenant id and subscription id and converted to `.exe` file using ps2exe and run .exe file successfully (login success) - https://i.imgur.com/JjiV7sR.png, https://i.imgur.com/OyuUi3y.png –  Jan 13 '23 at 07:39
  • I suspect that you have (as Microsoft recommend) installed the Azure module per-user. When you run the compiled .exe file elevated or in another context, it can't access your profile. Put some error trapping in to find out why the module isn't being installed. Please post your actual code and what settings you're compiling to exe – Scepticalist Jan 13 '23 at 08:02
  • "*when I compile the script using ps2exe*". `ps2exe` does ***not* compile** your PowerShell script! In fact it just embeds it the `exe` file and whenever your execute it, it unpacks the PowerShell file and places it in a temporary folder and executes it from there. Meaning that there isn't much benefit in using `ps2exe`. You might hide/encode your code and embed it in a standalone PowerShell script as well. – iRon Jan 13 '23 at 15:39
  • @Scepticalist Thanks! looks like that was it. I figured install-module Az -scope allusers -allowclobber would override the previous installed Az.accounts. but doing a full uninstall-module Az.accounts then reinstalling with allusers scope and it worked! guess somethings different because when I installed sqlserver module it didnt need the all users scope. – ryankennedy712 Jan 13 '23 at 18:16

1 Answers1

0

Glad @ryankennedy712 for identifying the cause of the issue and solving yourself. Posting it as answer that would help the community members who will face similar issue.

It was the overriding of previously installed the modules of Az.Accounts with the cmdlet Install-Module Az -scope allusers -allowclobber.

Re-installation (Full Uninstall and Install) of the module Az.Accountsalong with the scope of allusers works successfully.

when I installed sqlserver module it didnt need the all users scope.

There is no scope attribute for installing the SqlServer Module installation for AllUsers in the PowerShell cmdlet. To install that module for all the users in the system, the command should be run in Elevated PowerShell Session (as an administrator) mentioned by Microsoft in this MS Doc.