2015-10-15 49 views
1

我只是Powershell腳本的新手。我只是在PowerShell腳本創建了一個方法 -將特殊字符作爲參數傳遞給Poweshell腳本函數

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":["[email protected]","[email protected]"],"CC":["[email protected]"],"BCC":[]} 

每次我嘗試編譯,它給了我一個錯誤 -

Unexpected token ':["[email protected]".com"' in expression or statement 

回答

1

的問題是JSON使用雙引號來分隔鍵和價值觀,相同的字符(雙引號)也使用PowerShell執行字符串「包裝」。

您可以使用單引號來包裝你的硬編碼輸入JSON像這樣:

'{"TO":["[email protected]","[email protected]"],"CC":["[email protected]"],"BCC":[]}' 

所以語句應該是:

Add-Entity -TableName $myTableName -PartitionKey "ABC" -RowKey "XYZ" -JsonString '{"TO":["[email protected]","[email protected]"],"CC":["[email protected]"],"BCC":[]}' 
+0

工作就像一個魅力。謝謝! –

相關問題