gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 06:42:42 25 4
gpt4 key购买 nike

我正在 Powershell 中开发我的第一个 GUI。我过去曾在 Azure 模块中使用过小脚本,所以我更困惑为什么会出现这种情况。在下面的脚本中,有两个地方我尝试导入并使用 connect-azureread 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 广告模块时出现的错误。

最佳答案

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

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}

enter image description here

输出文件成功导出如下:

enter image description here

如果连接到 Azure AD 帐户,通常会出现错误“在调用任何其他 cmdlet 之前必须先调用 Connect-AzureAD cmdlet”没有成功。

使用以下命令尝试Connect-AzureAD:

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

enter image description here

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

Connect-AzureAd -TenantId TenantID

enter image description here

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

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

关于azure - 为什么我在连接到服务后收到 Connect-AzureAD 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74394753/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com