【Azure 应用服务】Azure Function 中运行Powershell 脚本,定位 -DefaultProfile 引发的错误
问题描述
突然之间,使用PowerShell脚本 Get-AzVirtualNetwork 获取虚拟网络信息时,如果带上 -DefaultProfile $sub 参数,就出现 Azure credentials 错误。
代码:
Connect-AzAccount -ServicePrincipal -Credential $pscredential -Tenant $cloudroomTenantId -Environment $region $sub = Set-AzContext -SubscriptionId $cloudroomId -Tenant $cloudroomTenantId Get-AzVirtualNetwork -Name $VNetName -ResourceGroupName $ResourceGroupName -DefaultProfile $sub -ErrorAction "SilentlyContinue"
错误消息:
ERROR: Your Azure credentials have not been set up or have expired, please run Connect-AzAccount to set up your Azure credentials.No certificate thumbprint or secret provided for the given service principal
这是为什么呢?
问题分析
根据错误消息,是Connect-AzAccount中获取的Credentials信息不对导致的。但是当 Get-AzVirtualNetwork 不使用 -DefaultProfile参数时,命令确认成功执行。那么可以定位到问题是 -DefaultProfile $sub 引发。
在代码不变动的情况下,是什么原因导致DefaultProfile信息不对呢? 所以就深入查看了Azure Function中所依赖的模块('Az.Accounts')的版本情况,发现当使用 version '2.7.0'时,就会遇见这样的错误,在本地也能复现问题。当修改为version '2.6.2' 后,脚本能正常运行。
如何修改Azure Function中 Powershell脚本的依赖版本呢?
第一步:进入高级管理工具(Kudu:)
第二步:点击DebugConsole,并进入到wwwroot文件夹
第三步:找到requirements.psd1,首先点击edit图标修改requirements.psd1,安装Az.Accountsde 2.6.2依赖
# This file enables modules to be automatically managed by the Functions service.
# See https://aka.ms/functionsmanageddependency for additional information.
#
@{
# For latest supported version, go to 'https://www.powershellgallery.com/packages/Az'.
# To use the Az module in your function app, please uncomment the line below.
'Az' = '6.5.0'
'Az.Accounts' = '2.6.2'
}
第四步:然后找到 profile.ps1文件,修改profile.ps1文件,在第一行添加: Import-Module Az.Accounts -RequiredVersion '2.6.2'
第五步:修改完成后,回到Function App界面点击重启按钮,重启会重新安装这些依赖(重装依赖包耗时在20分钟左右)。
附录一:Function Script
using namespace System.Net # Input Parameter
param($Request, $TriggerMetadata)
$username = $env:ClientID
$password = $env:Password # Login to Azure
$password = ConvertTo-SecureString -String $password -AsPlainText -Force
$pscredential = New-Object System.Management.Automation.PSCredential($username, $password)
# Write to the Azure Functions log stream.
Write-Host "PowerShell HTTP trigger function processed a request."
$VNetName = "XXXXXXXXXXXX"
$ResourceGroupName = "XXXXXXXXXXXX"
$cloudroomTenantId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
$cloudroomId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" Connect-AzAccount -ServicePrincipal -Credential $pscredential -Tenant $cloudroomTenantId -Environment "China East"
$sub = Set-AzContext -SubscriptionId $cloudroomId -Tenant $cloudroomTenantId # SELECT-AzSubscription -SubscriptionId $cloudroomId
$vnetResult = Get-AzVirtualNetwork -Name $VNetName -ResourceGroupName $ResourceGroupName -DefaultProfile $sub -ErrorAction "SilentlyContinue"
if ($vnetResult -and $vnetResult -ne "") {
Write-Host "OK"
} else {
Write-Host "Fail"
}
参考资料
Function Dependency management :https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-powershell?tabs=portal#dependency-management
【Azure 应用服务】Azure Function 中运行Powershell 脚本,定位 -DefaultProfile 引发的错误的更多相关文章
- monkeyrunner之eclipse中运行monkeyrunner脚本之环境搭建(四)
monkeyrunner脚本使用Python语法编写,但它实际上是通过Jython来解释执行. Jython是Python的Java实现,它将Python代码解释成Java虚拟机上的字节码并执行,这种 ...
- Linux中运行.sh脚本,异常/bin/sh^M: bad interpreter: No such file or directory。
在Linux中运行.sh脚本,异常/bin/sh^M: bad interpreter: No such file or directory. 分析:这是不同系统编码格式引起的:在windows系统中 ...
- Visual Studio下运行PowerShell脚本自增小版本号并发布到Nuget服务器上
Visual Studio下运行PowerShell脚本自动更新项目里AssemblyInfo.cs文件的版本(自增小版本号)并发布到Nuget服务器上 附脚本[ update.ps1文件内容]: $ ...
- 运行PowerShell脚本
[运行PowerShell脚本] powershell脚本以ps1为扩展名.最的一个是数字1,不是字母l. 当右键ps1文件时,会有用powershell运行的选项,选中这个选项即可运行. 团体pow ...
- 转载:monkeyrunner之eclipse中运行monkeyrunner脚本之环境搭建(四)
转载自:lynnLi 的monkeyrunner之eclipse中运行monkeyrunner脚本之环境搭建(四) monkeyrunner脚本使用Python语法编写,但它实际上是通过Jython来 ...
- 在C#中运行PowerShell
C#中运行PowerShell需要用到System.Management.Automation.dll.在Visual Studio中可以通过NuGet添加引用,package名字为"Sys ...
- 在 Snoop 中使用 PowerShell 脚本进行更高级的 UI 调试
原文:在 Snoop 中使用 PowerShell 脚本进行更高级的 UI 调试 版权声明:本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可.欢迎转载.使用.重新发布, ...
- 开发中运行mysql脚本,发现提示mysql提示Column count doesn't match value count at row 1错误
开发中运行mysql脚本,发现提示mysql提示Column count doesn't match value count at row 1错误, 调试后发现是由于写的SQL语句里列的数目和后面的值 ...
- 『动善时』JMeter基础 — 57、Linux系统中运行JMeter脚本
目录 1.Linux系统中安装Java环境 (1)解压Java安装包 (2)配置Java环境变量 (3)验证Java环境是否配置成功 2.Linux系统中安装JMeter (1)下载JMeter (2 ...
- 【Azure Developer】调用SDK的runPowerShellScript方法,在Azure VM中执行PowerShell脚本示例
当需要通过代码的方式执行PowerShell脚本时,可以参考以下的示例. Azure SDK中提供了两个方法来执行PowerShell脚本 (SDK Source Code: https://gith ...
随机推荐
- node使用nodemailer发送邮件
安装模块 npm install nodemailer 代码 const nodemailer = require('nodemailer'); // 查找到有关QQ邮箱的相关信息在 /node_mo ...
- 如何安装typescript-如何运行typescript
如何安装 typescript npm install typescript -g 安装 ts 之前需要安装 node 哈 查看 ts 的版本 tsc -V 我的是 3.6.3 的版本 typescr ...
- vue动画 <transition-group> 使用半场动画
<style> /* 给动画添加一组过度效果 */ .v-enter, .v-leave-to { opacity: 0.8; /* 从右向左进入 */ transform: transl ...
- 限制input框中字数的输入maxlength
今天产品提出一个需求就是.限制input框中的的值. 当用户超过10个字符时,用户再次输入的时,就不能够输入了. (最后就能够输入10个字符) maxlength=10 <input maxle ...
- layui之静态表格的分页及搜索功能以及前端使用XLSX导出Excel功能
LayUI官方文档:https://layui.dev/docs/2/#introduce XLSX NPM地址:https://www.npmjs.com/package/xlsx XLSX 使用参 ...
- Matplotlib配置图例legend()设置透明和并排显示
1.多排显示 x=np.linspace(start=-np.pi,stop=np.pi,num=300) plt.style.use('classic') Fig,Axes=plt.subplots ...
- 通过URL载入ShellCode代码
将生成的shellcode放到web服务器上,本地不保存恶意代码,本地只负责加载到内存运行,这样可以很好的躲过查杀. 生成shellcode msfvenom -a x86 --platform Wi ...
- LyScriptTools 反汇编类API接口手册
LyScriptTools模块实在LyScript模块反汇编基础上封装而成,其提供了更多的反汇编方法,可以更好的控制x64dbg完成自动化反汇编任务,API参考手册如下. 插件地址:https://g ...
- CF813E Army Creation 题解
题目链接:CF 或者 洛谷 并不是很难的题,关于颜色数量类问题,那么很显然,沿用经典的 "HH的项链" 思想去思考问题.由于涉及到了 \(k\) 个数的限制,我们观察到如果一个数在 ...
- .NET中使用BootstrapBlazor组件库Table实操篇
前言 Table表格在后台管理应用中使用的是相当频繁的,因此找一个功能齐全的前端框架对于我们而言是非常必要的,因为封装完善的前端框架能够大大提升我们的工作对接效率.今天我们主要来讲解一下在.NET中使 ...