簡體   English   中英

使用.Net 5.0 運行遠程 PowerShell 腳本

[英]Running remote PowerShell script using .Net 5.0

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();
            }
        }

I am using the NuGet package 'Microsoft.PowerShell.5.ReferenceAssemblies 1.1.0', as it is showing as the recommended package in Visual Studio 2019 for resolving the types of "Runspace", "PowerShell" etc.

但是,創建“運行空間”或“PowerShell”的實例時出現異常。 異常是這樣的——“Common Language Runtime 檢測到無效程序”。

我發現這篇文章並意識到我確實收到了 NuGet package 的警告 -

“包‘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'。這個 package 可能與您的項目不完全兼容。”

按照帖子中的建議,我安裝了 Powershell Core 7.1.3,但警告和異常沒有解決。 然后我切換到“.Net Standard 2.0”,因為它是一個 class 庫,但沒有任何改變。 相同的警告消息和異常。

如何使用“.Net Core 5.0”(或“.Net Standard 2.0”)進行遠程 PowerShell 調用?

如果您想定位.Net 5,正確的 nuget package 使用的是Microsoft.PowerShell.ZF203675C5E5B63FAB86

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM