小编viR*_*iRg的帖子

PowerShell Try 语句缺少 Catch 或 Final 块

我对 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 …
Run Code Online (Sandbox Code Playgroud)

regex excel powershell

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

标签 统计

excel ×1

powershell ×1

regex ×1