Powershell v3-5: Out-Grid View Truncated Data

与世无争的帅哥 提交于 2020-01-06 04:37:13

问题


I'm working on a PS script (v5 on the machine I'm using) that uses Invoke-WebRequest to grab information from a web address and returns the results.

When attempting to pipe my output to Out-GridView with more than 9 results, the column containing data lists "..." on the 10th line.

I've tried doing several types of joins, and am just wondering if I need to have my result in a specific type to avoid having this effect (hashtable maybe?)

Checking MS forums has only yielded results about joining on line-end's, which doesn't seem to help in this case.

The pages I'm querying are simple HTML showing the output of .txt files, however the .Content property of my Invoke-WebRequest query seems to be one long string.

Here's my code thus far:

[cmdletBinding(
    DefaultParameterSetName='FileWithURIs'
)]
Param(
    [Parameter(ParameterSetName='FileWithURIs',
               Mandatory=$true
    )]
    [ValidateNotNull()]
    [ValidateNotNullOrEmpty()]
    [ValidateScript({
            if(-Not ($_ | Test-Path) ){
                throw "File or folder does not exist"
            }
            if(-Not ($_ | Test-Path -PathType Leaf) ){
                throw "The Path argument must be a file. Folder paths are not allowed."
            }
            if($_ -notmatch "(\.txt)"){
                throw "The file specified in the path argument must be of type txt"
            }
            return $true 
        })]
    [String]$FileWithURIs,

    [Parameter(ParameterSetName='SingleURI',
               Mandatory=$True)]
    [ValidateNotNull()]
    [ValidateNotNullOrEmpty()]
    [ValidateScript({
        if($_.StartsWith("http://") -eq $false -and $_.StartsWith("https://" -eq $false))
        {
            throw "User specified URI must start with http:// or https://"
        }
        else
        {
            return $true
        }
    })]
    [String]$URI,
    [Switch]$ViewAsGrid
)

BEGIN
{
    Function Check-CustomType()
    {
        if("TrustAllCertsPolicy" -as [type])
        {
            Out-Null
        }
        else
        {
            Set-CustomType
        }
    }

    Function Set-CustomType()
    {

add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
    public bool CheckValidationResult(
        ServicePoint srvPoint, X509Certificate certificate,
        WebRequest request, int certificateProblem) {
        return true;
    }
}
"@
        $script:newCertPolicy = [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
    }

    Function Evaluate-URIs()
    {
        if($URI)
        {
            Get-Blocklist -ListURI $URI
        }
        elseif($FileWithURIs)
        {
            $lines = Get-Content $FileWithURIs
            foreach($line in $lines)
            {
                Get-Blocklist -ListURI $line
            }
        }
    }

    Function Create-Table()
    {
        $script:tabName = "ResultTable"

        # Create Table object
        $script:table = New-Object System.Data.DataTable “$script:tabName”

        # Create first column
        $script:col1 = New-Object System.Data.DataColumn SiteName,([string])
        $script:table.columns.add($script:col1)
    }

    Function Add-RowToTable($Value)
    {
        # Create new row
        $newRow = $script:table.NewRow()

        # Add value to row
        $newRow.SiteName = $Value

        # Add row to table
        $script:table.Rows.Add($newRow)
    }

    Function Get-Blocklist($ListURI)
    {
        try
        {
            $query = Invoke-WebRequest -Uri "$ListURI"

            if($ViewAsGrid)
            {
                Create-Table
                $content = @($query.Content.Split("`r`n"))

                foreach($entry in $content)
                {
                    Add-RowToTable -Value $entry
                }

                $script:table | Out-GridView -Title "Blocklist for $ListURI"
            }
            else
            {
                Write-Host "`nBlocklist for $ListURI " -ForegroundColor Yellow -NoNewline
                Write-Host "`n`n$($query.Content | Sort -Descending)"
            }  
        }
        catch [Exception]
        {
           Write-Host "`nUnable to connect to resource " -ForegroundColor Yellow -NoNewline
           Write-Host "$ListURI" -ForegroundColor Red
           Write-Host "`nERROR: $($Error[0].Exception)"
        }
    }

    Function Run-Stack()
    {
        Check-CustomType
        Evaluate-URIs
    }
}

PROCESS
{
    Run-Stack
}

END { Write-Host "`nEnd of script" }

The idea is to only use Out-GridView if a single, user-entered address is the input. Otherwise it just becomes console output.

Any help would be appreciated!

来源:https://stackoverflow.com/questions/46694015/powershell-v3-5-out-grid-view-truncated-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!