PowerShell登錄Azure方法

我們都知道Azure中國的用戶名超長,每次運行腳本登錄時都特別心塞,那有沒有更方便和安全的驗證方式呢?

本篇文章我們來探索一下使用PowerShell登錄Azure的方式

一、

當然是直接使用原始命令與Azure登錄界面交互來登錄了

#Azure驗證界面登錄
Login-AzureRmAccount -EnvironmentName AzureChinaCloud



二、

使用Windows憑據輸入界面登錄

這種方式可以預定義用戶名,只需要在彈出窗口輸入密碼就可以登錄了

#Windows憑據驗證界面登錄
Login-AzureRmAccount -EnvironmentName AzureChinaCloud -Credential ` 
$(Get-Credential -UserName [email protected] -Message Login_AzureChinaCloud)



三、

不需要界面交互,我們可以預先將密碼加密保存在文件中,直接從文件載入,既可以達到免輸入登錄,又能保證密碼安全。

#從加密文件載入密碼登錄
$pwd = Get-Content $home\secretfile.txt | ConvertTo-SecureString
$cred = New-Object System.Management.Automation.PSCredential("[email protected]",$pwd)
Login-AzureRmAccount -EnvironmentName AzureChinaCloud -Credential $cred

加密密碼保存到文件

#加密
#定義加密文件路徑
$secretfile = "$home\secretfile.txt"
#定義加密內容(密碼)
$pwd = "az.trig.c0m"
#加密並將內容放入文件中
ConvertTo-SecureString -String $pwd -AsPlainText -Force | ConvertFrom-SecureString | Out-File -FilePath  $secretfile -Encoding unicode

同樣可以從加密文件中獲得解密的明文密碼,但解密過程只能在原加密計算機操作才能成功。

#解密
#從加密文件載入密碼
$pwd = Get-Content $home\secretfile.txt | ConvertTo-SecureString
#轉換成明文
[Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($pwd))



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章