简体   繁体   中英

Some of the PowerShell module (Cluster cmdlets) does not get loaded when I create RunSpace on a localhost without providing runspaceconnectioninfo

Please refer the sample below: RunspaceSample(string.Empty); fails with The term 'Add-VMToCluster' is not recognized as the name of a cmdlet. Whereas the RunspaceSample("localhost") or RunspaceSample("somecomputerName") succeed. Any pointer why is it ? Does including RunspaceConnectionInfo changes the powershell version used or RunspaceConfiguration used for execution ? Also what would be the performance impact if I create Runspace with RunSpaceConnectionInfo with ComputerName = "localhost" even for executing powershell script on a local computer ?.

Thanks

public static void RunspaceSample(string computerName)
    {
        Runspace rs;
        if (string.IsNullOrEmpty(computerName))
        {
            rs = RunspaceFactory.CreateRunspace();
        }
        else
        {
            var connectionInfo = new WSManConnectionInfo
            {
                OperationTimeout = 10000,
                OpenTimeout = 10000,
                ComputerName = computerName
            };
            rs = RunspaceFactory.CreateRunspace(connectionInfo);
        }

        rs.Open();
        PowerShell ps = PowerShell.Create();
        ps.Runspace = rs;
        string script = @"$ComputerName = 'somevm'; $null = Add-VMToCluster -Name $ComputerName -VMName $ComputerName -ErrorAction SilentlyContinue -verbose";
        ps.AddScript(script);


        Console.WriteLine("Script: {0}", script);
        Console.WriteLine("------------------------");
        foreach (PSObject result in ps.Invoke())
        {
            Console.WriteLine(result.ToString());
        }

        if (ps.HadErrors)
        {
            var sb = new StringBuilder();
            foreach (var errorRecord in ps.Streams.Error)
            {
                sb.AppendFormat("\nError: {0} CategoryInfo: {1}", errorRecord.Exception.Message, (errorRecord.CategoryInfo != null) ? errorRecord.CategoryInfo.ToString() : string.Empty);
            }

            var errorMessage = sb.ToString();
            Console.WriteLine(errorMessage);
        }
    }

The FailoverClusters module is only available in 64-bit PowerShell sessions. Making this assembly as 64 bit assembly resolved the module not found issue.

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