正在尝试将c#代码转换为PowerShell版本5脚本
我有一个有效的c#应用程序。现在我将制作一个PowerShell脚本版本5 我不熟悉PowerShell脚本。我只是试着找到有什么方法可以做到,我发现我可以用以下方法做到:- 但我得到了解释错误 我的剧本:-正在尝试将c#代码转换为PowerShell版本5脚本,c#,powershell,C#,Powershell,我有一个有效的c#应用程序。现在我将制作一个PowerShell脚本版本5 我不熟悉PowerShell脚本。我只是试着找到有什么方法可以做到,我发现我可以用以下方法做到:- 但我得到了解释错误 我的剧本:- $Assem = ( "Newtonsoft.Json, version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed" ) $Source = @"
$Assem = (
"Newtonsoft.Json, version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed"
)
$Source = @"
susing Newtonsoft.Json.Linq;
using System;
using System.IO;
using Newtonsoft.Json.Schema;
using System.Collections.Generic;
using System.Reflection;
using System.Threading;
namespace Eurostep.SAS.Bootstrap
{
static class Program
{
static void Main(string[] args)
{
string file = "host.json"; ;
JObject json = JObject.Parse(File.ReadAllText(file));
JToken _;
if (json.TryGetValue("id", out _) == false)
{
json["id"] = Environment.MachineName;
}
JToken systemPathStr;
if (json.TryGetValue("systemPath", out systemPathStr) == false)
{
systemPathStr = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "home", "test");
}
DirectoryInfo systemPath = new DirectoryInfo(systemPathStr.ToString());
}
}
}
"@;
Add-Type -ReferencedAssemblies $Assem -TypeDefinition $Source -Language CSharp
我发现以下错误:-
Add-Type : (0) : Metadata file 'Newtonsoft.Json, version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed.dll' could not be
found
(1) : susing Newtonsoft.Json.Linq;
At C:\test\test.ps1:59 char:1
+ Add-Type -ReferencedAssemblies $Assem -TypeDefinition $Source -Langua ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (Microsoft.Power...peCompilerError:AddTypeCompilerError) [Add-Type], Exception
+ FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand
Add-Type : Cannot add type. Compilation errors occurred.
At C:\test\test.ps1:59 char:1
+ Add-Type -ReferencedAssemblies $Assem -TypeDefinition $Source -Langua ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Add-Type], InvalidOperationException
+ FullyQualifiedErrorId : COMPILER_ERRORS,Microsoft.PowerShell.Commands.AddTypeCommand
如何修复错误 我发现我必须加载程序集。有几种方法可以做到这一点。但最简单的方法是给出库的实际路径,如下所示:
Add-Type -Path 'C:\thirdparty\Newtonsoft\Newtonsoft.Json.dll'
使用Newtonsoft.Json.Linq时,在
前面有一个s
字符代码>。删除它,您的代码将被编译。有一个输入错误:使用Newtonsoft.Json.Linq;=>在排版后使用。新错误:-添加类型:(0):找不到元数据文件“Newtonsoft.Json,version=6.0.0.0,Culture=neutral,PublicKeyToken=30ad4fe6b2a6eed.dll”(1):在C:\practice\try.ps1:46 char:1+Add Type-referencedsassemblems$Assem-TypeDefinition$Source-Langua…我想我需要知道如何在PowerShell脚本中加载外部dll或程序集。