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,如需转载请自行联系原作者

相关文章
|
4月前
|
数据安全/隐私保护
【Azure Function App】PowerShell Function 执行 Get-AzAccessToken 的返回值类型问题:System.String 与 System.Security.SecureString
将PowerShell Function部署到Azure Function App后,Get-AzAccessToken返回值类型在不同环境中有差异。正常为SecureString类型,但部分情况下为System.String类型,导致后续处理出错。解决方法是在profile.ps1中设置环境变量$env:AZUREPS_OUTPUT_PLAINTEXT_AZACCESSTOKEN=false,以禁用明文输出。
157 0
|
10月前
|
JSON 数据格式
【Azure Fabric Service】演示使用PowerShell命令部署SF应用程序(.NET)
本文详细介绍了在中国区微软云Azure上使用Service Fabrics服务时,通过PowerShell命令发布.NET应用的全过程。由于Visual Studio 2022无法直接发布应用,需借助PowerShell脚本完成部署。文章分三步讲解:首先在Visual Studio 2022中打包应用部署包,其次连接SF集群并上传部署包,最后注册应用类型、创建实例并启动服务。过程中涉及关键参数如服务器证书指纹和服务端证书指纹的获取,并附带图文说明,便于操作。参考官方文档,帮助用户成功部署并运行服务。
339 73
【Azure 应用服务】Azure Powershell Function 出错 The term 'Connect-AzAccount' is not recognized
【Azure 应用服务】Azure Powershell Function 出错 The term 'Connect-AzAccount' is not recognized
173 0
【Azure App Service】PowerShell脚本批量添加IP地址到Web App允许访问IP列表中
Web App取消公网访问后,只允许特定IP能访问Web App。需要写一下段PowerShell脚本,批量添加IP到Web App的允许访问IP列表里!
228 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.
150 3
【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
115 3
|
数据安全/隐私保护
【Azure Entra ID】使用PowerShell脚本导出Entra ID中指定应用下的所有用户信息
在Azure Entra ID中,需要导出一个Application 下的用户信息, 包含User的创建时间。
249 0
|
存储 C# Python
【Azure Storage Account】Azure 存储服务计算Blob的数量和大小的PowerShell代码
【Azure Storage Account】Azure 存储服务计算Blob的数量和大小的PowerShell代码
176 0
|
Ubuntu Linux 测试技术
【Azure Function App】Python Function调用Powershell脚本在Azure上执行失败的案例
【Azure Function App】Python Function调用Powershell脚本在Azure上执行失败的案例
138 0
|
数据安全/隐私保护 异构计算 Windows
【Azure 环境】 介绍两种常规的方法来监视Window系统的CPU高时的进程信息: Performance Monitor 和 Powershell Get-Counter
【Azure 环境】 介绍两种常规的方法来监视Window系统的CPU高时的进程信息: Performance Monitor 和 Powershell Get-Counter
243 0