每次我打开一个 azure powershell windows 时都可以自动登录吗?
即下面给出的这个命令应该在我每次打开 Azure 命令窗口时运行。
Get-AzurePublishSettingsFile(本地计算机中的路径)
甚至是 Add-AzureAccount
每次我打开一个 azure powershell windows 时都可以自动登录吗?
即下面给出的这个命令应该在我每次打开 Azure 命令窗口时运行。
Get-AzurePublishSettingsFile(本地计算机中的路径)
甚至是 Add-AzureAccount
你实际上可以做到。遵循这个过程:
对于 64 位操作系统,打开位于“C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\ServiceManagement\Azure\Services\ShortcutStartup.ps1”的 Azure PowerShell 初始化文件或“C:\Program Files\Microsoft SDKs\ Azure\PowerShell\ServiceManagement\Azure\Services\ShortcutStartup.ps1" 适用于 32 位操作系统。
在语句之后添加以下代码$VerbosePreference="Continue"
$username = "<username>"
$password = "<password>"
$secpasswd = ConvertTo-SecureString $password -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ($userName, $secpasswd)
Add-AzureAccount -Credential $cred
那应该为您自动登录
@Update:对于 Azure ARM 模式,命令为Login-AzureRMAccount -Credential $cred
. 不过,这可能不适用于 Microsoft 帐户。
使用 Import-AzurePublishSettingsFile 后,来自那里的数据将保存在用户配置文件中的订阅数据文件中(默认情况下位于:%userprofile%\AppData\Roaming\Windows Azure Powershell\WindowsAzureProfile.xml)。它还从设置文件中获取带有私钥的证书并将其保存到机器的证书存储中,以便订阅数据文件仅包含证书的指纹。然后,即使跨会话使用该订阅数据文件,您也不必继续导入. 如果需要,使用 -SubscriptionDataFile 参数实际上可以将文件写入不同的位置。
请注意,这也意味着任何可以访问您的计算机并打开 PowerShell 窗口的人都可以使用您的 cmdlet 来影响以这种方式导入的任何/所有订阅。
这里要注意的一件事是,如果您保留发布设置文件,那么您有一个文件,其中包含管理证书的私钥的完整副本。如果在您没有意识到的情况下从您那里取走该文件,那么某人将拥有他们可能对您的订阅造成不可挽回的伤害所需的一切。我建议在将证书加载到机器的证书存储区后保留 SubscriptionDataFile 的副本。然后,您可以使用 Set-AzureSubscription 并指向订阅数据文件以在订阅因某种原因被删除时拉入。
如果您使用 Add-AzureAccount,一旦您登录,它也会在同一个文件中存储几乎相同的数据;但是,它会跟踪来自 Azure AD 的令牌。这意味着最终令牌将过期,无论如何您都必须再次登录。目前使用 Add-AzureAccount 仅适用于交互式脚本,因为在 PowerShell 中没有存储或传递我知道的凭据的好方法。
因此,如果您使用导入发布设置文件,则不必继续导入,除非您已完成 remove-azuresubscription 或该订阅数据文件已被删除。如果您使用的是 PowerShell 3,则应自动为您加载 Azure 模块,除非您已关闭自动加载。
我相信您要求的是从 Powershell 向 Azure 进行身份验证,而无需提供您的用户名和密码 - 这是您的服务管理员凭据。
一种方法是创建所谓的服务主体。这个概念是向 Azure 进行身份验证,而不是作为服务管理员 - 这需要您输入凭据,而是作为具有相同权限的服务帐户。
这是您如何做到这一点的方法。
将这些行添加到您的 Powershell 配置文件(C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1)
Import-Module azure;
Get-AzurePublishSettingsFile YourPath
Add-AzureAccount YourAccount
等等
您可以通过设置 PowerShell 配置文件自动登录到 Azure。
您可以在配置文件中包含您想要的任何命令,当您启动 PowerShell 时,所有这些命令都会自动运行并为您准备就绪。
设置 PowerShell 配置文件并导入 Azure 模块:
要导入 Azure 模块,请从此处安装 Powershell SDK - https://www.microsoft.com/web/handlers/webpi.ashx/getinstaller/WindowsAzurePowershellGet.3f.3f.3fnew.appids
在管理员模式下打开 PowerShell 并在 cmd 下运行以检查您是否已经设置了配置文件
测试路径 $profile
i. If the O/P is False, you don’t have a profile. Go to step 3
ii. If the O/P is True, you already have a profile setup. Go to step 4
在同一个 PowerShell 窗口中,按照这些 cmds
新建项目 –type 文件 –force $profile
记事本 $profile
i. In the notepad, type
>> Import-Module "C:\Program Files (x86)\WindowsPowerShell\Modules\AzureRM\5.6.0\AzureRM.psm1" (Note: this path might change in future release, verify before you put in)
>> Login-AzureRmAccount -verbose
ii. Save and close the notepad
在同一个 PowerShell 窗口中,按照这些 cmds
记事本 $profile
i. In the notepad, type
>> Import-Module "C:\Program Files (x86)\Microsoft\SDKs\Azure\PowerShell\ServiceManagement\Azure\Azure.psd1"
>> Login-AzureRmAccount -verbose
ii. Save and close the notepad
随意在 Login-AzureRmAccount 之后的配置文件中添加您想要的任何命令
希望这有帮助!
Note that, startign with release 4.4.0, you can do this:
Enable-AzureRmContextAutosave
and your credentials will automatically be persisted across sessions.