【问题标题】:Function Get-IniContent is not recognized - INI file support inPowerShell无法识别函数 Get-IniContent - PowerShell 中的 INI 文件支持
【发布时间】:2019-08-15 18:00:58
【问题描述】:

我想编辑 INI 文件的值。我使用这个脚本,但它给了我错误。

Get-IniContent : The term 'Get-IniContent' is not recognized as the name of a 
cmdlet, function, script file, or operable program.

c:\Users\file.ini 处我的 INI 文件的内容:

[XXX]
AB=23
BC=34

读取和更新脚本的内容:

# Read the content of an *.ini file into a (nested) hashtable.
$ini = Get-IniContent "C:\Users\file.ini"

# Update the 'AB' entry in section [XXX] in-memory.
$ini["XXX"]["AB"] = "12"  

# Write the updated content back to the *.ini file.
$ini | Out-IniFile -FilePath "C:\Users\file.ini -Force"

【问题讨论】:

  • 这是因为Get-IniContent 不是普通 PowerShell 安装中包含的标准 cmdlet。 (Out-IniFile 也不是。)网上有各种脚本提供此类 cmdlet,但您必须先在会话中导入它们才能获取它们。试试Import-Module PsIni——不过你可能需要先安装它。

标签: powershell ini


【解决方案1】:

JeroenMostert 在评论中提供了关键指针:

PowerShell,从 v7 开始,没有用于处理 INI 文件的内置 cmdlet (*.ini),尽管引入了此类 cmdlet is being discussed on GitHub

Get-IniContentOut-IniFilethird-party PSIni moduleavailable from the PowerShell Gallery 附带的高级函数(类似 cmdlet 的函数)。

PowerShellGet 模块[1] 随附的 PowerShell v5 或更高版本中,安装非常简单:

Install-Module -Scope CurrentUser PsIni

如果您省略-Scope CurrentUser,您将为所有用户安装该模块,但这样做需要以管理权限运行。

$PSModuleAutoLoadingPreference 处于默认状态(未设置),然后此模块会按需自动加载到尝试调用模块命令之一的会话中,例如 Get-IniContent

这是一个完整的、独立的示例,用于练习 PsIni 模块的核心功能:

  • 按需安装模块
  • 使用 Out-IniFile 从嵌套的有序哈希表中从头开始创建示例 *.ini 文件。
  • 使用Get-IniContent 将文件从磁盘读取到(新)嵌套有序哈希表中
  • 修改和删除条目
  • 将修改后的哈希表写回带有Out-IniFile的文件

注意:假设Install-Module可用,即安装了PowerShellGet模块,并且运行机器在线并允许从https://www.powershellgallery.com/下载包。

# Import the PsIni module.
# If necessary, install it first, for the current user.
$ErrorActionPreference = 'Stop' # Abort, if something unexpectedly goes wrong.
try {
  Import-Module PsIni
} catch {
  Install-Module -Scope CurrentUser PsIni
  Import-Module PsIni
}

# Create an ordered hashtable that is the in-memory representation of the
# sample *.ini file from the question, with a second section added.
$iniFileContent = [ordered] @{
  # 'XXX' is the section name.
  # The nested hashtable contains that section's entries.
  XXX = [ordered] @{  
    # IMPORTANT: 
    #  * The PsIni module only supports STRING values.
    #  * While you can assign values of different types in-memory, they are
    #    CONVERTED TO STRINGS with .ToString() and READ AS STRINGS later
    #    by Get-IniContent.
    #  * In v3+, PSIni now supports values in *.ini files that have 
    #    embedded quoting - e.g., `AB = "23"` as a raw line - which is
    #    (sensibly) *stripped* on reading the values.
    AB = '23'
    BC = '34'
  }
  # Create a 2nd section, named 'YYY', with entries 'yin' and 'yang'
  YYY = [ordered] @{
    yin = 'foo'
    yang = 'none'
  }
}

# Use Out-IniFile to create file 'file.ini' in the current dir.
# * Default encoding is UTF-8 (with BOM in Windows PowerShell, without BOM
#   in PowerShell Core)
# * Use -Encoding to override, but note that
#   Get-IniContent has no matching -Encoding parameter, so the encoding you use
#   must be detectable by PowerShell in the absence of explicit information.
# * CAVEAT: -Force is only needed if an existing file must be overwritten.
#           I'm using it here so you can run the sample code repeatedly without
#           failure, but in general you should only use it if you want to
#           blindly replace an existing file - such as after having modified
#           the in-memory representation of an *.ini file and wanting to
#           write the modifications back to disk - see below.
$iniFileContent | Out-IniFile -Force file.ini

# Read the file back into a (new) ordered hashtable 
$iniFileContent = Get-IniContent file.ini

# Modify the value of the [XXX] section's 'AB' entry.
$iniFileContent.XXX.AB = '12'

# Use the alternative *indexing syntax* (which is equivalent in most cases)
# to also modify the [YYY] section's 'yin' entry.
$iniFileContent['YYY']['yin'] = 'bar'

# Remove the 'yang' value from section [YYY]:
$iniFileContent.YYY.Remove('yang')

# Save the modified content back to the original file.
# Note that -Force is now *required* to signal the explicit intent to
# replace the existing file.
$iniFileContent | Out-IniFile -Force file.ini

# Double-check that modifying the values succeeded.
(Get-IniContent file.ini).XXX.AB # should output '12'
(Get-IniContent file.ini).YYY.yin # should output 'bar'

# Print the updated content of the INI file, which
# shows the updated values and the removal of 'yang' from [YYY].
"--- Contents of file.ini:"
Get-Content file.ini

上面运行应该成功,输出如下,说明*.ini文件创建成功,读回内存,修改,保存回磁盘:

12
bar
--- Contents of file.ini:
[XXX]
AB=12
BC=34
[YYY]
yin=bar

[1] 您可以为 PowerShell 版本 3 和 4 按需安装 PowerShellGet - 请参阅 https://www.microsoft.com/en-us/download/details.aspx?id=51451

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-06
    • 2015-05-26
    • 2019-01-22
    • 1970-01-01
    • 2014-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多