微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Azure powershell Void Newtonsoft.Json.Serialization

如何解决Azure powershell Void Newtonsoft.Json.Serialization

我有一个正在运行的 azure powershell 应用程序,但今天该 powershell 应用程序停止工作,出现以下错误

The running command stopped because the preference variable "ErrorActionPreference" or common parameter is set to Stop: Method not found: 'Void Newtonsoft.Json.Serialization.JsonDictionaryContract.set_PropertyNameResolver(System.Func`2<System.String,System.String>)'.

我很困惑,因为我没有在应用程序代码中使用 Newtonsoft。这是我的应用程序代码

   Param
(

    [Parameter (Mandatory= $false)]
    [object] $WebhookData
)

if ($WebhookData){

$requestBody = (ConvertFrom-Json -InputObject $WebhookData.RequestBody) 
$priority = $requestBody.priority
$IPList = $requestBody.IPList
$rule = $requestBody.rule
$UserName = $requestBody.Name
$subnet = $requestBody.subnet

if (!$subnet) { $subnet = '32'}

$ErrorActionPreference = 'stop'

function Login() {
    $connectionName = "AzureRunAsConnection"
    try
    {
        $servicePrincipalConnection = Get-AutomationConnection -Name $connectionName         

        Write-Verbose "Logging in to Azure..." -Verbose

        Add-AzureRmAccount `
            -ServicePrincipal `
            -TenantId $servicePrincipalConnection.TenantId `
            -ApplicationId $servicePrincipalConnection.ApplicationId `
            -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint | Out-Null
    }
    catch {
        if (!$servicePrincipalConnection)
        {
            $ErrorMessage = "Connection $connectionName not found."
            throw $ErrorMessage
        } else{
            Write-Error -Message $_.Exception
            throw $_.Exception
        }
    }
}



Login



$RGName = 'resourcegroup'
function Add-AzureIpRestrictionRule
{
$ApiVersions = Get-AzureRmResourceProvider -ProviderNamespace Microsoft.Web | 
Select-Object -ExpandProperty ResourceTypes |
Where-Object ResourceTypeName -eq 'sites' |
Select-Object -ExpandProperty ApiVersions

$LatestApiVersion = $ApiVersions[0]

$WebAppConfig = Get-AzureRmResource -ResourceType 'Microsoft.Web/sites/config' -ResourceName $WebAppName -ResourceGroupName $RGName -ApiVersion $LatestApiVersion

$WebAppConfig.Properties.ipSecurityRestrictions = $WebAppConfig.Properties.ipSecurityRestrictions + @($rule) | 
group-object name | 
ForEach-Object { $_.Group | Select-Object -Last 1 }

Set-AzureRmResource -ResourceId $WebAppConfig.ResourceId -Properties $WebAppConfig.Properties -ApiVersion $LatestApiVersion -Force 
}


$webAppNames = 'webapp-test'
$webAppList = $webAppNames.split(',')
Foreach($webAppName in $webAppList) { 
$IPList= @($IPList-split ",")
Write-Host "IPList found "$IPList"."
$increment = 1
foreach ($element in $IPList)
{
if ($element -eq "" -OR $element -eq " ") {continue}
else
{
$element=$element.Trim()
$rule = [PSCustomObject]@{
ipAddress = "$($element)/$subnet"
action = "Allow"
priority = "$priority"
name = $UserName+ $increment}
$increment++
Add-AzureIpRestrictionRule -ResourceGroupName "$RGName" -AppServiceName "$WebAppName" -rule $rule
}
}
}
}

我正在使用 Azure Runbook 运行此 powershell 并通过发布请求发送我的数据

解决方法

您使用的命令属于已弃用的 AzureRM 模块,请尝试迁移到新的 Az 模块。

导航到门户中的自动化帐户 -> Modules gallery -> 导入 Az.AccountsAz.Resources 模块,然后使用下面的脚本。

   Param
(

    [Parameter (Mandatory= $false)]
    [object] $WebhookData
)

if ($WebhookData){

$requestBody = (ConvertFrom-Json -InputObject $WebhookData.RequestBody) 
$priority = $requestBody.priority
$IPList = $requestBody.IPList
$rule = $requestBody.rule
$UserName = $requestBody.Name
$subNet = $requestBody.Subnet

if (!$subNet) { $subNet = '32'}

$ErrorActionPreference = 'stop'

function Login() {
    $connectionName = "AzureRunAsConnection"
    try
    {
        $servicePrincipalConnection = Get-AutomationConnection -Name $connectionName         

        Write-Verbose "Logging in to Azure..." -Verbose

        Connect-AzAccount `
            -ServicePrincipal `
            -TenantId $servicePrincipalConnection.TenantId `
            -ApplicationId $servicePrincipalConnection.ApplicationId `
            -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint | Out-Null
    }
    catch {
        if (!$servicePrincipalConnection)
        {
            $ErrorMessage = "Connection $connectionName not found."
            throw $ErrorMessage
        } else{
            Write-Error -Message $_.Exception
            throw $_.Exception
        }
    }
}



Login



$RGName = 'resourcegroup'
function Add-AzureIpRestrictionRule
{
$ApiVersions = Get-AzResourceProvider -ProviderNamespace Microsoft.Web | 
Select-Object -ExpandProperty ResourceTypes |
Where-Object ResourceTypeName -eq 'sites' |
Select-Object -ExpandProperty ApiVersions

$LatestApiVersion = $ApiVersions[0]

$WebAppConfig = Get-AzResource -ResourceType 'Microsoft.Web/sites/config' -ResourceName $WebAppName -ResourceGroupName $RGName -ApiVersion $LatestApiVersion

$WebAppConfig.Properties.ipSecurityRestrictions = $WebAppConfig.Properties.ipSecurityRestrictions + @($rule) | 
Group-Object name | 
ForEach-Object { $_.Group | Select-Object -Last 1 }

Set-AzResource -ResourceId $WebAppConfig.ResourceId -Properties $WebAppConfig.Properties -ApiVersion $LatestApiVersion -Force 
}


$webAppNames = 'webapp-test'
$webAppList = $webAppNames.split(',')
Foreach($webAppName in $webAppList) { 
$IPList= @($IPList-split ",")
Write-Host "IPList found "$IPList"."
$increment = 1
foreach ($element in $IPList)
{
if ($element -eq "" -OR $element -eq " ") {continue}
else
{
$element=$element.Trim()
$rule = [PSCustomObject]@{
ipAddress = "$($element)/$subNet"
action = "Allow"
priority = "$priority"
name = $UserName+ $increment}
$increment++
Add-AzureIpRestrictionRule -ResourceGroupName "$RGName" -AppServiceName "$WebAppName" -rule $rule
}
}
}
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。