Hi, i wrote a script in powershell using Windows Forms. The buttons triggers inside the Visual Studio. When i try to open the script on a terminal or the .exe the button doesn’t works.
Windows 11
Visual Studio 2022
This is my code.
. (Join-Path $PSScriptRoot ‘Form1.designer.ps1’)
$Form1_Load = {
}
$button1_Click = {
Get-Dependencies
}
$button2_Click = {
Set-Authentications
}
Function Get-Dependencies {
$ModulesArray = “ExchangeOnlineManagement”, “AzureAD”, “ImportExcel”, “Az”, “PowerShellGet”, “MicrosoftTeams”
foreach ($module in $ModulesArray) {
if (Get-Module -ListAvailable -Name $module) {
[System.Windows.MessageBox]::Show(“Módulo $module já instalado.”,‘Warning’,‘Ok’,‘Information’)
} else {
[System.Windows.MessageBox]::Show(“Instalando módulo $module.”)
try {
Install-Module $module
wait
} catch {
##throw $_.Exception.Message
Write-Output “”
[System.Windows.MessageBox]::Show(“Não foi possível instalar o módulo $module. Tente executar o script como administrador.”,‘Error’,‘Ok’,‘Error’)
}
}
}
}
Function Set-Authentications {
Import-Module AzureAD
try {
Connect-AzureAD -erroraction ‘silentlycontinue’
Connect-ExchangeOnline
Connect-MicrosoftTeams
} catch {
[System.Windows.MessageBox]::Show(“Não foi possivel se autenticar! Tente executar o script novamente.”,‘Error’,‘Ok’,‘Error’)
Break
}
Write-Output “”
}
$Form1.ShowDialog()
EDIT: Looks like the $PSScriptRoot doesn’t work, it’s null.
On VS2022
On exe
Now i’m able to get this value
But none of the elements works
Only running inside the visual studio 2022