相关疑难解决方法(0)

如何从命令提示符将布尔值传递给PowerShell脚本

我必须从批处理文件中调用PowerShell脚本.脚本的一个参数是布尔值:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -File .\RunScript.ps1 -Turn 1 -Unify $false
Run Code Online (Sandbox Code Playgroud)

该命令失败,并显示以下错误:

Cannot process argument transformation on parameter 'Unify'. Cannot convert value "System.String" to type "System.Boolean", parameters of this type only accept booleans or numbers, use $true, $false, 1 or 0 instead.

At line:0 char:1
+  <<<< <br/>
+ CategoryInfo          : InvalidData: (:) [RunScript.ps1], ParentContainsErrorRecordException <br/>
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,RunScript.ps1
Run Code Online (Sandbox Code Playgroud)

截至目前,我在我的脚本中使用字符串进行布尔转换.但是如何将布尔参数传递给PowerShell?

powershell scripting

93
推荐指数
7
解决办法
15万
查看次数

使用命令行中的完整路径和参数运行 powershell 命令

我有一个位于Program Files文件夹中并接受参数的脚本:verbose从其restart 文件夹运行它效果很好:

powershell ./<file.ps1> -verbose:$True -restart
Run Code Online (Sandbox Code Playgroud)

尝试使用完整路径运行它是我遇到问题的地方:

powershell & "C:\Program Files\Folder\<file.ps1> -verbose:$True -restart"
Run Code Online (Sandbox Code Playgroud)

上面的命令不运行脚本;相反,它会打开 PS 命令提示符,退出时会在记事本中打开脚本。
我还尝试将每个变量放在单独的引号中,但这效果不佳。

我找到了使用progra~1代替的解决方法Program Files,但我想以正确的方式解决问题。

我缺少什么?

powershell command-line command-prompt

8
推荐指数
1
解决办法
3万
查看次数

如何防止主机退出并返回退出码?

每当启动新的 PowerShell 进程时,Ansgar Wiechers 的答案都很有效。/sf/answers/3514186441/这适用于 cmd.exe 和 powershell.exe。

C:>type .\exit1.ps1
function ExitWithCode($exitcode) {
  $host.SetShouldExit($exitcode)
  exit $exitcode
}
ExitWithCode 23
Run Code Online (Sandbox Code Playgroud)

在 cmd.exe 交互式 shell 中。

C:>powershell -NoProfile -Command .\exit1.ps1
C:>echo %ERRORLEVEL%
23
C:>powershell -NoProfile -File .\exit1.ps1
C:>echo %ERRORLEVEL%
23
Run Code Online (Sandbox Code Playgroud)

在 PowerShell 交互式 shell 中。

PS C:>powershell -NoProfile -Command .\exit1.ps1
PS C:>$LASTEXITCODE
23
PS C:>powershell -NoProfile -File .\exit1.ps1
PS C:>$LASTEXITCODE
23
Run Code Online (Sandbox Code Playgroud)

但是...在现有交互式 PowerShell 主机内运行 .ps1 脚本将完全退出主机。

PS C:>.\exit1.ps1
    <<<poof! gone! outahere!>>>
Run Code Online (Sandbox Code Playgroud)

如何阻止它退出主机 shell?

powershell exit-code

5
推荐指数
1
解决办法
1841
查看次数

为什么“退出”功能在我的 PowerShell 代码中不起作用?

当我尝试“退出”功能时。它不会关闭控制台。它没有任何作用。如何更改它以使其退出控制台?

Write-Host "-------------- MENU ---------------"
Write-Host "Utility Menu"
Write-Host "1 = Help Ipconfig"
Write-Host "2 = Ipconfig"
Write-Host "3 = Notepad"
Write-Host "4 = Calculator"
Write-Host "5 = Exit"
Write-Host "-----------------------------------"

$userinput = Read-Host "Please select a menu item: "

switch ( $userinput )
{
  1 {Help Ipconfig}
  2 {ipconfig}
  3 {start notepad}
  4 {start calc.exe}
  5 {Exit}
}
Run Code Online (Sandbox Code Playgroud)

powershell

4
推荐指数
1
解决办法
4887
查看次数

如何从 PowerShell 脚本获取退出代码

我正在尝试编写一个 PowerShell 脚本,每当我们的构建系统无法将 Docker 映像推送到 Docker Hub 时,该脚本都会发送 Slack 通知。如何检测并处理Process exited with code 1命令docker push?我已尝试以下操作,但未catch调用该块。

try {
  docker push $dockerImage;
}catch{
  #Send error details to Slack
}
Run Code Online (Sandbox Code Playgroud)

error-handling powershell try-catch exit-code

4
推荐指数
1
解决办法
4646
查看次数

如何在不关闭 powershell 控制台的情况下从 Powershell 模块函数返回非零退出代码?

我的 powershell 模块有一个函数,我希望它返回一个非零退出代码。但是,作为一个模块函数,当我运行时,它会加载到 powershell 控制台的上下文中Import-Module。因此,当函数执行时exit 1- poof,控制台窗口就会出现!

至少,这就是我解释它退出时关闭 powershell 窗口的方式。

那么,ps 模块函数如何以非零退出代码退出而不杀死导入模块的控制台呢?

聚苯乙烯

我确实注意到关于这个主题的几个问题,但似乎没有人研究这个特殊情况。

编辑1

我想提供一些背景信息。我有一个PS模块,有很多功能。其中一些在 Azure DevOps yaml 构建脚本中按原样使用。后者知道识别非零退出代码并中止管道,因此不需要从函数中抛出来中止流程。

但是,如果我想从控制台调用该函数,例如快速测试某些内容并且它以非零代码退出,则整个控制台窗口将关闭。这非常烦人。

有时有一个解决方法。所以,代替这段代码:

dotnet build ...
$ExitCode = $LastExitCode
DoSomethingInAnyCase()
if ($ExitCode)
{
    exit $ExitCode
}
Run Code Online (Sandbox Code Playgroud)

我们可以有以下版本:

try
{
    dotnet build ...
}
finally
{
    DoSomethingInAnyCase()
}
Run Code Online (Sandbox Code Playgroud)

两个版本都会正确返回正确的退出代码,但由于第二个版本没有显式exit语句,因此它不会关闭控制台。

powershell

3
推荐指数
1
解决办法
4218
查看次数

如果程序失败,powershell 脚本将停止(如 bash `set -o errexit`)

如果程序失败,是否有一个优雅的 Powershell 脚本设置可以退出正在运行的 Powershell 脚本(或 shell 实例)?

我正在想象类似 Bash 功能set -o errexit(或set -e)但用于 Powershell 的东西。在该功能中,如果 bash 脚本中的程序失败(进程返回代码不是0),则 bash shell 实例会立即退出。

在 powershell 中,脚本可以检查$LastExitCode. 但是,对于每个程序调用来说,这变得很麻烦。也许powershell有一个功能可以自动检查程序返回码并做出反应。

在脚本中解释

Set-Powershell-Auto-Exit 'ProgramReturnCode'  # what should this be?
& program.exe --fail  # this program fails with an error code
& foo.exe             # this never runs because script has exited
Run Code Online (Sandbox Code Playgroud)

bash powershell

2
推荐指数
1
解决办法
249
查看次数

从PowerShell脚本返回退出代码

我对从cmd.exe调用运行时从PowerShell返回退出代码值有疑问。我发现https://weblogs.asp.net/soever/returning-an-exit-code-from-a-powershell-script有帮助。但是,PowerShell代码的解决方案是添加一个功能。

function ExitWithCode { param($exitcode) $host.SetShouldExit($exitcode) exit }
Run Code Online (Sandbox Code Playgroud)

产生:

ExitWithCode : The term 'ExitWithCode' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path
was included, verify that the path is correct and try again.
At C:\src\t\e\exit5.ps1:6 char:1
+ ExitWithCode -exitcode 12345
+ ~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (ExitWithCode:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
Run Code Online (Sandbox Code Playgroud)

但是,将“出口”放在新行上是可行的。这只是语言异常吗?

function ExitWithCode { param($exitcode) $host.SetShouldExit($exitcode)
exit } …
Run Code Online (Sandbox Code Playgroud)

powershell

1
推荐指数
3
解决办法
8148
查看次数

批处理文件中的退出代码不会传播到父 powershell 脚本

请注意:

c:\temp\1.cmd

@echo off
setlocal

cmd /c dir aaa
IF %ERRORLEVEL% NEQ 0 GOTO fail
GOTO end
:fail
echo - Script failed

:end
endlocal
Run Code Online (Sandbox Code Playgroud)

现在,如果我在命令提示符下运行它:

C:\temp> cmd
Microsoft Windows [Version 10.0.16299.967]
(c) 2017 Microsoft Corporation. All rights reserved.

C:\temp>c:\temp\1.cmd
 Volume in drive C has no label.
 Volume Serial Number is 4A5E-F223

 Directory of C:\temp

File Not Found
- Script failed

C:\temp>echo %errorlevel%
1

C:\temp>
Run Code Online (Sandbox Code Playgroud)

现在我从 Powershell 运行它:

C:\temp> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.16299.967
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, …
Run Code Online (Sandbox Code Playgroud)

powershell

1
推荐指数
1
解决办法
489
查看次数