如何从命令行调用PowerShell脚本中的特定函数?

3
我想从命令行调用我的PowerShell脚本中的特定函数。但是它返回了以下错误: 键不能为空。 $Get = $ini_file.($a_section).($b_node)
键不能为空。 参数名: 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

这段代码是有效的。 - Job
1个回答

9
你尝试调用函数的方式不起作用,因为你正在向脚本传递参数,但从未使用它们。如果你希望在运行脚本时自动调用该函数,请从脚本内部调用该函数。
function Write-IniFile {
    Param(
        ...
    )
    ...
}
Write-IniFile -FilePath C:\Users\file.ini -a Name -b Joyce -c 1309

并像这样运行脚本:

PS.ps1

否则,在调用函数之前,请对脚本进行点源处理:
. PS.ps1
Write-IniFile -FilePath C:\Users\file.ini -a Name -b Joyce -c 1309

如果您想使用参数运行脚本并将其传递给函数,请使用第一种方法,但在函数上使用splat $args
function Write-IniFile {
    Param(
        ...
    )
    ...
}
Write-IniFile @args

然后像这样调用脚本:

PS.ps1 -FilePath C:\Users\file.ini -a Name -b Joyce -c 1309

编辑:

如果您有一个包含多个函数的脚本,并且想要通过运行脚本来调用任何一个函数,您可以将脚本参数进行 split'n'splat(分离和展开)处理:

PS.ps1:

function Foo {
    Param(
        [Parameter(Mandatory=$true)]
        $x
    )
    ...
}

function Bar {
    Param(
        [Parameter(Mandatory=$true)]
        $y,
        [Parameter(Mandatory=$true)]
        $z
    )
    ...
}

$cmd, $params = $args
& $cmd @params

调用:

PS.ps1 Foo -x 'some'          # invoke Foo() with the given arguments
PS.ps1 Bar -y 'or' -z 'other' # invoke Bar() with the given arguments

该语句

$cmd, $params = $args

将第一个参数分配给变量$cmd,将其余参数分配给变量$params。下一条语句。
& $cmd @params

通过调用运算符&)调用 $cmd 并在 $params 中使用 splats 参数。

дҪҶжҳҜжҲ‘зҡ„жңҹжңӣжҳҜпјҢеҪ“жҲ‘жңү2дёӘжҲ–3дёӘеҮҪж•°ж—¶пјҢжҲ‘еҸҜд»ҘдҪҝз”ЁиҝҷдёӘе‘Ҫд»Ө PS.ps1 function1 FilePath -a Name -b Joyce -c 1309 еҰӮжһңжҲ‘еҸӘжғіжү§иЎҢfunction1гҖӮ PS.ps1 function2...... еҰӮжһңжҲ‘еҸӘжғіжү§иЎҢfunction2гҖӮ - Job
@工作 你应该在你的问题中解释清楚。请查看更新后的答案。 - Ansgar Wiechers
它仍然没有解决我的问题。我更新了问题。 - Job
@工作 因为你从函数中删除了参数定义,并在脚本中放置了一个全局参数定义。我的回答中哪里建议这样做了?无论如何,已经更新以提高清晰度。 - Ansgar Wiechers
我创建了新的更新代码。这是你的意思吗?@Ansgar Wiechers? - Job
更新的代码正常工作。非常感谢 @Ansgar Wiechers - Job

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接