regex - PowerShell Try 语句缺少其 Catch 或 finally 块
问题描述
我对 PowerShell很陌生,最近才发现这个工具的瑰宝。我拼凑了几个不同的脚本,以允许我在驱动器中搜索 Excel 文件并查看它们是否包含字符模式。我想我有我需要的一切,但是我不断收到大括号错误(据我所知)。
我的脚本的正则表达式部分有意义吗?下面的两行是否足以确定每个单元格值是否与规定的模式匹配?
$formula = $workSheet.cells.item($row,$column).value
if($formula -match [regex]$REGEX)
另外,在尝试运行脚本时,我不断收到错误消息(有关详细信息,请参阅后面的代码)。我尝试添加和删除大括号,因为这是消息不断告诉我的问题。
我的脚本是这样写的:
Function Search-Files-For-Patterns {
[cmdletbinding()]
Param (
[parameter(Mandatory)]
[ValidateScript({
Try {
If (Test-Path -Path $_) {$True}
Else {Throw "$($_) is not a valid path!"}
}
Catch {
Throw $_
}
})]
[String]$Source,
[parameter(Mandatory)]
[string]$SearchText
)
$LogCSV = "X:\PowerShell\Scrape.csv"
$Filelist = Get-ChildItem $Source -Filter '*.xls*' -Recurse
$Excel = New-Object -ComObject Excel.Application
$DisableAutomationSecurity = [Microsoft.Office.Core.MsoAutomationSecurity]::msoAutomationSecurityForceDisable
$EnableAutomationSecurity = [Microsoft.Office.Core.MsoAutomationSecurity]::msoAutomationSecurityByUI
$REGEX = "\d\d\d-\d\d\d-\d\d\d"
$Excel.Application.AutomationSecurity = $DisableAutomationSecurity
Remove-Item $LogCSV -ErrorAction SilentlyContinue
Out-File -FilePath $LogCSV -InputObject "File;Count;NAS"
#"File;Count" > $LogCSV
foreach ($File in $Filelist) {
$password = ""
try {
$Workbook = $Excel.Workbooks.Open($File.FullName, 0, 0, 5, $password)
$Numbers = New-Object System.Collections.ArrayList
$NumberCount = 0
ForEach ($Worksheet in @($Workbook.Sheets)) {
$rowMax = ($Worksheet.usedRange.rows).count
$columnMax = ($Worksheet.usedRange.columns).count
For($row = 1 ; $row -le $rowMax ; $row ++) {
For($column = 1 ; $column -le $columnMax ; $column ++) {
$formula = $workSheet.cells.item($row,$column).value
#[string]$formula = $workSheet.cells.item($row,$column).value
if($formula -match [regex]$REGEX) {
$Numbers.Add("$($formula.Text);")
$NumberCount++
if ($NumberCount -eq 25) {break}
} #end for if $formula
} #end for $column
} #end for $row
if ($NumberCount -ge 10) {
Out-File -FilePath $LogCSV -InputObject "$($File.FullName);$NumberCount;$Numbers" -Append
} #end if $numbercount >= 10
$workbook.close($false)
} #end foreach worksheet
catch {
Out-File -FilePath $LogCSV -InputObject "$($File.FullName);File is password protected - skipped;" -Append
} #end catch
} #end try <--ERROR OCCURS HERE
} #end for each file
$Excel.Application.AutomationSecurity = $EnableAutomationSecurity
[void][System.Runtime.InteropServices.Marshal]::ReleaseComObject([System.__ComObject]$excel)
[gc]::Collect()
[gc]::WaitForPendingFinalizers()
Remove-Variable excel -ErrorAction SilentlyContinue
} #end function
Search-Files-For-Patterns "X:\SomeFolder" "\d\d\d-\d\d\d-\d\d"
我尝试运行时收到的错误消息是:
At X:\PowerShell\ScrapeG\Search-Files-For-Patterns.ps1:58 char:10
+ } #end try
+ ~
The Try statement is missing its Catch or Finally block.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingCatchOrFinally
任何方向/帮助将不胜感激。
提前致谢。
解决方案
尝试使用catch [System.Exception]
如果您收到错误消息:
Catch 块缺少其语句块
这是一个编译时错误。如果你有这样的事情:
try
{
...
}
catch (System.Exception)
{
write $_
return 'test'
}
尝试将异常括在括号中,而不是括号中:
try
{
...
}
catch [System.Exception]
{
write $_
return 'test'
}
推荐阅读
- c++ - CMakeLists 包含子目录中的头文件
- visual-studio-code - Visual Studio Code:如何禁用`editor.codeActionsOnSave`
- excel - How to save workbook to a new location without saving changes in the old one
- javascript - TypeError:当前用户在firebase反应中为空
- c# - 如何使用 OWIN 获取 keycloak 的 id_token?
- asp.net-core - 通过 Entity Framework Core 将多个数据库连接到 .NET Core API 项目
- javascript - 有条件地将属性(样式)添加到反应元素
- python - 使用反向索引进行字符串切片
- python - How to convert Bytes object to _io.BytesIO python?
- python - 我可以在 Google Colab 中使用可执行文件吗?