简体   繁体   English

运行 powershell 脚本时云不加载文件或程序集“System.Management.Automation”

[英]Cloud not load file or assembly 'System.Management.Automation' while running powershell script

So I am trying to run some powershell script on my WPF app to update my IpRules on Azure but even simple script like "Write-Output 'Hello, World:'" gives me this error.所以我试图在我的 WPF 应用程序上运行一些 powershell 脚本来更新我在 Azure 上的 IpRules 但即使是像“Write-Output 'Hello, World:'”这样的简单脚本也会给我这个错误。 Could not load file or assembly 'System.Management,Automation.无法加载文件或程序集“System.Management,Automation。 Version=7.2.8,0, Culture=neutral.版本=7.2.8,0,文化=中性。 PublicKeyToken=token123456'. PublicKeyToken=token123456'。 The system cannot find the file specified: Here is my code:系统找不到指定的文件:这是我的代码:

        public Collection<PSObject> GetExistingFirewallIPRules(string script, Dictionary<string, object> scriptParameters)
        {
            PowerShell ps = PowerShell.Create();
            ps.AddScript(script);
            return ps.Invoke();
        }

And here is.csproj这是.csproj

 <PropertyGroup>
    <TargetFramework>net6.0-windows</TargetFramework>
    <Nullable>enable</Nullable>
    <UseWPF>true</UseWPF>
      <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Azure.ResourceManager.AppService" Version="1.0.0" />
    <PackageReference Include="Azure.ResourceManager.CosmosDB" Version="1.2.0" />
    <PackageReference Include="Microsoft.PowerShell.SDK" Version="7.3.1" />
    <PackageReference Include="Prism.Core" Version="8.1.97" />
    <PackageReference Include="Prism.Wpf" Version="8.1.97" />
    <PackageReference Include="System.Management.Automation" Version="7.2.8" />
  </ItemGroup>

How can I fix this error or is it any other way to update my CosmosDB IpRules in Azure portal than running powershell (eg. "Update-AzCosmosDBAccount -ResourceGroupName $resourceGroupName -Name $accountName -IpRangeFilter $ipFilter") script?我该如何修复此错误,或者是否有任何其他方法可以在 Azure 门户中更新我的 CosmosDB IpRules 而不是运行 powershell(例如“Update-AzCosmosDBAccount -ResourceGroupName $resourceGroupName -Name $accountName -IpRangeFilter $ipFilter”)脚本?

Using the System.Management.Automation NuGet package directly is not recommended - see this answer for what package to choose for what scenario.建议直接使用System.Management.Automation NuGet package - 请参阅此答案以了解 package 为何种场景选择。

In the context of a .NET (Core) application, use only the Microsoft.PowerShell.SDK package (similarly, all flavors of the PowerShell SDK only require one package)..NET(核心)应用程序的上下文中,使用Microsoft.PowerShell.SDK package (类似地,PowerShell SDK 的所有版本只需要一个包)。

  • That you mistakenly used both and had conflicting version numbers is likely what caused your problem.您错误地使用了两者并且版本号冲突可能是导致您出现问题的原因。

Since you're targeting .NET 6 , you must use an older version of that package, given that the version that is current as of this writing, 7.3.1 requires .NET 7 ;由于您的目标是 .NET 6 ,因此您必须使用该 package 的版本,因为在撰写本文时, 7.3.1的最新版本需要 .NET 7 for .NET 6, use a 7.2.x version对于 .NET 6,使用7.2.x版本

In other words:换句话说:

  • Remove line <PackageReference Include="System.Management.Automation" Version="7.2.8" />删除行<PackageReference Include="System.Management.Automation" Version="7.2.8" />

  • Update line <PackageReference Include="Microsoft.PowerShell.SDK" Version="7.3.1" /> to将行<PackageReference Include="Microsoft.PowerShell.SDK" Version="7.3.1" />更新为
    <PackageReference Include="Microsoft.PowerShell.SDK" Version="7.2.8" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法加载文件或程序集“System.Management.Automation,版本 = 3.0.0.0 - Could not load file or assembly 'System.Management.Automation, Version=3.0.0.0 运行 Start-DSCConfiguration 时出错:无法加载文件或程序集“System.Management.Automation”,版本 = 7.1.0.0 - Error when running Start-DSCConfiguration: Could not load file or assembly 'System.Management.Automation', Version=7.1.0.0 System.Management.Automation中没有PowerShell? - No PowerShell in System.Management.Automation? 加载System.Management.Automation程序集时出错 - Error loading System.Management.Automation assembly 有没有一种方法可以捕获Powershell脚本执行的输出,而无需使用System.Management.Automation - Is there a way to capture powershell script executed output without using System.Management.Automation Visual Studio 2010 system.management.automation装配参考 - visual studio 2010 system.management.automation assembly reference MVC System.Management.Automation Powershell执行中的访问被拒绝错误 - Access Denied error in MVC System.Management.Automation powershell execution 从 System.Management.Automation 调用脚本时未设置 $PSScriptRoot - $PSScriptRoot not set when invoking script from System.Management.Automation System.Management.Automation错误访问被拒绝 - System.Management.Automation error access denied 2.0版的System.Management.Automation? - 2.0 version of System.Management.Automation?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM