繁体   English   中英

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

[英]Powershell code from C# doesn't work when “Microsoft.WindowsAzure.Storage” is included

我有一个.NET 4.5 Web应用程序(ASP.NET MVC 4 Web应用程序),我的问题似乎是从C#执行带有某些简单的Azure存储功能的Powershell代码无法正常工作。 但是当通过普通的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库。 如果我们删除它,代码将起作用。 但是我还需要其他东西。

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

我们得到的错误是这样的:

无法将参数“ operation”转换为值:“ Microsoft.WindowsAzure.Storag e.Table.TableOperation”,对于“ Execute”键入“ Microsoft.WindowsAzure.Storage.Table.TableOperation”:“无法转换“ Microsoft.WindowsAzure。 “类型为” Microsoft.WindowsAzure.Storage.Table.TableOperation“的” Storage.Tabl e.TableOperation“值键入” Microsoft.WindowsAzure.Storage.Table.TableOperation“。 在OurPowershellFile.ps1:90 char:5 + $ result = $ table.CloudTable.Execute([Microsoft.WindowsAzure.Storage.Table .Ta ... + ~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~ + CategoryInfo:未指定:(:) [],MethodException + FullyQualifiedErrorId:MethodArgumentConversionInvalidCastArgument

预先感谢您的任何答案。 我只是找不到解决方案!

得益于Microsoft的支持,似乎修改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"})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM