2016-06-28 88 views
0

我在使用Powershell for Azure時遇到問題。我擁有的是一個來自我運行Powershell Azure命令的小型C#控制檯應用程序。Powershell Azure CmdletInvocationException

這是我的代碼:rs.Open()

例外如下::上線時

public class PowerShellService : IDisposable 
{ 
    private PowerShell _shellInstance; 
    private PSDataCollection<PSObject> _outputCollection; 
    private Runspace rs; 

    public event EventHandler<DataEventArgs> Data; 
    public event EventHandler<ErrorRecordEventArgs> Error; 

    public PowerShellService() 
    { 
     InitializeShellInstance(); 
    } 

    private void InitializeShellInstance() 
    { 
     InitialSessionState iss = InitialSessionState.CreateDefault(); 
     string[] modules = new string[] { "Full path to Azure.psd1", "Full path to AzureRM.psd1" }; 
     iss.ImportPSModule(modules); 

     _shellInstance = PowerShell.Create(); 
     string initializationScript = "Set-ExecutionPolicy -Scope Process -ExecutionPolicy RemoteSigned"; 
     _shellInstance.AddScript(initializationScript); 
     _shellInstance.Invoke(); 

     rs = RunspaceFactory.CreateRunspace(iss); 
     rs.Open(); 
     ... 
     ... 
     ... 

異常

An unhandled exception of type 'System.Management.Automation.CmdletInvocationException' occurred in System.Management.Automation.dll 

Additional information: Exception calling ".ctor" with "0" argument(s): "The type initializer for 'Microsoft.Azure.Commands.ResourceManager.Common.AzureRMCmdlet' threw an exception." 

回答

0

解決方案:我重新安裝了與Azure的一切,然後它工作