简体   繁体   English

如何退出PowerShell中的Invoke-Command -ScriptBlock?

[英]How do you exit from an Invoke-Command -ScriptBlock in PowerShell?

How do you exit from an invoke-command script block running on a remote server? 如何退出在远程服务器上运行的invoke-command脚本块? I have tried next . next试过了。

Here is my code: 这是我的代码:

$Res = Invoke-Command -Session $Ses -ArgumentList ($ROOTdir, $PARAMS.SYS, $PARAMS.main, $PARAMS.zip) -ScriptBlock {
    Param($ROOTdir, $SYS, $MAIN, $ZIP)
    $list | %{
        $completed = $false
        $retrycount = 1
        while (-not $completed) {
            try {
                $Copytime = (Measure-Command {
                    Copy-Item -Path $_.FullName -Destination ($SYS.KITCHENdir) -Force -ErrorVariable copyerror
                }).TotalSeconds
                $completed = $true
            } catch {
                if ($retrycount -gt $MAIN.Retry ) {
                    break or exit #HOW STOP EXECUTING NEXT STEPS AND EXIT
                } else {
                    Start-Sleep $MAIN.DelayRetry
                    $retrycount++
                }
            }
        }
        #IF copy bad result need stop and exit
        #The final one can produce flavors here, but it does not look quite kosher
        #next steps
    }

I suspect you want to use the Exit keyword. 我怀疑你想使用Exit关键字。

"The exit keyword is used to exit from contexts; it will exit the (currently running) context where your code is running. This means that if you use this keyword in a script, and launch the script directly from your console, it will exit both the script and the console since they're both running in the same context. However, if you use the exit keyword in a function within the script, and call that function from the same script, it will just exit the script and not the entire console. The exit keyword is best used in functions, and when those functions are called in a script. It's a great way to terminate execution of functions." “exit关键字用于退出上下文;它将退出运行代码的(当前正在运行的)上下文。这意味着如果在脚本中使用此关键字,并直接从控制台启动脚本,它将退出脚本和控制台,因为它们都在相同的上下文中运行。但是,如果在脚本中的函数中使用exit关键字,并从同一个脚本调用该函数,它将只退出脚本而不是exit关键字最好用在函数中,当在脚本中调用这些函数时,这是终止函数执行的好方法。“

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

相关问题 具有任意ScriptBlock的Powershell调用命令 - Powershell Invoke-Command with arbitrary ScriptBlock 将脚本块作为参数传递给 powershell 中的 Invoke-Command 脚本块 - Passing scriptblock as an argument to Invoke-Command scriptblock in powershell Powershell - Invoke-Command 脚本块中的“Copy-item”命令 - Powershell - “Copy-item” command in scriptblock of Invoke-Command Powershell $using 变量不能与脚本块中的 Invoke-Command 一起使用 - Powershell $using variable not working with Invoke-Command in scriptblock 如何在PowerShell 4.0 Invoke-Command -ScriptBlock中正确将字符串数组作为参数传递 - How to correctly pass a string array as a parameter in a PowerShell 4.0 Invoke-Command -ScriptBlock 如何捕获使用Powershell的Invoke-Command调用的ScriptBlock的返回值 - How to capture the Return Value of a ScriptBlock invoked with Powershell's Invoke-Command 将 Invoke-Command 脚本块内部的变量发送回主机 - Send variables from inside Invoke-Command scriptblock back to host 从 Invoke-Command 执行嵌套的 ScriptBlock 时出错 - Getting an error when executing a nested ScriptBlock from Invoke-Command 使用invoke-command时如何将xml元素传递给scriptBlock - How to pass an xml element to a scriptBlock when using invoke-command 文件路径中带有脚本块的调用命令-无参数 - Invoke-Command with Scriptblock in Filepath - no arguments
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM