【问题标题】:Cloud not load file or assembly 'System.Management.Automation' while running powershell script云在运行 powershell 脚本时不加载文件或程序集 \'System.Management.Automation\'
【发布时间】:2023-01-11 04:01:36
【问题描述】:

所以我试图在我的 WPF 应用程序上运行一些 powershell 脚本来更新我在 Azure 上的 IpRules 但即使是像“Write-Output 'Hello, World!'”这样的简单脚本也会给我这个错误: 无法加载文件或程序集“System.Management.Automation,Version=7.2.8.0,Culture=neutral,PublicKeyToken=token123456”。该系统找不到指定的文件。 这是我的代码:

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

这是.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>

我该如何修复此错误,或者是否有任何其他方法可以在 Azure 门户中更新我的 CosmosDB IpRules 而不是运行 powershell(例如“Update-AzCosmosDBAccount -ResourceGroupName $resourceGroupName -Name $accountName -IpRangeFilter $ipFilter”)脚本?

【问题讨论】:

  • 可能是版本问题。尝试使用 Visual Studio 打开。如果您从解决方案资源管理器中得到相同的错误,则删除引用而不是添加返回引用,这将提供最新版本。 VS 应该在打开时自动更新版本,但如果找不到库就会失败。
  • 我仍然遇到同样的错误,但还有一件事。我无法安装最新的软件包,因为我收到此“严重性代码描述项目文件行抑制状态错误 NU1202 软件包 System.Management.Automation 7.3.1 与 net6.0-windows7.0 不兼容(.NETCoreApp,Version=v6.0 ).Package System.Management.Automation 7.3.1 支持:net7.0 (.NETCoreApp,Version=v7.0) Dover.DXM.DevTool.Modules.SqlConfiguration C:\Users\...." 1
  • 您需要获取与核心兼容的 Nuget 库:nuget.org/packages/System.Management/?force_isolation=true
  • 我将 System.Management.Automation 库版本更改为 7.2.8,它似乎可以正常工作。谢谢

标签: c# .net wpf azure powershell


【解决方案1】:

使用 System.Management.Automation NuGet 包直接地不是推荐 - 请参阅this answer 以了解在什么情况下选择什么包。

在一个上下文中.NET(核心)应用,利用只要Microsoft.PowerShell.SDK(相似地,全部PowerShell SDK 的变种只需要包裹)。

  • 你误用了两个都并且有版本号冲突可能是导致您的问题的原因。

由于您的目标是 .NET6个,你必须使用年长的该包的版本,鉴于在撰写本文时的最新版本,7.3.1 需要 .NET7;对于 .NET 6,使用 7.2.x 版本

换句话说:

  • 删除行&lt;PackageReference Include="System.Management.Automation" Version="7.2.8" /&gt;

  • &lt;PackageReference Include="Microsoft.PowerShell.SDK" Version="7.3.1" /&gt; 行更新为
    &lt;PackageReference Include="Microsoft.PowerShell.SDK" Version="7.2.8" /&gt;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-10
    • 2015-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-14
    • 1970-01-01
    相关资源
    最近更新 更多