Azure 基础:用 PowerShell 自动登录

简介:

PowerShell 是管理 Azure 的最好方式,因为我们可以使用脚本把很多的工作自动化。比如把 Azure 上的虚拟机关机,并在适当的时间把它开机,这样我们就能节省一些开支,当然我们同时也为减少二氧化碳的排放做出了贡献!

PowerShell 的 Azure 模块中为我们提供了不同的 API, 早期的叫 ASM(Azure Service Manager)。随着 Azure 的发展变化,又出现了一套新的 API 叫 ARM(Azure Resource Management)。我们这里仅介绍如何使用 ARM 中的 API 实现自动登录并且操作 Azure 上的资源。

使用 PowerShell 自动登录 Azure 的大体思路是这样的:首先使用登录命令在交互式界面下进行登录操作,然后使用 Save-AzureRmProfile 命令把你的登录认证信息保存到本地的文件中。以后在脚本中进行自动登录时,只要使用这个本地文件就可以了。下面让我们来看看具体的操作过程。

使用 Login-AzureRmAccount 命令登录

在登录前先检查一下当前的登录状态,我们通过查询 Resource Group 来间接的进行。
执行命令:Get-AzureRmResourceGroup

因为没有登录,查询失败并提示我们进行登录。

执行命令:Login-AzureRmAccount
通过弹出的对话框登录:

登录成功后会显示你的账户信息:

好了现在让我们再来执行一次 Get-AzureRmResourceGroup 命令。

之前的错误信息已经没有了,输出的结果为 Resource Group 的列表。

把登录信息保存到文件中

Save-AzureRmProfile 命令能够把当前 session 的登录信息保存到文件中,在其他的 session 中就可以使用这个文件进行自动登录。
执行命令:Save-AzureRmProfile -Path “d:\test\myprofile.json”
myprofile.json 是一个普通的文本文件,只有认证信息被加密了,其它的信息都是可读的。

注意,一定要保护好生成的 myprofile.json 文件,如果泄露出去和别人拿到你的账户密码是一样的。

自动登录 Azure

Select-AzureRmProfile 命令从文件中载入用户的登录信息并且设置 Azure 的执行上下文。
Select-AzureRmProfile –Path “d:\test\myprofile.json”
执行结果和我们运行 Login-AzureRmAccount 命令是一样的:

一个自动重启虚拟机的例子

我们通过重启 Azure 上的一台虚机来完成一个完整的例子:

复制代码
$profile = "your profile path"
$resourceGroupName = "your resource group name"
$vmName = "your vm name"
$logfile = "log file name"
# 自定义日志方法
Function LogWrite
{
    Param ([string]$logstring)
    $now = Get-Date
    $logcontent = $now.ToShortDateString() + " " + $now.ToShortTimeString() + ": " + $logstring    
    Add-Content $logfile -value $logcontent
}

LogWrite("before Select-AzureRmProfile.")
Select-AzureRmProfile -Path $profile
LogWrite("after Select-AzureRmProfile.")

LogWrite("before Restart-AzureRmVM.")
Restart-AzureRmVM -ResourceGroupName $resourceGroupName -Name $vmName
LogWrite("after Restart-AzureRmVM.")
复制代码

好了,一个简单的自动化工作就完成了!


本文转自sparkdev博客园博客,原文链接:http://www.cnblogs.com/sparkdev/p/6358266.html,如需转载请自行联系原作者

相关文章
|
2月前
【Azure 应用服务】Azure Powershell Function 出错 The term 'Connect-AzAccount' is not recognized
【Azure 应用服务】Azure Powershell Function 出错 The term 'Connect-AzAccount' is not recognized
|
2月前
【Azure Durable Function】PowerShell Activity 函数遇见 Newtonsoft.Json.JsonReaderException: The reader's MaxDepth of 64 has been exceeded.
【Azure Durable Function】PowerShell Activity 函数遇见 Newtonsoft.Json.JsonReaderException: The reader's MaxDepth of 64 has been exceeded.
|
2月前
【Azure Web Job】Azure Web Job执行Powershell脚本报错 The term 'Select-AzContext' is not recognized as the name
【Azure Web Job】Azure Web Job执行Powershell脚本报错 The term 'Select-AzContext' is not recognized as the name
|
2月前
|
存储 C# Python
【Azure Storage Account】Azure 存储服务计算Blob的数量和大小的PowerShell代码
【Azure Storage Account】Azure 存储服务计算Blob的数量和大小的PowerShell代码
|
2月前
|
Ubuntu Linux 测试技术
【Azure Function App】Python Function调用Powershell脚本在Azure上执行失败的案例
【Azure Function App】Python Function调用Powershell脚本在Azure上执行失败的案例
|
2月前
|
数据安全/隐私保护 异构计算 Windows
【Azure 环境】 介绍两种常规的方法来监视Window系统的CPU高时的进程信息: Performance Monitor 和 Powershell Get-Counter
【Azure 环境】 介绍两种常规的方法来监视Window系统的CPU高时的进程信息: Performance Monitor 和 Powershell Get-Counter
|
2月前
|
存储 Shell 容器
【Azure 存储服务】使用PowerShell脚本创建存储账号(Storage Account)的共享访问签名(SASToken) : New-AzStorageContainerSASToken
【Azure 存储服务】使用PowerShell脚本创建存储账号(Storage Account)的共享访问签名(SASToken) : New-AzStorageContainerSASToken
|
2月前
|
Java 开发工具 数据安全/隐私保护
【Azure Developer】使用 Powershell az account get-access-token 命令获取Access Token (使用用户名+密码)
【Azure Developer】使用 Powershell az account get-access-token 命令获取Access Token (使用用户名+密码)
|
2月前
【Azure 应用服务】Azure Function 启用 Managed Identity后, Powershell Funciton出现 ERROR: ManagedIdentityCredential authentication failed
【Azure 应用服务】Azure Function 启用 Managed Identity后, Powershell Funciton出现 ERROR: ManagedIdentityCredential authentication failed
|
2月前
【Azure Developer】使用PowerShell Where-Object方法过滤多维ArrayList时候,遇见的诡异问题 -- 当查找结果只有一个对象时,返回结果修改了对象结构,把多维变为一维
【Azure Developer】使用PowerShell Where-Object方法过滤多维ArrayList时候,遇见的诡异问题 -- 当查找结果只有一个对象时,返回结果修改了对象结构,把多维变为一维
下一篇
无影云桌面