为什么我在连接到服务后收到Connect-AzureAD错误?

pbpqsu0x  于 2022-11-17  发布在  其他
关注(0)|答案(1)|浏览(290)

我正在Powershell中开发我的第一个GUI。我过去曾在Azure模块中使用过小脚本,所以我更不明白为什么会出现这种情况。在下面的脚本中,有两个地方我尝试导入并使用connect-azuread cmdlet,因此在第二个地方对其进行了注解,但我在这两个地方都尝试了。该脚本旨在提供一个GUI,以选择UPN的csv,从而在Azure租户中查找其他信息,并在另一个csv中返回这些信息。无论我将connect-azuread命令放在何处,都会收到错误:“在调用任何其他cmdlet之前,您必须调用Connect-AzureAD cmdlet。”当我运行该脚本时,它确实提示我使用标准的Microsoft登录页面登录Azure,但它显然没有保留凭据。
这是我尝试过的代码:

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}

我不希望得到错误连接到azure广告模块时,我已经作出了连接。

2guxujil

2guxujil1#

我尝试在我的环境中重现相同的结果,结果成功如下:

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}

输出文件成功导出,如下所示:

如果连接到Azure AD帐户不成功,通常会出现错误“您必须先调用Connect-AzureAD cmdlet,然后再调用任何其他cmdlet”。
使用以下命令尝试**Connect-AzureAD**:

$credentials = Get-Credential
Connect-AzureAD -Credential $credentials

否则,请尝试按以下方式连接Azure AD:

Connect-AzureAd -TenantId TenantID

如果问题仍然存在,请尝试重新安装AzureAD模块:

Uninstall-Module AzureAD
Install-Module AzureAD
Import-Module AzureAD

相关问题