请参考以下示例: RunspaceSample(的String.Empty);失败的是“Add-VMToCluster”这个术语。不被识别为cmdlet的名称。 RunspaceSample(" localhost")或RunspaceSample(" somecomputerName")成功。任何指针为什么呢?包含RunspaceConnectionInfo是否更改了使用的powershell版本或用于执行的RunspaceConfiguration? 如果我使用RunName创建带有RunSpaceConnectionInfo的Runspace,并且计算机名为" localhost"甚至在本地计算机上执行powershell脚本?。
由于
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);
}
}
答案 0 :(得分:1)
FailoverClusters模块仅适用于64位PowerShell会话。将此程序集作为64位程序集解决了模块未找到问题。