我是PowerShell的非常,最近才发现这个工具的瑰宝。我整理了一些不同的脚本,使我可以在驱动器中搜索Excel文件,并查看它们是否包含字符模式。我想我拥有了我需要的所有东西,但是我一直在用牙套弄一个错误(据我所知)。
脚本的REGEX部分有意义吗?下面的两行是否足以确定每个单元格值是否与指定的模式匹配?
$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]
如果出现错误:
这是一个编译时错误。
如果您有这样的事情:
try
{
...
}
catch (System.Exception)
{
write $_
return 'test'
}
尝试将异常放在方括号内,而不放在括号中:
try
{
...
}
catch [System.Exception]
{
write $_
return 'test'
}
关于regex - PowerShell的Try语句缺少其Catch或Finally块,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50790359/