[英]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 的所有版本只需要一个包)。
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.