我刚刚在 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/