You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PowerShell读取appsettings.json配置及实现数据库读写的问题

使用PowerShell读取appsettings.json配置及实现数据库读写的问题

嘿,我来帮你解决这个问题!首先针对你遇到的ConvertFrom-Json报错情况,大概率是因为你的appsettings.json文件编码不是UTF-8(比如带BOM的UTF-8),或者文件里包含了原生PowerShell的ConvertFrom-Json不支持的注释(毕竟C#配置系统允许注释,但原生PowerShell的JSON解析器不买账)。不过既然你想和C#一样的逻辑读取配置,直接用.NET官方的配置系统会更靠谱,完全对齐C#的处理逻辑,还能自动解决编码、注释这些麻烦问题。

一、用.NET配置系统读取appsettings.json(和C#逻辑完全一致)

你可以在PowerShell里引用Microsoft.Extensions.Configuration相关的NuGet包,复刻C#里的配置读取流程:

  1. 安装必要的NuGet包
    在PowerShell中执行以下命令(支持PowerShell 5.1及以上版本,PowerShell Core也适用):

    # 安装配置核心包和JSON配置包
    Install-Package Microsoft.Extensions.Configuration -Scope CurrentUser
    Install-Package Microsoft.Extensions.Configuration.Json -Scope CurrentUser
    
  2. 编写读取配置的代码
    几乎和你C#里的代码一模一样,用PowerShell实现:

    # 加载安装好的.NET程序集(注意替换成你实际的包版本和路径)
    $packageVersion = "6.0.0" # 可以用Get-Package查看已安装的版本
    $nugetPath = "$env:USERPROFILE\.nuget\packages"
    Add-Type -Path "$nugetPath\microsoft.extensions.configuration\$packageVersion\lib\netstandard2.0\Microsoft.Extensions.Configuration.dll"
    Add-Type -Path "$nugetPath\microsoft.extensions.configuration.json\$packageVersion\lib\netstandard2.0\Microsoft.Extensions.Configuration.Json.dll"
    Add-Type -Path "$nugetPath\microsoft.extensions.configuration.abstractions\$packageVersion\lib\netstandard2.0\Microsoft.Extensions.Configuration.Abstractions.dll"
    Add-Type -Path "$nugetPath\microsoft.extensions.fileproviders.physical\$packageVersion\lib\netstandard2.0\Microsoft.Extensions.FileProviders.Physical.dll"
    
    # 构建配置对象,和C#逻辑完全对齐
    $builder = [Microsoft.Extensions.Configuration.ConfigurationBuilder]::new()
    $builder = $builder.SetBasePath($PWD.Path) # 用当前脚本目录作为基础路径,对应C#的env.ContentRootPath
    $builder = $builder.AddJsonFile("appsettings.json", $true, $true) # 可选文件、支持自动重载
    $configuration = $builder.Build()
    
    # 读取连接字符串,比如配置里的节点是ConnectionStrings:DefaultConnection
    $connectionString = $configuration.GetSection("ConnectionStrings:DefaultConnection").Value
    Write-Host "成功读取连接字符串:$connectionString"
    

二、原生ConvertFrom-Json报错的临时解决办法(不想装包的话)

如果只是临时读取配置,不想安装NuGet包,可以试试这几个方案:

  • 把appsettings.json保存为UTF-8无BOM格式
  • 移除文件里的所有注释(原生ConvertFrom-Json不支持注释)
  • 指定编码读取文件:
    $json = Get-Content 'appsettings.json' -Encoding UTF8 | Out-String | ConvertFrom-Json
    $connectionString = $json.ConnectionStrings.DefaultConnection
    

三、读取配置后实现数据库读写

拿到连接字符串后,就可以轻松操作数据库了,这里以SQL Server为例:

读取数据示例

$query = "SELECT TOP 10 * FROM YourTableName"
# 执行查询并返回结果
$dbResults = Invoke-SqlCmd -ConnectionString $connectionString -Query $query
# 格式化输出结果
$dbResults | Format-Table -AutoSize

写入数据示例

$insertQuery = "INSERT INTO YourTableName (Column1, Column2) VALUES ('测试值1', '测试值2')"
# 执行插入操作
Invoke-SqlCmd -ConnectionString $connectionString -Query $insertQuery
Write-Host "数据插入完成"

如果是MySQL、PostgreSQL等其他数据库,可以安装对应的PowerShell模块(比如MySQLCmdlets),或者用.NET的DbConnection对象进行底层操作。

备注:内容来源于stack exchange,提问作者Ken Kazinski

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.22 12:08:11