当包含“Microsoft.WindowsAzure.Storage”时,来自C#的Powershell代码不起作用

时间:2015-09-16 15:29:56

标签: c# asp.net-mvc-4 powershell .net-4.5 azure-storage

我有一个.NET 4.5 Web应用程序(ASP.NET MVC 4 Web应用程序),我的问题似乎是使用一些简单的 azure storage 功能运行 powershell代码从C#执行时不起作用。但是当它通过普通的PowerShell控制台或ISE执行时它将起作用。

Powershell(只需创建一个表并插入一列进行测试):

function Test-SetTableValues($storageAccount, $storageKey, $tableName, $valuesHash)
{
    $storageContext = New-AzureStorageContext $storageAccount -StorageAccountKey $storageKey
    $table = New-AzureStorageTable –Name $tableName –Context $storageContext

    $entity = New-Object Microsoft.WindowsAzure.Storage.Table.DynamicTableEntity "PartitionKey", "RowKey"
    foreach ($key in $valuesHash.Keys)
    {
        $entity.Properties.Add($key, $valuesHash[$key])
    }

    $result = $table.CloudTable.Execute([Microsoft.WindowsAzure.Storage.Table.TableOperation]::InsertOrMerge($entity), $null, $null)
}
Test-SetTableValues("STORAGE_ACCOUNT", "STORAGE_KEY", "TABLE_NAME", @{MyColumn="MyValue"})

通过C#执行时不起作用:

using System.Management.Automation;
using System.Management.Automation.Runspaces;
public static void Test()
        {
            var runspace = RunspaceFactory.CreateRunspace();
            runspace.Open();
            var powershell = PowerShell.Create();
            powershell.Runspace = runspace;

            powershell.AddScript(/*powershell code here*/);
            powershell.AddCommand("out-default");
            powershell.Invoke();
        }

原因是因为包含了库 Microsoft.WindowsAzure.Storage 。 如果我们删除它,代码将工作。 但我需要其他东西。

CLR版本对于所有环境(c#/ ise / powershell_console)都是相同的,因为我尝试输出变量 $ PSVersionTable (CLRVersion 4.0.30319.17400)。

我们得到的错误是:

  

无法使用值转换参数“operation”:   “Microsoft.WindowsAzure.Storag e.Table.TableOperation”,用于“执行”   键入“Microsoft.WindowsAzure.Storage.Table.TableOperation”:   “无法转换”Microsoft.WindowsAzure.Storage.Tabl   e.TableOperation“类型的值   键入“Microsoft.WindowsAzure.Storage.Table.TableOper ation”   “Microsoft.WindowsAzure.Storage.Table.TableOperation”。“At   OurPowershellFile.ps1:90 char:5   + $ result = $ table.CloudTable.Execute([Microsoft.WindowsAzure.Storage.Table .Ta   ...   + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~   ~~~       + CategoryInfo:NotSpecified:(:) [],MethodException       + FullyQualifiedErrorId:MethodArgumentConversionInvalidCastArgument

提前感谢您的回答。我只是找不到解决方案!

1 个答案:

答案 0 :(得分:1)

感谢微软支持似乎稍微修改了PowerShell代码"修复"问题。不确定原因,但下面的代码可以正常工作。

function Test-SetTableValues($storageAccount, $storageKey, $tableName, $valuesHash)
{
    $accountCredentials = New-Object "Microsoft.WindowsAzure.Storage.Auth.StorageCredentials" $storageAccount, $storageKey
    $storageAccount = New-Object "Microsoft.WindowsAzure.Storage.CloudStorageAccount" $accountCredentials, $true
    $tableClient = $storageAccount.CreateCloudTableClient()
    $table = $tableClient.GetTableReference($tableName)
    $table.CreateIfNotExists()

    $entity = New-Object Microsoft.WindowsAzure.Storage.Table.DynamicTableEntity "PartitionKey", "RowKey"
    foreach ($key in $valuesHash.Keys)
    {
        $entity.Properties.Add($key, $valuesHash[$key])
    }

    $result = $table.Execute([Microsoft.WindowsAzure.Storage.Table.TableOperation]::InsertOrMerge($entity))
}
Test-SetTableValues("STORAGE_ACCOUNT", "STORAGE_KEY", "TABLE_NAME", @{MyColumn="MyValue"})