如果文件夹中的Filetype不存在,则Get-ChildItem停止脚本

问题描述 投票:0回答:1

我试图在PowerShell脚本中处理一些“错误”。如果指定文件夹中不存在特定文件类型(.key),则该脚本无需执行任何操作,因此应终止。我使用此示例来解决我的问题,这不能按预期工作。 PowerShell没有终止。

$Eventlogfolder = "c:\temp\test\"

# Check if  subfolder exists and check if .key-files are available in this folder.
# if not - exit. There is nothing to do if no LicenceFile (*.key Files) are available.
$CheckFileExistence = Get-ChildItem $EventLogFolder -recurse -filter  "*.key"
if (!($CheckFileExistence)) {ErrorAction Stop}

我所知道的是,Get-ChildItem给出了一个空数组作为结果。没有错误消息或状态。我认为这就是ErrorAction在这里不起作用的原因。

所以我的问题是,如何退出Get-ChildItem?当我使用EXIT时,PowerShell ISE退出。

powershell get-childitem
1个回答
0
投票

像这样的东西?

$Eventlogfolder     = "c:\temp\test\"
$CheckFileExistence = Get-ChildItem $EventLogFolder -recurse -filter  "*.key"

if ([string]::IsNullOrEmpty($CheckFileExistence)){
    throw "No .key files found in $Eventlogfolder"
}else{
   # do stuff with .key files
}
© www.soinside.com 2019 - 2024. All rights reserved.