使用PowerShell读取AppConfig。

huangapple go评论61阅读模式
英文:

Use PowerShell to Read AppConfig

问题

I would like to use PowerShell to read and write to a database as part of a larger project. The connection string is already in the appsettings.json file in the project.

我想使用PowerShell来读取和写入数据库,作为一个更大项目的一部分。连接字符串已经在项目的appsettings.json文件中。

I searched around and found $json = Get-Content 'appsettings.json' | Out-String | ConvertFrom-Json

我搜索了一下,找到了$json = Get-Content 'appsettings.json' | Out-String | ConvertFrom-Json

I keep getting an error:

我一直得到一个错误:

ConvertFrom-Json : Invalid object passed in, ':' or '}' expected. (252): {

ConvertFrom-Json:传入的对象无效,期望':'或'}'。 (252): {

But the file has no issue when it is being read in by the C# application.

但是当C#应用程序读取该文件时,文件没有问题。

Is there a way to read the system configuration files in the same fashion as a C# application?

是否有一种方式可以像C#应用程序一样读取系统配置文件?

Something like:

类似于:

var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

Configuration = builder.Build();

像这样:

var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

Configuration = builder.Build();

Thanks in advance.

提前感谢。

英文:

I would like to use powershell to read and write to a database as part of a larger project. The connection string is already in the appsettings.json file in the project.

I searched around and found $json = Get-Content 'appsettings.json' | Out-String | ConvertFrom-Json

I keep getting an error:

ConvertFrom-Json : Invalid object passed in, ':' or '}' expected. (252): {

But the file has no issue when it is being read in by the C# application.

Is there a way to read the system configuration files in the same fashion as a C# application?

Something like:

 var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

        Configuration = builder.Build();

Thanks in advance.

答案1

得分: 0

以下是翻译好的内容:

你的命令是正确的,out-string 不是必要的,但也不会造成任何问题。

Get-Content appsettings.json | ConvertFrom-Json

你确定你正在从正确的目录获取正确的文件吗?如果不确定,可以直接在 PowerShell 中显示文件的内容。

Get-Content appsettings.json

通常,你可以这样从 appsettings.json 中访问连接字符串:

(Get-Content appsettings.json | ConvertFrom-Json).ConnectionStrings.MyDatabase

在我的示例中,我使用名称 "MyDatabase" 获取连接字符串。这个示例的 appsettings.json 如下所示:

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  },
  "ConnectionStrings": {
    "MyDatabase": "Server=localhost;Database=test;"
  }
}
英文:

Your commands are correct, the out-string is not necessary, but does not break anything either.

Get-Content appsettings.json | ConvertFrom-Json

Are you sure you are getting the right file from the right directory? If in doubt, have the contents of the file displayed directly in PowerShell.

Get-Content appsettings.json

Generally, you can access a connection string from appsettings.json this way:

(Get-Content appsettings.json | ConvertFrom-Json).ConnectionStrings.MyDatabase

In my example, I get the connection string with the name "MyDatabase". The appsettings.json for this example looks like this:

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
    "Default": "Debug",
    "System": "Information",
    "Microsoft": "Information"
  }
  },
  "ConnectionStrings": {
    "MyDatabase": "Server=localhost;Database=test;"
  },
}

答案2

得分: 0

我发现我的 JSON 文件中有评论。最终我使用了以下代码:

$json = Get-Content '.appsettings.json' | Where-Object {$_ -notmatch '//'} | Out-String
$json = $json | ConvertFrom-Json

这将删除了注释行,并将所有内容转换成了我期望的形式。我成功获取了我的连接字符串:

$Conn01 = new-object System.Data.SqlClient.SqlConnection $json.ConnectionStrings.DefaultConnection
英文:

I found that I have comments in my json file. I ended up using:

$json = Get-Content '.appsettings.json' | Where-Object {$_ -notmatch '//'} | Out-String 
$json = $json | ConvertFrom-Json

This removed the comment line and everything converted as I expected. I was able to get my connection string:

$Conn01 = new-object System.Data.SqlClient.SqlConnection $json.ConnectionStrings.DefaultConnection

huangapple
  • 本文由 发表于 2023年5月10日 22:23:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76219591.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定