格式化数组内部的数组,以便稍后用作电子邮件正文

时间:2017-09-20 12:42:50

标签: powershell

我在我的脚本中提前创建了一个空的$array,然后我循环了一些东西并希望将这些东西添加到我的数组中。这可以按预期工作。

$AspxFiles = gci $path $filter -r | ? { $_.DirectoryName -notlike '*Werkstatt*' -and `
                                        $_.DirectoryName -notlike '*- Kopie*'   -and `
                                        $_.DirectoryName -notlike '*.1*'        -and `
                                        $_.DirectoryName -notlike '*.0*'        -and `
                                        $_.DirectoryName -notlike '*.2*'        -and `
                                        $_.Name -notlike '*- Kopie*'
                                        }
$array = @()
foreach ($file in $AspxFiles)
{
    $getURL = sls -Path $file.FullName -Pattern $regex -AllMatches | ? { $_ -notlike '*www.w3.org*' -and `
                                                                         $_ -notlike '*jquery.com*' -and `
                                                                         $_ -notlike '*www.mwe*' } | 
    % { $_.Matches } | % { $_.Value }

    foreach ($URL in $getURL)
    {
        $Request = [System.Net.WebRequest]::Create($URL)
        $Response = $Request.GetResponse()
        $Status = [int]$Response.StatusCode
        if ($Status -ne 200 -or $Response.ResponseUri -like '*PageNotFound*') 
        { 
            $x = [PSCustomObject] @{
                File = $file.Name
                URL  = $URL
            } ; $array += $x
        }
        $Response.Close()
    }
}

但是$array的输出是这样的:

@{File=B-1.56.aspx; URL=http://sdfsdfsdf/b-1.39.4_fr1_d.pdf}
@{File=B-1.56.aspx; URL=http://sdfsdfsdfssd/b-1.39.4_fr1_d.pdf}
@{File=B-1.58.aspx; URL=https://sdfffssd/b-1.39.6_de_d.pdf}
@{File=B-1.58.aspx; URL=https://fsdfsfb-1.39.6_de_d.pdf}

如何将此格式设置为列表,以便我可以抓取FileURL属性将其作为电子邮件正文发送给您?:

$body = $array | sort File | ConvertTo-Html -Property File,URL -Head "<html><h2>Folgende Links wurden im Katalog nicht gefunden:</h2><br></html>" | Out-String

1 个答案:

答案 0 :(得分:1)

看起来这仍然只是脚本的一部分,但是:

  • 我将您的anded通配符转换为带有OR&#39; ed |条目的-notmatch RegEx
  • 并设置$array = Foreach($file ...收集所有输出
$REDir = '.*Werkstatt.*|.*- Kopie.*|.*\.[012].*'
$REURL = '.*www\.w3\.org.*|.*jquery\.com.*|.*www.mwe.*'

$AspxFiles = Get-ChildItem $path $filter -r | 
    Where-Object { $_.DirectoryName -notmatch $REDir  -and `
                            $_.Name -notlike  '*- Kopie*'}
$array = @()

$array = foreach ($file in $AspxFiles) {
    $getURL = Select-String -Path $file.FullName -Pattern $regex -AllMatches | 
        Where-Object { $_ -notmatch $REURL } | 
           ForEach-Object { $_.Matches } | ForEach-Obkect { $_.Value }

    ForEach ($URL in $getURL) {
        $Request = [System.Net.WebRequest]::Create($URL)
        $Response = $Request.GetResponse()
        $Status = [int]$Response.StatusCode
        if ($Status -ne 200 -or $Response.ResponseUri -like '*PageNotFound*') { 
            [PSCustomObject] @{
                File = $file.Name
                URL  = $URL
            }
        }
        $Response.Close()
    }
}

$body = $array | Sort-Object File | 
    ConvertTo-Html -Property File,URL -Head `
      "<html><h2>Folgende Links wurden im Katalog nicht gefunden:</h2><br></html>" | 
        Out-String