Azure實踐之automation自動整理資產信息

今天來討論下Azure automation的其他應用方式,對於雲管理人員來說,肯定是非常希望對雲中的資產情況有一個全面的瞭解的,對於這種需求,Azure Portal其實不失爲一個不錯的選擇,然而,很多情況下,Portal瞭解的信息往往沒有那麼全面,而且效率非常低

 

這時候,使用PowerShell進行各種信息的統計就顯得更爲高效了,可以使用各種腳本統計包括IaaS到PaaS服務的各種信息,如果想要定期的去收集這些信息,還可以結合Azure automation,定義好schedule,然後配合收集信息的腳本,即可實現這樣的需求

 

下邊來看具體如何實現,首先在Azure中創建好一個automation account,這個就不演示了,之後更新好我們的module,並且導入好需要的module,常用的包括azurerm.storage, azurerm.network, azurerm.profile等,這部分內容可以看下之前的博客

http://blog.51cto.com/mxyit/2348123

 

有了這些內容之後,下一步要做的就是創建一個PowerShell的runbook了,收集信息的runbook有很多,包括網上的以及我自己寫的,各位可以根據需要使用,我自己寫的其實以前也介紹過很多次

 

使用PowerShell 獲取 Azure賬戶中的用戶權限分配情況

http://blog.51cto.com/mxyit/2347819

使用PowerShell快速獲取Azure中的SQL Server VM

http://blog.51cto.com/mxyit/2347699

如何使用PowerShell 收集Azure VM Image列表

http://blog.51cto.com/mxyit/2070613

 

這裏我用的是一個外國友人寫的腳本,因爲涵蓋的服務比較多一些

https://gallery.technet.microsoft.com/scriptcenter/Azure-Inventory-using-3db0f658?redir=0

 

這個腳本本身是會把信息存儲在硬盤上的,但是因爲在automation中,這部分信息沒辦法持久化,所以我們可以稍作修改,分兩個層面

1.      將收集好的信息存儲爲csv文件,保存在blob中

2.      將csv文件作爲附件發送到郵箱中

 

爲此,我們需要在原來的腳本基礎上做一些修改,添加一些內容,以收集虛擬機信息爲例,在虛擬機信息收集的腳本後添加以下內容

#Define file name
$filename="Virtual_Machine_details_"+ (Get-Date -UFormat '%Y_%m_%d')+".csv"
$virtual_machine_object | Export-Csv -path $filename -NoTypeInformation -Force
 
#Define blob info
$StorageAccountName = "<your storage account name>"
$StorageAccountKey = "<************************* your storage account key*************************>"
$ctx = New-AzureStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey
$containername="inventory"
Set-AzureStorageBlobContent -Container $ContainerName -File $filename -Blob $filename -Context $ctx -Force
 
#send email
$uname = "your email account"
$pwd = ConvertTo-SecureString -String "<your password>" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($uname, $pwd)
Send-MailMessage -from $uname -To '<target mail account>' -Subject "Azure Environment daily check $(Get-Date -UFormat '%Y/%m/%d')" -Body $("<font size=3><b>Here's the Azure environment status today</b></font><br>" ) -Attachments $filename -SmtpServer '<mail server>' -Credential $cred -UseSsl -BodyAsHtml



完成後,將runbook link to schedule

image001.png



創建一個schedule

image003.png



之後即可在schedule中看到了

image005.png


運行後可以看到類似以下的效果,當然,這裏只是演示了獲取VM的信息,未包含腳本里的其他內容

 

Blob中存儲內容


image007.png



郵件收到的內容

image009.png



這樣,每天早上來了之後就可以看到Azure的資產信息了!


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