【发布时间】:2023-03-31 16:51:01
【问题描述】:
C#中的方法调用是这样的-
public void GetKey()
{
WSManConnectionInfo connectioninfo = new WSManConnectionInfo();
var ss = new NetworkCredential("xxx.yyyy\\Administrator", "PassFail2Hehe");
connectioninfo.ComputerName = "<some IP Address>";
connectioninfo.Credential = new PSCredential(ss.UserName, ss.SecurePassword);
//Runspace runspace = RunspaceFactory.CreateRunspace(connectioninfo);
//runspace.Open();
using (PowerShell ps = PowerShell.Create())
{
var re = ps.AddScript("Get-Service");
var results = re.Invoke();
}
}
我正在使用 NuGet 包“Microsoft.PowerShell.5.ReferenceAssemblies 1.1.0”,因为它在 Visual Studio 2019 中显示为推荐的包,用于解析“运行空间”、“PowerShell”等类型。
但是,创建“运行空间”或“PowerShell”实例时出现异常。异常是这样的——“Common Language Runtime检测到无效程序。”
我发现this 的帖子并意识到我确实收到了 NuGet 包的警告 -
“包 'Microsoft.PowerShell.5.ReferenceAssemblies 1.1.0' 已使用 '.NETFramework,Version=v4.6.1 恢复, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8' 而不是项目目标框架 'net5.0'。此包可能与您的项目不完全兼容。”
按照帖子中的建议,我安装了 Powershell Core 7.1.3,但没有解决警告和异常。然后我切换到“.Net Standard 2.0”,因为它是一个类库,但没有任何改变。相同的警告消息和异常。
如何使用“.Net Core 5.0”(或“.Net Standard 2.0”)进行远程 PowerShell 调用?
【问题讨论】:
标签: c# .net-core powershell-remoting .net-standard-2.0 powershell-core