我有一个有效的c#应用程序。现在我将制作PowerShell脚本版本5。
我是PowerShell脚本新手。只是试图找到有任何办法,我发现我可以通过以下方式做到: -
但我得到翻译错误。
我的剧本: -
$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
如何修复错误?
答案 0 :(得分:-1)
我发现我必须加载程序集。有几种方法可以做到这一点。但最简单的方法是将库的实际路径设为:
Add-Type -Path 'C:\thirdparty\Newtonsoft\Newtonsoft.Json.dll'