相关疑难解决方法(0)

使用IDE时从Powershell调用第三方可执行文件时出错

我有一个PowerShell脚本,它使用du.exe(最初来自Sysinternals的磁盘使用情况)来计算目录的大小.

如果我du c:\Backup在控制台中运行,它按预期工作,但在ISE或PowerGui中运行的相同代码行给出了预期的结果加上错误

+ du <<<<  c:\backup
+ CategoryInfo          : NotSpecified: (:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
Run Code Online (Sandbox Code Playgroud)

这是为什么?如何避免此错误?我尝试使用invoke-expression &,但是没有去.

谢谢您的帮助.

ide powershell du powergui

64
推荐指数
3
解决办法
5万
查看次数

$?之间的区别?和PowerShell中的$ LastExitCode

在PowerShell中,$?和之间有什么区别$LastExitCode

我读到了自动变量,它说:

$? Contains the execution status of the last operation. It contains TRUE if the last operation succeeded and FALSE if it failed.

$LastExitCode Contains the exit code of the last Windows-based program that was run.

$?它的定义中并没有解释成功和失败意味着什么.


我问,因为$?当且仅当$ LastExitCode为0时,我才认为这是真的,但我发现了一个令人惊讶的反例:$ LastExitCode = 0但是PowerShell中的$?= False.将stderr重定向到stdout会产生NativeCommandError.

windows powershell command-line

55
推荐指数
1
解决办法
6万
查看次数

Powershell中的`$?`是什么?

$?Powershell 是什么意思?


编辑:TechNet在重言式中的答案,没有解释"成功"或"失败"的含义.

$?
包含上次操作的执行状态.如果上一次操作成功,则包含TRUE;如果失败,则包含FALSE.

我推测是$?只会测试$ LastExitCode是否为0,但我找到了一个计数器示例,其中$?是假,但$ LastExitCode为True.

powershell

47
推荐指数
2
解决办法
3万
查看次数

捕获程序stdout和stderr以分隔变量

是否有可能在一次运行中将stdout从外部程序重定向到外部程序的变量和stderr到另一个变量?

例如:

$global:ERRORS = @();
$global:PROGERR = @();

function test() {
    # Can we redirect errors to $PROGERR here, leaving stdout for $OUTPUT?
    $OUTPUT = (& myprogram.exe 'argv[0]', 'argv[1]');

    if ( $OUTPUT | select-string -Pattern "foo" ) {
        # do stuff
    } else {
        $global:ERRORS += "test(): oh noes! 'foo' missing!";
    }
}

test;
if ( @($global:ERRORS).length -gt 0 ) {
    Write-Host "Script specific error occurred";
    foreach ( $err in $global:ERRORS ) {
        $host.ui.WriteErrorLine("err: $err");
    }
} else {
    Write-Host …
Run Code Online (Sandbox Code Playgroud)

windows powershell command-line stdout stderr

40
推荐指数
4
解决办法
4万
查看次数

Git clone:将stderr重定向到stdout但是将错误写入stderr

git clone它的输出写到stderr作为记录在这里.我可以使用以下命令重定向它:

git clone https://myrepo c:\repo 2>&1
Run Code Online (Sandbox Code Playgroud)

但是这会将所有输出(包括错误)重定向stderrstdout.有没有办法将进度消息重定向到stdout但仍然写入错误消息stderr.

git powershell git-clone

19
推荐指数
4
解决办法
6339
查看次数

在Windows PowerShell中忽略错误级别!= 0

我有一个运行外部EXE文件的脚本.当该EXE文件失败时(将errorlevel设置为1),PowerShell脚本将失败.

我正在运行curl.exe并得到这个:

  • CategoryInfo:NotSpecified:(%Total%... Time Current:String)[],RemoteException + FullyQualifiedErrorId:NativeCommandError

如何忽略/捕获外部EXE文件的失败并继续我的脚本?

powershell

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

PsExec引发错误消息,但没有任何问题

因此,我们在自动化中使用PsExec来安装虚拟机,因为我们无法在Windows 2003机器上使用ps远程会话.一切都很好,并没有问题,但PsExec不断抛出错误,即使每个命令都没有正确执行.例如:

D:\tools\pstools\psexec.exe $guestIP -u $global:default_user -p $global:default_pwd -d -i C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command "Enable-PSRemoting -Force"
Run Code Online (Sandbox Code Playgroud)

在guest虚拟机上启用PsRemoting,但也会抛出此错误消息:

psexec.exe : 
Bei D:\Scripts\VMware\VMware_Module5.ps1:489 Zeichen:29
+     D:\tools\pstools\psexec.exe <<<<  $guestIP -u $global:default_user -p $global:default_pwd -d -i C:\Windows\System32\WindowsPowerShell\
v1.0\powershell.exe -command "Enable-PSRemoting -Force"
+ CategoryInfo          : NotSpecified: (:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError

PsExec v1.98 - Execute processes remotely
Copyright (C) 2001-2010 Mark Russinovich
Sysinternals - www.sysinternals.com


Connecting to 172.17.23.95...Starting PsExec service on 172.17.23.95...Connecting with PsExec service on 172.17.23.95...Starting C:\Windows\
System32\WindowsPowerShell\v1.0\powershell.exe on 172.17.23.95...
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe started on 172.17.23.95 …
Run Code Online (Sandbox Code Playgroud)

windows powershell automation psexec

10
推荐指数
2
解决办法
2万
查看次数

不要在从外部命令写入 stderr 时抛出 PowerShell 异常

当外部命令(例如 git)写入 stderr 时,PowerShell 会生成 NativeCommandError 异常。我想正常地看到输出和标准输出,类似于它在标准 UNIX/Linux 系统上的外观。此脚本需要运行许多本机命令,如果可能,我更喜欢不会给每个命令增加太多混乱和维护的解决方案。

在 Linux 上,可以这样检出分支:

$ git checkout devel
Branch 'devel' set up to track remote branch 'devel' from 'origin'.
Switched to a new branch 'devel'
Run Code Online (Sandbox Code Playgroud)

其中微妙的部分是,无论出于何种原因,最后一行都已写入 stderr。但是,git 的退出状态为零表示它成功了。在 PowerShell 下,我得到了这个:

PS> git checkout devel
Branch 'devel' set up to track remote branch 'devel' from 'origin'.
git : Switched to a new branch 'devel'
At line:1 char:1
+ git checkout devel
+ ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Switched to a new branch 'devel':String) …
Run Code Online (Sandbox Code Playgroud)

git error-handling powershell

9
推荐指数
2
解决办法
1291
查看次数

Powershell类似于Bash的`set -e`

我怎样才能让Powershell像旗帜一样表现得像Bash set -eset -o errexit使Bash脚本"如果一个简单的命令以非零状态退出则立即退出".

我以为我可以通过设置来做到这一点,$ErrorActionPreference="Stop"但这似乎不起作用.假设我有一个脚本a.ps1

$ErrorActionPreference="Stop"
& cmd /c "exit 1"
echo "cmd exited `$LastExitCode=$LastExitCode and `$?=$?"
Run Code Online (Sandbox Code Playgroud)

如果我运行它

.\a. ; echo "a.ps1 exited `$LastExitCode=$LastExitCode `$?=$?"
Run Code Online (Sandbox Code Playgroud)

令我惊讶的是它打印出来

cmd exited $LastExitCode=1 and $?=False
a.ps1 exited $LastExitCode=1 $?=True
Run Code Online (Sandbox Code Playgroud)

这是怎么回事?!我希望a.ps1在第一行之后退出,抛出错误并设置$?为假.

有没有官方文档解释$ ErrorActionPreference?所有我发现了这个帖子上关于咖啡博客.

.net bash powershell

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

继续执行Exception

下面是我想要执行的脚本.这里的问题是一旦发生异常就会停止执行,我continue在catch块中使用但是没有用.即使在发生异常后它如何循环,我如何使它工作foreach.

我也使用了while($true)循环但是进入了无限循环.怎么去呢?

$ErrorActionPreference = "Stop";
try 
{
# Loop through each of the users in the site
foreach($user in $users)
{
    # Create an array that will be used to split the user name from the domain/membership provider
    $a=@()


    $displayname = $user.DisplayName
    $userlogin = $user.UserLogin


    # Separate the user name from the domain/membership provider
    if($userlogin.Contains('\'))
    {
        $a = $userlogin.split("\")
        $username = $a[1]
    }
    elseif($userlogin.Contains(':'))
    {
        $a = $userlogin.split(":")
        $username = $a[1]
    }

    # Create …
Run Code Online (Sandbox Code Playgroud)

error-handling powershell continue

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

powershell二进制文件比较

全部,有一个应用程序生成它的导出转储.我需要编写一个脚本,将比较前一天的转储与最新的转换,如果它们之间存在差异,我必须对移动和删除类型的东西进行一些基本的操作.我试过找到一个合适的方法,我尝试的方法是: $var_com=diff (get-content D:\local\prodexport2 -encoding Byte) (get-content D:\local\prodexport2 -encoding Byte) 我也尝试了Compare-Object cmdlet.我注意到内存使用率非常高,最终我System.OutOfMemoryException在几分钟后收到消息.你有一个人做了一些similer吗?请一些想法.有一个线程提到了一个比较,我不知道如何去做.在此先感谢大家Osp

powershell powershell-2.0

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

如何重定向 stdout 和 stderr 而不污染 PowerShell 错误输出

问题:

我试图通过 PowerShell 运行命令并捕获其 stdout 和 stderr而不将它们打印在屏幕上(命令非常嘈杂并污染控制台)。

我想捕获变量中的 stdout 和 stderr,然后在找到特定字符串时抛出异常。

我的逻辑似乎正常工作,我可以在我期望的时候使 cmdlet 失败/通过,但是输出与我的期望不匹配,而不是返回我指定的错误消息,我得到的是我认为是 stderr 的内容命令代替?

我的代码:

(为了便于阅读而进行了简化)
第一个 cmdlet:

function Test-Validation
{
    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true, Position = 0)]
        [Array]
        $ValidExitCodes = @(0),

        [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true, Position = 1)]
        [bool]
        $FailOnWarning = $true
    )
    $Validation = . pdk validate 2>&1

    if ($LASTEXITCODE -notin $ValidExitCodes)
    {
        throw "Module validation has failed. Exit code: $($LASTEXITCODE)."
    }
    $Warnings = $Validation -match 'pdk \(WARNING\)' …
Run Code Online (Sandbox Code Playgroud)

error-handling powershell stderr

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