小编Sha*_*iss的帖子

尝试捕捉实际上不会捕捉

我有一些用户在 gui 脚本中选择的选定项目,那些代表 GPO 现在我有他想要链接到的 OU。代码有效,但我想为用户提供链接和无法链接的结果,因为它们已经链接但是由于某种原因,这个 try catch 不会这样做,gui 输出成功的链接并写入链接已经存在的控制台错误

$ResultsTextBox.clear()
 #$listBox1.selecteditems
 $SWITCH = Get-ADOrganizationalUnit -filter *  -Property CanonicalName | Where-Object {$_.CanonicalName -eq $listBox2.SelectedItem}

 forEach ($line in $listBox1.selecteditems){
 #Link each selected item GPO to the OU
 try {
    New-GPlink -name $line -target $SWITCH
    $ResultsTextBox.AppendText("`n GPO: $line HAVE BEEN LINKED Successfully.`n")
        }

    catch{$ResultsTextBox.AppendText("`n COULDN NOT LINK GPO: $line TO $SWITCH `n")
    }
 }
Run Code Online (Sandbox Code Playgroud)

这里有什么问题?

error-handling powershell

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

如果找到文件则停止 get-Childitem

我有这个代码:

    $Paths = 'C:\' , 'P:\' , "\\fril01\ufr$\$env:username"

    $torEXE = Get-childitem -path $paths -recurse -Exclude $ExcludePaths -erroraction 'silentlycontinue'  | where-object {$_.name -eq "Tor.exe"}

    if ($torEXE.Exists) {$answer = 1}

Run Code Online (Sandbox Code Playgroud)

检查文件 tor.exe,但正如您所看到的,此检查可能需要一些时间。检查可能会在前几秒找到 tor.exe,但会继续检查所有路径。我希望它在找到 tor.exe 后立即停止,而不是继续搜索它。如何做呢?

windows directory powershell get-childitem

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