首页 > 解决方案 > 使用 Powershell 查找 SSIS 包中的存储过程

问题描述

我正在尝试使用 PowerShell 搜索目录中的所有 SSIS 包,并返回以下内容:

我想用来搜索的标准只是“exec”,或者“exec or execute”(如果可能),我希望 exec 后面的字符串末尾的空格作为返回的分隔符.

我试过了:

Get-ChildItem -recurse | Select-String -pattern "exec" | group path | select name

但是,这仅返回文件名,而不是所需的附加文本。

如果有比 PowerShell 更好的方法来做到这一点,我愿意接受,但根据我目前发现的情况,这似乎是正确的道路。先感谢您!

编辑: 继续研究,我发现这个命令

Get-ChildItem -recurse | Select-String -pattern exec,execute | Select filename,linenumber

似乎为我正在寻找的命令提供了正确的文件和行号。有没有办法让 linenumber 引用的行打印而不是数字?我查看了 ForEach 和 Get-Content,但似乎无法完全正确地理解语法。

标签: powershellssisfull-text-searchtext-search

解决方案


这是一个应该证明对您有用的功能:

function Get-StoredProcedure {
    [CmdletBinding()]
    [OutputType('System.Management.Automation.PSObject')]
    param(
        [Parameter(Position = 0, ValueFromPipeline)]
        [System.IO.FileInfo[]] $Path = (Get-ChildItem -Recurse -File)
    )

    process {
        foreach ($item in $Path) {
            [pscustomobject]@{
                'Path'    = Split-Path -Parent -Path $item.FullName
                'Name'    = $item.Name
                'Command' = ($item | Select-String -Pattern 'exec(ute)?\s[^\s]+').Matches.Value
            }
        }
    }
}

这将遍历FileInfo您传递给它的任何对象(请注意使用-FileGet-ChildItem过滤掉该PSIsContainer属性)。它以字符串的形式返回一个对象,其中包含路径、文件名和命令exec(ute) <contiguous non-spaces>


推荐阅读