viR*_*iRg 5 regex excel powershell
我对 PowerShell非常陌生,最近才发现这个工具的瑰宝。我拼凑了几个不同的脚本,以便在驱动器中搜索 Excel 文件并查看它们是否包含字符模式。我想我已经拥有了我需要的一切,但是我一直收到大括号错误(据我所知)。
我的脚本的 REGEX 部分有意义吗?下面的两行足以确定每个单元格值是否符合规定的模式吗?
$formula = $workSheet.cells.item($row,$column).value
if($formula -match [regex]$REGEX)
Run Code Online (Sandbox Code Playgroud)
另外,在尝试运行脚本时,我不断收到错误(有关详细信息,请参阅后面的代码)。我尝试添加和删除大括号,因为这就是消息不断告诉我是否出现问题的原因。
我的脚本是这样写的:
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"
Run Code Online (Sandbox Code Playgroud)
当我尝试运行此命令时收到的错误消息是:
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
Run Code Online (Sandbox Code Playgroud)
任何指导/帮助将不胜感激。
提前致谢。
尝试使用catch [System.Exception]
如果出现错误:
Catch 块缺少其语句块
这是一个编译时错误。如果你有这样的事情:
try
{
...
}
catch (System.Exception)
{
write $_
return 'test'
}
Run Code Online (Sandbox Code Playgroud)
尝试将异常括在方括号中,而不是圆括号中:
try
{
...
}
catch [System.Exception]
{
write $_
return 'test'
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9611 次 |
| 最近记录: |