问题描述

突然之间,使用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 引发的错误的更多相关文章

  1. monkeyrunner之eclipse中运行monkeyrunner脚本之环境搭建(四)

    monkeyrunner脚本使用Python语法编写,但它实际上是通过Jython来解释执行. Jython是Python的Java实现,它将Python代码解释成Java虚拟机上的字节码并执行,这种 ...

  2. 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系统中 ...

  3. Visual Studio下运行PowerShell脚本自增小版本号并发布到Nuget服务器上

    Visual Studio下运行PowerShell脚本自动更新项目里AssemblyInfo.cs文件的版本(自增小版本号)并发布到Nuget服务器上 附脚本[ update.ps1文件内容]: $ ...

  4. 运行PowerShell脚本

    [运行PowerShell脚本] powershell脚本以ps1为扩展名.最的一个是数字1,不是字母l. 当右键ps1文件时,会有用powershell运行的选项,选中这个选项即可运行. 团体pow ...

  5. 转载:monkeyrunner之eclipse中运行monkeyrunner脚本之环境搭建(四)

    转载自:lynnLi 的monkeyrunner之eclipse中运行monkeyrunner脚本之环境搭建(四) monkeyrunner脚本使用Python语法编写,但它实际上是通过Jython来 ...

  6. 在C#中运行PowerShell

    C#中运行PowerShell需要用到System.Management.Automation.dll.在Visual Studio中可以通过NuGet添加引用,package名字为"Sys ...

  7. 在 Snoop 中使用 PowerShell 脚本进行更高级的 UI 调试

    原文:在 Snoop 中使用 PowerShell 脚本进行更高级的 UI 调试 版权声明:本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可.欢迎转载.使用.重新发布, ...

  8. 开发中运行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语句里列的数目和后面的值 ...

  9. 『动善时』JMeter基础 — 57、Linux系统中运行JMeter脚本

    目录 1.Linux系统中安装Java环境 (1)解压Java安装包 (2)配置Java环境变量 (3)验证Java环境是否配置成功 2.Linux系统中安装JMeter (1)下载JMeter (2 ...

  10. 【Azure Developer】调用SDK的runPowerShellScript方法,在Azure VM中执行PowerShell脚本示例

    当需要通过代码的方式执行PowerShell脚本时,可以参考以下的示例. Azure SDK中提供了两个方法来执行PowerShell脚本 (SDK Source Code: https://gith ...

随机推荐

  1. Codeforces round 919 (div2)

    Problem - A - Codeforces 暴力枚举 就可以: #include <bits/stdc++.h> #define int long long using namesp ...

  2. MySQL控制权限

    编写顺序和执行顺序是不一样的 编写顺序: SELECT 字段列表 FROM 表名列表 WHERE 条件列表 GROUP BY 分组字段列表 HAVING 分组后条件列表 ORDER BY 排序字段列表 ...

  3. STM32CubeMX教程26 FatFs 文件系统 - W25Q128读写

    1.准备材料 正点原子stm32f407探索者开发板V2.4 STM32CubeMX软件(Version 6.10.0) keil µVision5 IDE(MDK-Arm) ST-LINK/V2驱动 ...

  4. HTML超链接自定义颜色

    先添加一个css样式 我这里直接内嵌了因为代码量比较少不会影响太大的性能 <style type="text/css"> body {background-color: ...

  5. 使用 docker 部署 kafka

    在很多时候我们需要使用到消息队列, 其中 kafka 是一个非常优秀的消息队列, 在我们平时开发中也经常会用到, 但是在开发环境中部署 kafka 是一个非常麻烦的事情 在 kafka 官网上, 有一 ...

  6. Netty-核心模块组件-4

    Netty 核心模块组件 一.Bootstrap.ServerBootstrap 1.Bootstrap 意思是引导,一个 Netty 应用通常由一个 Bootstrap 开始,主要作用是配置整个 N ...

  7. 【Linux】root密码忘记了怎么办【技能篇】一分钟教会你重置root密码

    [Linux]root密码重置 文章目录 前言介绍 操作步骤 尾声 前言介绍 那么这里博主先安利一下一些干货满满的专栏啦! 数据结构专栏:数据结构 这里包含了博主很多的数据结构学习上的总结,每一篇都是 ...

  8. 关于一些OJ上的\r以及\n以及字符串行输入的一些警示

    \r,\n,\r\n的区别 - 小 天 - 博客园 (cnblogs.com) 这篇文章详细的解释了在Windows系统和Linux系统下的换行的区别 概括的说,就是Windows系统下的" ...

  9. 小知识:Oracle中的层次查询

    使用Oracle中的start with .. connect by prior ..语句可以轻松实现. 下面通过scott用户下的emp来做演示,使用自己的一个19c测试环境,结果发现默认并没有sc ...

  10. IIS的基本安装和配置

    实验介绍:IIS的作用 IIS是web服务器中常见的一种.当客户端想访问某个域名时,向web服务器发出请求.web服务器返回网页的代码做出回应.客户端解析代码生成网页. 一:安装IIS 1.打开一台w ...