我想从命令行调用我的PowerShell脚本中的特定函数。但是它返回了以下错误:
键不能为空。
$Get = $ini_file.($a_section).($b_node)
键不能为空。 参数名: key $Output = $ini_file.($a_section).($b_node) | Out-File $Store\Val ...
这是我的代码:
我可以帮您翻译。以下是翻译结果:
如果我运行该命令,仍然可以获取INI文件的
看起来我不能同时使用“调用函数”。但我不确定。
我的INI文件:
键不能为空。 参数名: key $Output = $ini_file.($a_section).($b_node) | Out-File $Store\Val ...
这是我的代码:
Param(
[Parameter(Mandatory=$true)]
[string]$FilePath,
$ini, $a_section, $b_node, $c_store,
$a, $b, $c
)
function Get-File {
$input_file = $FilePath
$ini_file = @{}
Get-Content $input_file | ForEach-Object {
$_.Trim()
} | Where-Object {
$_ -notmatch '^(;|$)'
} | ForEach-Object {
if ($_ -match '^\[.*\]$') {
$section = $_ -replace '\[|\]'
$ini_file[$section] = @{}
} else {
$key, $value = $_ -split '\s*=\s*', 2
$ini_file[$section][$key] = $value
}
}
$Get = $ini_file.($a_section).($b_node)
$Store = $c_store
$Output = $ini_file.($a_section).($b_node) | Out-File $Store\Value.cmd
}
function Write-File {
Import-Module PsIni
$ff = Get-IniContent $FilePath
$ff["$a"]["$b"] = "$c"
$ff | Out-IniFile -FilePath $FilePath -Force
}
# Calling the function with the global parameter $FilePath
Write-File $FilePath $a $b $c
Get-File $FilePath $ini $a_section $b_node $c_store
我可以帮您翻译。以下是翻译结果:
我从命令行调用每个函数的方式是:
调用函数Get-File
:
powershell.ps1 Get-File -FilePath C:\User\file.ini -a_section Information -b_node Name -c_store C:\User\
如果我运行该命令,仍然可以获取INI文件的
value
,但会返回上述错误。如果我删除这部分Write-File $FilePath $a $b $c
,就不会返回任何错误。看起来我不能同时使用“调用函数”。但我不确定。
我的INI文件:
[Information] Name=Joyce Age=12
更新代码:
Function Read-File
{
Param(
[parameter(mandatory=$true)]$FilePath,
[parameter(mandatory=$true)] $ini,
[parameter(mandatory=$true)] $a_section,
[parameter(mandatory=$true)] $b_node,
[parameter(mandatory=$true)] $c_store
)
$input_file = $FilePath
$ini_file = @{}
Get-Content $input_file | ForEach-Object {
$_.Trim()
} | Where-Object {
$_ -notmatch '^(;|$)'
} | ForEach-Object {
if ($_ -match '^\[.*\]$') {
$section = $_ -replace '\[|\]'
$ini_file[$section] = @{}
} else {
$key, $value = $_ -split '\s*=\s*', 2
$ini_file[$section][$key] = $value
}
}
$Get = $ini_file.($a_section).($b_node)
$Store = $c_store
$Output = $ini_file.($a_section).($b_node) | Out-File $Store\Value.cmd
}
#---------------------------------------------------------------#
Function Write-File
{
Param(
[parameter(mandatory=$true)]$FilePath,
[parameter(mandatory=$true)] $a,
[parameter(mandatory=$true)] $b,
[parameter(mandatory=$true)] $c
)
Import-Module PsIni
$ff = Get-IniContent $FilePath
$ff["$a"]["$b"] = "$c"
$ff | Out-IniFile -FilePath $FilePath -Force
}
$cmd, $params = $args
& $cmd @params