powershell - 将特殊字符作为参数传递给Powershell脚本函数

标签 powershell azure

我刚刚在 powershell 脚本中创建了一个方法 -

function Add-Entity() {
    [CmdletBinding()]
    param(
       $TableName,
       $PartitionKey,
       $RowKey,
       [String]$JsonString
    )


  $entity = New-Object -TypeName Microsoft.WindowsAzure.Storage.Table.DynamicTableEntity -ArgumentList $PartitionKey, $RowKey
  $entity.Properties.Add("JsonStringProperty", $JsonString)
  $result = $table.CloudTable.Execute([Microsoft.WindowsAzure.Storage.Table.TableOperation]::Insert($entity))
}

我想将这个完整的 json 字符串作为参数传递 -

Add-Entity -TableName $myTableName -PartitionKey "ABC" -RowKey "XYZ" -JsonString {"TO":["<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="24454647645c5d5e0a474b49" rel="noreferrer noopener nofollow">[email protected]</a>","<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fa9e9f9cba828380d4999597" rel="noreferrer noopener nofollow">[email protected]</a>"],"CC":["<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ddadacaf9da5a4a7f3beb2b0" rel="noreferrer noopener nofollow">[email protected]</a>"],"BCC":[]}

每次我尝试编译时,都会出现错误 -

Unexpected token ':["abc@xyz".com"' in expression or statement

最佳答案

问题在于 JSON 使用双引号来分隔键和值,并且 Powershell 也使用相同的字符(双引号)来“换行”字符串。

您可以使用单引号来包装硬编码的输入 json,如下所示:

'{"TO":["<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8beae9e8cbf3f2f1a5e8e4e6" rel="noreferrer noopener nofollow">[email protected]</a>","<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3d59585b7d454447135e5250" rel="noreferrer noopener nofollow">[email protected]</a>"],"CC":["<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="92e2e3e0d2eaebe8bcf1fdff" rel="noreferrer noopener nofollow">[email protected]</a>"],"BCC":[]}' 

所以声明是:

Add-Entity -TableName $myTableName -PartitionKey "ABC" -RowKey "XYZ" -JsonString '{"TO":["<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="47262524073f3e3d6924282a" rel="noreferrer noopener nofollow">[email protected]</a>","<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9afefffcdae2e3e0b4f9f5f7" rel="noreferrer noopener nofollow">[email protected]</a>"],"CC":["<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="017170734179787b2f626e6c" rel="noreferrer noopener nofollow">[email protected]</a>"],"BCC":[]}'

关于powershell - 将特殊字符作为参数传递给Powershell脚本函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33141774/

相关文章:

azure - 如何在Azure搜索中通过两个字符串过滤集合过滤器?

azure - 使用 AAD 进行身份验证所需的步骤,Web 应用程序位于前端,API 位于 API 管理后面

function - 无法在 Visual Studio 2017 中调试 Azure 计时器函数

powershell - 有没有办法对 Powershell cmdlet 的结果进行自动换行?

xml - 如何从XML获取参数并执行exe列表?

windows - 在 PowerShell 中将位置设置为新安装的磁盘镜像失败

Azure Pipeline - Zip 部署

azure - 如何检查 Azure 中是否已存在资源组?

powershell - 控制 Powershell 控制台的窗口位置

powershell - 使用 Pester 5 和 PowerShell 7 模拟类函数