我要编辑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"
答案 0 :(得分:1)
JeroenMostert在评论中提供了关键指针:
PowerShell从v6.2.0开始,没有 no 内置cmdlet来处理INI文件(*.ini
),尽管引入了此类cmdlet {{3 }}。
Get-IniContent
和Out-IniFile
是 is being discussed on GitHub ,third-party PSIni
module随附的高级功能(类似于cmdlet的功能)。
在PowerShellGet
模块 [1] 随附的PowerShell v5或更高版本中,安装很容易:
Install-Module -Scope CurrentUser PsIni
如果省略-Scope CurrentUser
,则将为所有用户安装该模块,但这样做需要具有管理权限。
将$PSModuleAutoLoadingPreference
设为默认值(未设置),然后根据需要自动将该模块自动加载到一个会话中,该会话尝试调用该模块的命令之一,例如{{1 }}。
这是一个完整的,自成体系的示例,它行使Get-IniContent
模块的核心功能:
PsIni
开始,从嵌套的有序哈希表中创建示例*.ini
文件。Out-IniFile
从磁盘读取文件到(新的)嵌套有序哈希表中Get-IniContent
将修改后的哈希表写回到文件中注意:假设Out-IniFile
可用,即已安装Install-Module
模块,并且正在运行的计算机处于联机状态,并允许从available from the PowerShell Gallery下载软件包。
PowerShellGet
运行上述操作应成功并输出以下内容,表明# 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'
# Rmove 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
[1]您可以根据需要安装PowerShell版本3和4的12
bar
--- Contents of file.ini:
[XXX]
AB=12
BC=34
[YYY]
yin=bar
-请参见https://www.powershellgallery.com/