JT-66
JT-66

Reputation: 31

PowerShell Script Converted To EXE Works But With Error Shown

I have a PowerShell script that works as expected when ran from PowerShell prompt (not compiled into an exe).

It also works when complied to an executable but shows an error as the file is uploaded.

Here is the code:

$subscriptionId = "xxxxxxxxxxxxxxxxxxxxx"
$tenantId = "xxxxxxxxxxxxxxxxxxxxxxx"
$client = "xxxxxxxxxxxxxxxxxxxxxxxxxx"
$accountName = "xxxxx"
$containerName = "xxxxxxxx"
$secret = "xxxxxxxxxxxxxxxxxxxxxxxx"
az cloud set --name XXXXX
az login --service-principal --username $client --password "$secret" --tenant $tenantId
az account set --subscription $subscriptionId
Add-Type -AssemblyName System.Windows.Forms
#$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{ 
    InitialDirectory = [Environment]::GetFolderPath('MyDocuments') 
    Filter = 'Documents (*.docx)|*.docx|SpreadSheet (*.xlsx)|*.xlsx|zip (*.zip)|*.zip|7zip (*.7z)|*.7z|Z03 Files (*.z03)|*.z03|PDF Files (*.pdf)|*.pdf|ISO Files (*.iso)|*.iso'
}
$FileBrowser.ShowDialog()
$FilePath = $FileBrowser.FileName


az storage blob upload --file $FilePath --account-name $accountName --container-name $containerName/XXXX/ --auth-mode login --overwrite

Here is the command I run to compile it. It errors the same way whether I uses the CLI ps2exe or the win-ps2exe complier.

ps2exe .\Upload-Files-To-Blob.ps1 Test-Upload.exe -iconFile "upload_icon.ico" -title "Test Upload Utility" -copyright "Copyright (C) 2024" -version "1.0" -verbose

And here is the error when I DoubleClick the executable and run it. As stated above, it still works but the user sees this erroneous error message as the file uploads successfully.

Any ideas? I want the progress bar to show so that the user will know that the script is running rather than appear to be hung.

OK
ERROR:
ERROR: Alive[                                                                ]  0.1203%
ERROR: Alive[                                                                ]  0.2406%
ERROR: Alive[######################                                          ]  35.1319%
ERROR: Alive[######################                                          ]  35.2522%
ERROR: Alive[######################                                          ]  35.3725%
ERROR: Alive[######################                                          ]  35.4928%
ERROR: Alive[##############################################################  ]  98.2971%
ERROR: Alive[##############################################################  ]  98.4174%
ERROR: Alive[############################################################### ]  98.5377%
ERROR: Alive[############################################################### ]  98.6581%
ERROR: Alive[############################################################### ]  98.7784%                                                              

Upvotes: 0

Views: 47

Answers (1)

MortenB
MortenB

Reputation: 3547

az is not a binary it's python wrapped in a msi-package, AFAIK ps2exe.exe only support self contained binaries/dlls you can add file by file.

where.exe az
C:\Program Files\Microsoft SDKs\Azure\CLI2\wbin\az
C:\Program Files\Microsoft SDKs\Azure\CLI2\wbin\az.cmd

cat "C:\Program Files\Microsoft SDKs\Azure\CLI2\wbin\az"
#!/usr/bin/env bash

AZ_INSTALLER=MSI "$(dirname "${BASH_SOURCE[0]}")/../python.exe" -IBm azure.cli "$@"

So you should install it separately, I have a program for silently installing az-cli, You do the login in your script so it work fine.

 cat .\azcli-install.ps1
<# INSTALL azcli #>
$msi = "https://aka.ms/installazurecliwindowsx64"
$arguments = "/i `"$msi`" /quiet /passive"
Start-Process msiexec.exe -ArgumentList $arguments -Wait

You then add az path checker in your *.ps1 aka:

if ((Get-Command "az" -ErrorAction SilentlyContinue) -eq $null) { 
   Write-error("Unable to find az in your PATH")
   Exit 1
}

Upvotes: 0

Related Questions