简体   繁体   中英

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

I have a .NET 4.5 web app (ASP.NET MVC 4 Web Application) and my problem seems to be that running powershell code with some simple azure storage functionality will not work when it is executed from C#. But it will work when executed through the normal powershell console or the ISE.

Powershell (Just creates a table and inserts a column for testing):

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"})

It doesn't work when executed through 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();
        }

And the reason is because the library Microsoft.WindowsAzure.Storage is included. If we remove it, the code will work. But I need it for other things.

The CLR Version is the same for all environments(c#/ise/powershell_console) as I tried outputing the variable $PSVersionTable (CLRVersion 4.0.30319.17400).

The error we get is this:

Cannot convert argument "operation", with value: "Microsoft.WindowsAzure.Storag e.Table.TableOperation", for "Execute" to type "Microsoft.WindowsAzure.Storage. Table.TableOperation": "Cannot convert the "Microsoft.WindowsAzure.Storage.Tabl e.TableOperation" value of type "Microsoft.WindowsAzure.Storage.Table.TableOper ation" to type "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

Thanks in advance for any answers. I just can't find a solution for this!

Thanks to microsoft support is seems like modifying the powershell code slightly "fixes" the issue. Not sure why exactly but the below code works.

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"})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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