Reputation: 1
I am working on my first GUI in Powershell. I have used small scripts with the Azure modules in the past so I am even more confused as to why this is breaking. In the script below there are two places that I tried to import and use the connect-azuread cmdlet so it is commented in the second spot but I attempted it in both places. The script is meant to present a GUI to select a csv of UPNs to find additional info in the azure tenant and return it in another csv. No matter where I place the connect-azuread command, I recieve the error: "You must call the Connect-AzureAD cmdlet before calling any other cmdlets." When I run the script it does prompt me to sign into Azure with the standard Microsoft login page but it apparently does not hold onto the credential.
This is the code that I tried:
Install-Module AzureAD -Force | Out-Null
Import-Module AzureAD | Out-Null
Connect-AzureAD
Add-Type -AssemblyName System.Windows.Forms
$LocationForm = New-Object system.Windows.Forms.Form
$LocationForm.ClientSize = '500,300'
$LocationForm.text = "Azure Information Pull"
$LocationForm.BackColor ='#ffffff'
$Title = New-Object System.Windows.Forms.Label
$Title.text = "Retrieving Data From Azure Tenant"
$Title.AutoSize = $true
$Title.Location = New-Object System.Drawing.Point(20,20)
$Title.Font = 'Microsoft Sans Serif,13'
$Description = New-Object System.Windows.Forms.Label
$Description.Text = "Retrieve UPN, Display Name, Email, and EmployeeID from Azure Tenant."
$Description.AutoSize = $False
$Description.Width = 450
$Description.Height = 50
$Description.location = New-Object System.Drawing.Point(20,50)
$Description.Font = 'Microsoft Sans Serif,10'
#$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog
#$FileBrowser.Title = "Select a file"
#$FileBrowser.InitialDirectory = [Environment]::GetFolderPath('Desktop')
#$FileBrowser.Filter = "CSV (*.csv)| *.csv"
#$SelectedFile = $FileBrowser.FileName
Function Get-FileName()
{
[System.Reflection.Assembly]::LoadWithPartialName(“System.windows.forms”) |
Out-Null
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = [Environment]::GetFolderPath('Desktop')
$OpenFileDialog.filter = "CSV (*.csv)| *.csv"
$OpenFileDialog.ShowDialog() | Out-Null
$global:Input = $OpenFileDialog.filename
$Input
} #end function Get-FileName
$InputFileBtn = New-Object System.Windows.Forms.Button
$InputFileBtn.BackColor = '#a4ba67'
$InputFileBtn.Text = "Select File"
$InputFileBtn.Width = 90
$InputFileBtn.height = 30
$InputFileBtn.Location = New-Object System.Drawing.Point(370,250)
$InputFileBtn.Font = 'Microsoft Sans Serif,10'
$InputFileBtn.ForeColor = "#ffffff"
$InputFileBtn.Add_Click({Get-FileName})
$cancelBtn = New-Object system.Windows.Forms.Button
$cancelBtn.BackColor = "#ffffff"
$cancelBtn.text = "Finish"
$cancelBtn.width = 90
$cancelBtn.height = 30
$cancelBtn.location = New-Object System.Drawing.Point(260,250)
$cancelBtn.Font = 'Microsoft Sans Serif,10'
$cancelBtn.ForeColor = "#000"
$cancelBtn.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$LocationForm.CancelButton = $cancelBtn
$LocationForm.Controls.AddRange(@($Title,$Description,$cancelBtn,$InputFileBtn))
[void]$LocationForm.ShowDialog()
#Begin Script
#Install-Module AzureAD -Force | Out-Null
#Import-Module AzureAD | Out-Null
#Connect-AzureAD
$OutputLocation = Split-Path -Path $global:Input
$UserList = import-csv -path $global:Input
foreach($user in $UserList){
Get-AzureADUser -ObjectID $user.upn | select UserPrincipalName,Displayname,mail, @{Name = 'EmployeeId'; Expression = {$_.ExtensionProperty.employeeId}} | export-csv "$OutputLocation\output.csv" -append -noTypeInformation}
I do not expect to get the error to connect to the azure ad module when I have already made the connection.
Upvotes: 0
Views: 3485
Reputation: 16109
I tried to reproduce the same in my environment and got the results successfully like below:
Connect-AzureAD
Add-Type -AssemblyName System.Windows.Forms
$LocationForm = New-Object system.Windows.Forms.Form
$LocationForm.ClientSize = '500,300'
$LocationForm.text = "Azure Information Pull"
$LocationForm.BackColor ='#ffffff'
$Title = New-Object System.Windows.Forms.Label
$Title.text = "Retrieving Data From Azure Tenant"
$Title.AutoSize = $true
$Title.Location = New-Object System.Drawing.Point(20,20)
$Title.Font = 'Microsoft Sans Serif,13'
$Description = New-Object System.Windows.Forms.Label
$Description.Text = "Retrieve UPN, Display Name, Email, and EmployeeID from Azure Tenant."
$Description.AutoSize = $False
$Description.Width = 450
$Description.Height = 50
$Description.location = New-Object System.Drawing.Point(20,50)
$Description.Font = 'Microsoft Sans Serif,10'
[Environment]::GetFolderPath('Desktop')
Function Get-FileName()
{
[System.Reflection.Assembly]::LoadWithPartialName(“System.windows.forms”) |
Out-Null
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = [Environment]::GetFolderPath('Desktop')
$OpenFileDialog.filter = "CSV (*.csv)| *.csv"
$OpenFileDialog.ShowDialog() | Out-Null
$global:Input = $OpenFileDialog.filename
$Input
}
$InputFileBtn = New-Object System.Windows.Forms.Button
$InputFileBtn.BackColor = '#a4ba67'
$InputFileBtn.Text = "Select File"
$InputFileBtn.Width = 90
$InputFileBtn.height = 30
$InputFileBtn.Location = New-Object System.Drawing.Point(370,250)
$InputFileBtn.Font = 'Microsoft Sans Serif,10'
$InputFileBtn.ForeColor = "#ffffff"
$InputFileBtn.Add_Click({Get-FileName})
$cancelBtn = New-Object system.Windows.Forms.Button
$cancelBtn.BackColor = "#ffffff"
$cancelBtn.text = "Finish"
$cancelBtn.width = 90
$cancelBtn.height = 30
$cancelBtn.location = New-Object System.Drawing.Point(260,250)
$cancelBtn.Font = 'Microsoft Sans Serif,10'
$cancelBtn.ForeColor = "#000"
$cancelBtn.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$LocationForm.CancelButton = $cancelBtn
$LocationForm.Controls.AddRange(@($Title,$Description,$cancelBtn,$InputFileBtn))
[void]$LocationForm.ShowDialog()
$OutputLocation = Split-Path -Path $global:Input
$UserList = import-csv -path $global:Input
foreach($user in $UserList){
Get-AzureADUser -ObjectID $user.upn | select UserPrincipalName,Displayname,mail, @{Name = 'EmployeeId'; Expression = {$_.ExtensionProperty.employeeId}} | export-csv "$OutputLocation\output1.csv" -append -noTypeInformation}
The output file is exported successfully as below:
The error "You must call the Connect-AzureAD
cmdlet before calling any other cmdlets" usually occurs if connection to the Azure AD Account is not successful.
Try Connect-AzureAD
by using below command:
$credentials = Get-Credential
Connect-AzureAD -Credential $credentials
Otherwise, try to Connect Azure AD like below:
Connect-AzureAd -TenantId TenantID
If still the issue persists, try re-install the AzureAD module:
Uninstall-Module AzureAD
Install-Module AzureAD
Import-Module AzureAD
Upvotes: 1