SimonS
SimonS

Reputation: 1973

Format Array inside array to later use as an E-Mail body

I create an empty $array early in my script and later i loop over some stuff and want to add this stuff to my array. This works as intended.

$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()
    }
}

But the output of $array is like this:

@{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}

How can I get this formatted like a list so I can grab the File and URL Property to send it as an E-Mail body like this?:

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

Upvotes: 0

Views: 73

Answers (1)

user6811411
user6811411

Reputation:

Looks like this is still only a part of your script but:

  • I'd transform your anded wildcards into a -notmatch RegEx with OR'ed | entries
  • and set $array = Foreach($file ... gathering all ouput

$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

Upvotes: 1

Related Questions