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

Job*_*Job 3 powershell ini command-line

我想从命令行调用 powershell 脚本中的特定函数。但它返回给我这样的错误:键不能为空。

参数名称:键
$Get = $ini_file.($a_section).($b_node)

键不能为空。
参数名称:键
$输出 = $ini_file.($a_section).($b_node) | 输出文件 $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
Run Code Online (Sandbox Code Playgroud)

我从命令行调用每个函数的方式是:

通话功能Get-File

powershell.ps1 Get-File -FilePath C:\User\file.ini -a_section Information -b_node Name -c_store C:\User\
Run Code Online (Sandbox Code Playgroud)

如果我运行该命令,我仍然可以获得valueINI 文件,但它返回上述错误。如果我删除这部分Write-File $FilePath $a $b $c它不会返回任何错误。

看来我不能同时使用“调用函数”。但我对此并不确定。

我的 INI 文件

    [信息]
    姓名=乔伊斯
    年龄=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
Run Code Online (Sandbox Code Playgroud)

Ans*_*ers 9

您尝试调用该函数的方式不起作用,因为您将参数传递给脚本,但从未对它们执行任何操作。如果您希望在脚本运行时自动调用该函数,请从脚本内调用该函数。

function Write-IniFile {
    Param(
        ...
    )
    ...
}
Write-IniFile -FilePath C:\Users\file.ini -a Name -b Joyce -c 1309
Run Code Online (Sandbox Code Playgroud)

并像这样运行脚本:

PS.ps1
Run Code Online (Sandbox Code Playgroud)

否则,在调用函数之前对脚本进行点源:

. PS.ps1
Write-IniFile -FilePath C:\Users\file.ini -a Name -b Joyce -c 1309
Run Code Online (Sandbox Code Playgroud)

如果您希望能够使用参数运行脚本并将这些参数传递给函数,请使用第一种方法,但$args在函数上进行 splat:

function Write-IniFile {
    Param(
        ...
    )
    ...
}
Write-IniFile @args
Run Code Online (Sandbox Code Playgroud)

并像这样调用脚本:

PS.ps1 -FilePath C:\Users\file.ini -a Name -b Joyce -c 1309
Run Code Online (Sandbox Code Playgroud)

编辑:

如果您有一个具有多个函数的脚本,并且希望能够通过运行该脚本来调用其中任何函数,您可以拆分脚本参数:

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
Run Code Online (Sandbox Code Playgroud)

调用:

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

该声明

$cmd, $params = $args
Run Code Online (Sandbox Code Playgroud)

将第一个参数分配给变量$cmd,并将其余参数分配给变量$params。下一个声明

& $cmd @params
Run Code Online (Sandbox Code Playgroud)

$cmd通过调用运算符( &)进行调用,并将参数放入$params中。