简体   繁体   English

调试PowerShell

[英]Debugging PowerShell

I'm not certain what is wrong with this scriptlet. 我不确定此脚本有什么问题。

I'm trying to break out functionality into several other functions (I have a programming background not a scripting one per se) and to me LOGICALLY the following should execute starting at the "main" function Test-SgnedMpsPackage, accepting the various optional parameters (the script is not yet complete) then when the function Check-Path is called, that is run, then work would resume in the original calling function. 我试图将功能分解为其他几个功能(我本身具有编程背景而不是脚本本身),并且对我来说,以下逻辑应该从“主要”功能Test-SgnedMpsPackage开始执行,接受各种可选参数(如果脚本尚未完成),则当调用Check-Path函数(即运行)时,工作将在原始调用函数中恢复。

Am I missing something here? 我在这里想念什么吗? On a side note, how does one return a value to the calling function? 附带说明一下,如何将一个值返回给调用函数? a simple return? 简单的回报?

function CheckPath($path)
{
    if ( test-path -Path $path ) 
        { Write-Host "{0} confirmed to exist." -f $path }
    else
        { Write-Host "{0} DOES NOT exis.\nPlease check and run the script again" -f $path }
    exit { exit }
}


function Test-SignedMpsPackage 
{
    Param(
        [string] $PkgSource,
        [string] $SigSource,
        [string] $Destination
    )
    Process
    {

        #Check that  both files exist
        Write-Host "Check for file existence..."


        CheckPath($PkgSource)
        CheckPath($SigSource)

        #retrieve signatures from file
    }
}

Unlike C, C++ or C# there is no "main" entry point function. 与C,C ++或C#不同,没有“主”入口点函数。 Any script at the top level - outside of a function - executes. 任何在函数外部的顶级脚本都将执行。 You have defined two functions above but you haven't called either one. 您已经在上面定义了两个函数,但是尚未调用任何一个。 You need to do something like this: 您需要执行以下操作:

function Test-SignedMpsPackage 
{
    ...
}

Test-SignedMpsPackage params

Also as mentioned by @Bill_Stewart, you call your defined functions just like you call PowerShell commands - arguments are space separated and you don't use parens except to evaluate an expression inside the parens. 同样,正如@Bill_Stewart所提到的那样,您就像调用PowerShell命令一样调用已定义的函数-参数之间用空格分隔,并且不使用括号,而是在括号内计算表达式。

As for returning a value from a function, any output (Output stream) not captured by assigning to a variable or being redirected to a file is automatically part of the function's output. 至于从函数返回值,未通过分配给变量或未重定向到文件而捕获的任何输出(输出流)将自动成为函数输出的一部分。 So I would modify your CheckPath function to this: 所以我将修改您的CheckPath函数:

function CheckPath($path)
{
    if (Test-Path -Path $path) { 
        Write-Verbose "{0} confirmed to exist." -f $path 
        $true
    }
    else { 
        Write-Verbose "{0} DOES NOT exist.\nPlease check and run the script again" -f $path 
        $false
    }
}

You can use Write-Host as you had before but sometimes, perhaps in a script, you don't want to see the extra output. 您可以像以前一样使用Write-Host,但是有时,也许在脚本中,您不想看到额外的输出。 That is where Write-Verbose comes in handy. 这就是Write-Verbose派上用场的地方。 Set $VerbosePreference = 'Continue' to see the verbose output. 设置$ VerbosePreference ='Continue'可以看到详细的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM