如何在 Powershell 中复制 Azure 资源管理器模板 uniquestring() 函数?

2024-04-21

我有一个 Azure 托管服务,该服务使用 ARM 模板自动部署(在我们的构建过程中),并在不同的资源组中提供该服务的多个实例。有些资源需要全局唯一的名称,为了支持这一点,我使用uniquestring函数(通常根据资源组 ID)生成这些资源名称的一部分。名称遵循模板化模式,例如实例名称 + 组件名称 + 唯一字符串 =“Example.WebAPI.abcd1234efgh5”

后来当使用 Powershell 脚本对这些资源执行维护时,我的问题出现了 - 我需要资源名称,这意味着使用相同的模板从实例、组件和资源组 ID 生成名称。但是,Powershell中没有函数返回与ARM相同的值uniquestring相同输入的函数。

据我所知,uniquestring只是生成输入字符串的哈希值并将其作为 13 个字符的表示形式返回;有谁知道这个哈希过程的细节,以便可以在 Powershell 中复制它,或者它是否已经完成?

我看过答案我可以在 JSON 部署模板之外调用 ARM 模板函数吗? https://stackoverflow.com/questions/41064916/can-i-call-arm-template-functions-outside-the-json-deployment-template,但这需要在初始 ARM 模板中使用输出参数,并假设立即使用该值;而在我的用例中,我在初始部署后几周或几个月需要资源名称。


我来晚了一点,但下面的 Powershell 脚本应该可以完成这项工作。

这是从具有关键字“unchecked”的 C# 翻译而来,因此函数 uncheckedUInt32Multiply 和 uncheckedUInt32Addition 用于在将数字相乘时不会导致溢出。

我不能将此归功于我,因为我刚刚从 C# 过渡到 Powershell。

C#函数源码来自:https://www.nuget.org/packages/Azure.Deployments.Expression/ https://www.nuget.org/packages/Azure.Deployments.Expression/

Usage:

# Pass an ordinary string
.\ArmUniqueStringGenerator.ps1 -InputStringValue "test example"

# Get Resource group name and pass the id to unique string generator.
(Get-AzResourceGroup -Name <Resource Group Name>).ResourceId | .\ArmUniqueStringGenerator.ps1
    param(
        [Parameter(
            Mandatory=$true,
            ValueFromPipeline=$true)]
        [string]$InputStringValue
    )

    #region Functions
    function GenerateUniqueString {
        param(
            [Parameter(Mandatory=$true)]
            [string]$InputString,
            [uint]$seed = 0u
        )

        [uint[]] $dataArray = [System.Text.Encoding]::UTF8.GetBytes($InputString)

        [int] $num =  $dataArray.Length
        [uint] $num2 = $seed
        [uint] $num3 =  $seed

        $index = 0

        for ($index = 0; $index + 7 -lt $num; $index += 8) {
            [uint] $num4 = [uint]($dataArray[$index] -bor ($dataArray[$index + 1] -shl 8) -bor ($dataArray[$index + 2] -shl 16) -bor ($dataArray[$index + 3] -shl 24))
            [uint] $num5 = [uint] ($dataArray[$index + 4] -bor ($dataArray[$index + 5] -shl 8) -bor ($dataArray[$index + 6] -shl 16) -bor ($dataArray[$index + 7] -shl 24))
            $num4 = uncheckedUInt32Multiply $num4 597399067
            $num4 = scramble -value $num4 -count 15
            $num4 = uncheckedUInt32Multiply $num4 2869860233u
            $num2 = $num2 -bxor $num4
            $num2 = scramble -value $num2 -count 19
            $num2 = uncheckedUInt32Addition $num2 $num3
            $num2 = uncheckedUInt32Addition (uncheckedUInt32Multiply $num2 5) 1444728091
            $num5 = uncheckedUInt32Multiply $num5 2869860233u
            $num5 = scramble -value $num5 -count 17
            $num5 = uncheckedUInt32Multiply $num5 597399067
            $num3 = $num3 -bxor $num5
            $num3 = scramble -value $num3 -count 13
            $num3 = uncheckedUInt32Addition $num3 $num2
            $num3 = uncheckedUInt32Addition (uncheckedUInt32Multiply $num3 5) 197830471

        }

        $num6 = [int]($num - $index)
        if ($num6 -gt 0) {

            $elseVal = switch($num6)
            {
                2 { 
                    [uint]($dataArray[$index] -bor ($dataArray[$index + 1] -shl 8)) 
                } 
                3 { 
                    [uint]($dataArray[$index] -bor ($dataArray[$index + 1] -shl 8) -bor ($dataArray[$index + 2] -shl 16)) 
                }
                default { 
                    $dataArray[$index] 
                }
            }
            
            $num7 = [uint](($num6 -ge 4) ? ([uint]($dataArray[$index] -bor ($dataArray[$index + 1] -shl 8) -bor ($dataArray[$index + 2] -shl 16) -bor ($dataArray[$index + 3] -shl 24))) : $elseVal)
            $num7 = uncheckedUInt32Multiply $num7 597399067
            $num7 = scramble -value $num7 -count 15
            $num7 = uncheckedUInt32Multiply $num7 2869860233u
            $num2 = $num2 -bxor $num7
            if ($num6 -gt 4)
            {
                $value = switch($num6)
                {
                    6 { 
                        uncheckedUInt32Multiply ($dataArray[$index + 4] -bor ($dataArray[$index + 5] -shl 8)) -1425107063
                    } 
                    7 { 
                        uncheckedUInt32Multiply ($dataArray[$index + 4] -bor ($dataArray[$index + 5] -shl 8) -bor ($dataArray[$index + 6] -shl 16)) -1425107063
                    } 
                    default { 
                        uncheckedUInt32Multiply ($dataArray[$index + 4]) -1425107063
                    } 
                }
                $value = scramble -value $value -count 17
                $value = uncheckedUInt32Multiply $value 597399067
                $num3  = $num3 -bxor $value
            }
        }


        $num2 = $num2 -bxor [uint]$num
        $num3 = $num3 -bxor [uint]$num
        $num2 = uncheckedUInt32Addition $num2 $num3
        $num3 = uncheckedUInt32Addition $num3 $num2
        $num2 = $num2 -bxor $num2 -shr 16
        $num2 = uncheckedUInt32Multiply $num2 2246822507u
        $num2 = $num2 -bxor $num2 -shr 13
        $num2 = uncheckedUInt32Multiply $num2 3266489909u
        $num2 = $num2 -bxor $num2 -shr 16
        $num3 = $num3 -bxor $num3 -shr 16
        $num3 = uncheckedUInt32Multiply $num3 2246822507u
        $num3 = $num3 -bxor $num3 -shr 13
        $num3 = uncheckedUInt32Multiply $num3 3266489909u
        $num3 = $num3 -bxor $num3 -shr 16
        $num2 = uncheckedUInt32Addition $num2 $num3
        $num3 = uncheckedUInt32Addition $num3 $num2

        $final = ([ulong]$num3 -shl 32) -bor $num2
        $uniqueString = Base32Encode $final
        return $uniqueString
    }


    $encodeLetters = "abcdefghijklmnopqrstuvwxyz234567"

    function scramble {
        param (
            [uint] $value,
            [int] $count
        )
        return ($value -shl $count) -bor ($value -shr 32 - $count)
    }


    function Base32Encode {
        param (
            [ulong] $longVal
        )
        $strOutput = ""
        for ($i = 0; $i -lt 13; $i++) {
            $charIdx = [int]($longVal -shr 59)
            $charAddition = $encodeLetters[$charIdx]
            $strOutput = $strOutput + $charAddition
            $longVal = $longVal -shl 5;

        }
        return $strOutput
    }

    function Base32Decode {
        param (
            [string] $encodedString
        )
        $bigInteger = [Numerics.BigInteger]::Zero
        for ($i = 0; $i -lt $encodedString.Length; $i++) {
            $char = $encodedString[$i]
            $ltrIdx = $encodeLetters.IndexOf($char)
            $bigInteger = ($bigInteger -shl 5) -bor $ltrIdx
        }

        return $bigInteger / 2
    }

    function uncheckedUInt32Multiply {
        param (
            [long] $nbrOne,
            [long] $nbrTwo
        )

        $nbrOnePos = $nbrOne -lt 0 ? [uint]::MaxValue - (-$nbrOne) + 1 : $nbrOne
        $nbrTwoPos = $nbrTwo -lt 0 ? [uint]::MaxValue - (-$nbrTwo) + 1 : $nbrTwo

        $uintMaxVal = [uint]::MaxValue

        $longMultiplyResult = ([ulong]$nbrOnePos * [ulong]$nbrTwoPos)

        $remainder = $longMultiplyResult % $uintMaxVal
        $totalDevisions = ($longMultiplyResult - $remainder) / $uintMaxVal

        $result = $remainder - $totalDevisions
        
        if ($result -lt 0) {
            return ($uintMaxVal - (-$result)) + 1
        }
        return $result
    }

    function uncheckedUInt32Addition {
        param (
            [uint] $nbrOne,
            [uint] $nbrTwo
        )

        $nbrOnePos = $nbrOne -lt 0 ? [uint]::MaxValue - (-$nbrOne) + 1 : $nbrOne
        $nbrTwoPos = $nbrTwo -lt 0 ? [uint]::MaxValue - (-$nbrTwo) + 1 : $nbrTwo

        $uintMaxVal = [uint]::MaxValue

        $longAdditionResult = ($nbrOnePos + $nbrTwoPos)
        $remainder = $longAdditionResult % $uintMaxVal
        $totalLoops = ($longAdditionResult - $remainder) / $uintMaxVal
        $result = [System.Math]::Abs($remainder - $totalLoops)

        return $result
    }
    #endregion

    $uniquestring = GenerateUniqueString -InputString $InputStringValue
    return $uniqueString
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Powershell 中复制 Azure 资源管理器模板 uniquestring() 函数? 的相关文章

随机推荐

  • 在 Python 中使用 Selenium 进行导航并使用 BeautifulSoup 进行抓取

    好的 这就是我想要实现的目标 调用带有动态过滤搜索结果列表的 URL 点击第一个搜索结果 5 页 抓取标题 段落和图像 并将它们作为 json 对象存储在单独的文件中 例如 Title 单个条目的标题元素 Content 各个条目的 DOM
  • 自定义键盘中断处理程序

    我正在尝试编写一个简单的程序 将标准键盘中断替换为自定义的键盘中断 以减少变量 但是 如果不调用旧处理程序 它就无法工作 这是我的中断处理程序 handler proc push ax push di dec EF pushf when t
  • res.send 和 res.render 调用

    我试图确定是否可以同时调用 res send data 和 res render reports 为了进一步详细解释 当我路由到 reports 时 首先在服务器端对返回 json 数据的 API 进行 REST 调用 现在我希望在客户端上
  • 我有 12000 个已知 URL,用 Python 抓取它们的最快方法是什么?

    因此 我有一个从数据库中提取的 URL 列表 我需要抓取并解析每个 URL 的 JSON 响应 某些 URL 返回 null 而其他 URL 返回发送到 csv 文件的信息 我目前正在使用Scrapy 但是抓取这12000个URL大约需要4
  • 将 Double.NaN 与其自身进行比较

    我一直试图找出为什么这两个操作返回不同的值 Double NaN Double NaN回报false Double NaN Equals Double NaN 回报true 我有answer https stackoverflow com
  • TypeScript 中的通用类型反射

    我可以确定泛型类型吗T在以下场景中 class MyClass constructor GenericMethod
  • 当项目数等于列数时,chrome 和 safari 渲染 css 列的方式不同

    我有一个目录列表 它使用 CSS 列 但在 Chrome 和 Safari 中的行为有所不同 目录的每个部分都有一个包装器 将列表排列成两列 我已经有了 CSS 所以 Chrome 会按照我想要的方式呈现它 在 Safari 中 第二列中的
  • 使用 $in 和 $nin 进行查询不使用索引

    当将属性与 in 和 nin 进行匹配时 Mongo 无法正确使用索引 如果仅使用 in 则索引会利用这一点 db assets find tags in blah explain cursor BtreeCursor tags 1 isM
  • Jasper 报告迭代数组列表[重复]

    这个问题在这里已经有答案了 如何创建将在 Jasper 报告中作为参数传递的详细信息部分中的数组列表进行迭代的报告 这可能吗 我搜索并找到了必须添加 ArrayList 作为数据源的解决方案 我怎么做 Regards 您可以将 ArrayL
  • 从 MySQL 中的列表字符串中获取单个项目

    给定以下代表可能列表的字符串 我如何获取指定索引处的项目n 1 2 3 4 5 word1 word2 word3 pipe delimited list 此功能的可能原因是 从 GROUP CONCAT 输出中提取特定元素 从 SET 列
  • 在 Spark 中读取 XML

    我正在尝试使用spark xml jar 读取pyspark 中的xml 嵌套xml df sqlContext read format com databricks spark xml option rowTag hierachy loa
  • Scala:为什么不能编译?

    Given class Foo T def get T class Bar class FooBar extends Foo Bar def get new Bar object Baz def something T U lt Foo T
  • symfony2:如何从模板访问服务

    如果我创建了一个服务 有没有办法从 twig 访问它 而不需要创建 twig extension 您可以将服务设置为一个树枝全局变量config yml e g app config config yml twig globals your
  • 自定义 UINavigationBar 适用于模拟器,但不适用于发布版本

    我希望能够深入了解过去几个小时以来我一直在努力解决的问题 我有一个正在配置的自定义 UINavigationBarapplication DidFinishLaunchingWithOptions通过调用以下方法 UINavigationB
  • 异步函数外部堆栈上下文

    有时代码想知道特定函数 或子函数 是否正在运行 例如 node js 有domains https nodejs org api domain html它也适用于异步内容 不确定这是否包括异步函数 一些简单的代码来解释我需要什么 如下所示
  • IOS stackview addArrangedSubview 在特定索引处添加

    如何在 UIStackView 的特定索引中添加排列的子视图 就像是 stackView addArrangedSubview nibView atIndex index 你的意思是你想插入 而不是添加 func insertArrange
  • 在 C# 中创建大型二进制文件的增量差异补丁

    我正在寻找一种创建大型二进制文件 VMWare 虚拟磁盘文件 的 Delta Diff 补丁的方法 是否有 C 中的实现或 NET Framework 中的任何有用的方法 任何帮助表示赞赏 谢谢 rAyt bsdiff http www d
  • 最佳 Java OpenID 库 [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 使用javascript读取和写入json文件[重复]

    这个问题在这里已经有答案了 可能的重复 如何使用 JavaScript 读取和写入文件 https stackoverflow com questions 585234 how to read and write into file usi
  • 如何在 Powershell 中复制 Azure 资源管理器模板 uniquestring() 函数?

    我有一个 Azure 托管服务 该服务使用 ARM 模板自动部署 在我们的构建过程中 并在不同的资源组中提供该服务的多个实例 有些资源需要全局唯一的名称 为了支持这一点 我使用uniquestring函数 通常根据资源组 ID 生成这些资源