[英]powershell Import-module from C# is always failing
I am using dot net version 4.5 and SCVMM 2012 R2. 我正在使用点网版本4.5和SCVMM 2012 R2。 trying to run below code 尝试运行以下代码
InitialSessionState initial = InitialSessionState.CreateDefault();
initial.ImportPSModule(new string[] { "C:\\Program Files\\Microsoft System Center 2012 R2\\Virtual Machine Manager\\bin\\psModules\\virtualmachinemanager\\virtualmachinemanager.psd1" });
Runspace runspace = RunspaceFactory.CreateRunspace(initial);
runspace.Open();
PowerShell ps = PowerShell.Create();
ps.Runspace = runspace;
ps.Commands.AddCommand("Get-SCVMMserver");
ps.AddParameter("ComputerName", "some.domain.com");
foreach (PSObject result in ps.Invoke())
{
Console.WriteLine(result.Members["IsSupported"].Value);
}
It always fails wit hthe following error, 它始终会因以下错误而失败,
The following error occurred while loading the extended type data file: Microsoft.PowerShell, C:\\Program Files\\Microsoft System Center 2012 >R2\\Virtual Machine Manager\\bin\\virtualmachinemanager.types.ps1xml(5) : Error >in type "Microsoft.SystemCenter.VirtualMachineManager.VMCheckpoint": >Exception: Cannot convert the >"Microsoft.SystemCenter.VirtualMachineManager.Cmdlets.VMCheckpointConverter" value of type "System.String" to type "System.Type". 加载扩展类型的数据文件时发生以下错误:Microsoft.PowerShell,C:\\ Program Files \\ Microsoft系统中心2012> R2 \\ Virtual Machine Manager \\ bin \\ virtualmachinemanager.types.ps1xml(5):错误>在“ Microsoft”类型中.SystemCenter.VirtualMachineManager.VMCheckpoint”:>异常:无法将类型为“ System.String”的>“ Microsoft.SystemCenter.VirtualMachineManager.Cmdlets.VMCheckpointConverter”值转换为类型“ System.Type”。 Microsoft.PowerShell, C:\\Program Files\\Microsoft System Center 2012 >R2\\Virtual Machine Manager\\bin\\virtualmachinemanager.types.ps1xml(11) : >Error in type "Microsoft.SystemCenter.VirtualMachineManager.Task": >Exception: Cannot convert the >"Microsoft.SystemCenter.VirtualMachineManager.Cmdlets.TaskConverter" value >of type "System.String" to type "System.Type". Microsoft.PowerShell,C:\\ Program Files \\ Microsoft系统中心2012> R2 \\ Virtual Machine Manager \\ bin \\ virtualmachinemanager.types.ps1xml(11):>类型“ Microsoft.SystemCenter.VirtualMachineManager.Task”中的错误:>异常:无法将类型为“ System.String”的>“ Microsoft.SystemCenter.VirtualMachineManager.Cmdlets.TaskConverter”值转换为类型“ System.Type”。
please help me this error. 请帮助我这个错误。 Thanks 谢谢
The error message states Cannot convert the >"Microsoft.SystemCenter.VirtualMachineManager.Cmdlets.TaskConverter" value >of type "System.String" to type "System.Type".
错误消息指出Cannot convert the >"Microsoft.SystemCenter.VirtualMachineManager.Cmdlets.TaskConverter" value >of type "System.String" to type "System.Type".
Just change the line 2 from string[]
to new[]
and it should be fine i think. 只需将第2行从string[]
更改为new[]
,我认为应该没问题。
initial.ImportPSModule(new[] { "C:\\Program Files\\Microsoft System Center 2012 R2\\Virtual Machine Manager\\bin\\psModules\\virtualmachinemanager\\virtualmachinemanager.psd1" });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.