Author Posts

January 24, 2018 at 4:04 am

Hi,

I have a script that takes some input from a csv and creates a HTML. The issue i have is it runs as expected from ISE ,but when the script is run directly i.e. right click and run in powershell, the output in the HTML is truncated. I'm not sure why this is happening.

Any help would be appreciated.

$a = ""
$a = $a + "FONT{color: white;}"
$a = $a + "BODY{background-color:#07c655;}"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$a = $a + "TH{border-width: 1px;padding: 5px;border-style: solid;border-color: black;background-color:#07c655}"
$a = $a + "TD{border-width: 1px;padding: 5px;border-style: solid;border-color: black;background-color:#e0e0e0}"
$a = $a + ""

Import-Module ActiveDirectory
$FormatEnumerationLimit=-1
Get-ADGroup -Searchbase "OU=xxx,OU=xxx,DC=xxx,DC=xxxx" -filter * -Properties member | Select-Object -Property @{label='Member';expression={$_.Member -replace '^CN=|,.*$'}}, Samaccountname | Select SamAccountNAme,Member | ft -AutoSize -HideTableHeaders |out-file C:\temp\test.txt -width 20000
$test = Get-Content C:\Temp\test.txt
$test |
foreach-object { $_ -replace ",",";" -replace "{","" -replace "}",""-replace "  +","`t"} | ft -AutoSize |
Out-string | out-file C:\temp\test2.txt -Width 10000
Import-Csv C:\temp\test2.txt -Header "Group","Members" -Delimiter "`t" | Select Group,Members | sort Group,@{expression="Members";Descending=$True} | Convertto-html -head $a -as List |out-file  c:\temp\groups.html

January 25, 2018 at 7:47 am

You don't show the error you are getting.
Also the code as posted, though it will create the file, they way the final HTML is coded, really does not render well or at all.
Your style-sheet is really not right at all.
Change to this...

$a = @'

FONT{color: white;}
BODY{background-color:#07c655;}
TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
TH{border-width: 1px;padding: 5px;border-style: solid;border-color: black;background-color:#07c655}
TD{border-width: 1px;padding: 5px;border-style: solid;border-color: black;background-color:#e0e0e0}

'@

Quickly made some change to you code to test on my system and the below works.

$a = @'
FONT{color: white;}
BODY{background-color:#07c655;}
TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
TH{border-width: 1px;padding: 5px;border-style: solid;border-color: black;background-color:#07c655}
TD{border-width: 1px;padding: 5px;border-style: solid;border-color: black;background-color:#e0e0e0}

'@
Import-Module ActiveDirectory
$FormatEnumerationLimit=-1

Get-ADGroup -Searchbase "$($(Get-ADOrganizationalUnit -Filter *).DistinguishedName -match 'users')" -filter * -Properties member `
| Select-Object -Property @{label='Member';expression={$_.Member -replace '^CN=|,.*$'}}, Samaccountname `
| Select SamAccountNAme,Member `
| ft -AutoSize -HideTableHeaders `
| out-file 'C:\temp\test.txt' -width 20000

$test = Get-Content 'C:\Temp\test.txt'

$test |
foreach-object { $_ -replace ",",";" -replace "{","" -replace "}",""-replace "  +","`t"} `
| ft -AutoSize `
| Out-string `
| out-file 'C:\temp\test2.txt' -Width 10000

Import-Csv 'C:\temp\test2.txt' -Header "Group","Members" -Delimiter "`t" `
| Select Group,Members `
| sort Group,@{expression="Members";Descending=$True} `
| Convertto-html -head $a -as List `
| out-file  'c:\temp\groups.html'

Start-Process -FilePath 'c:\temp\groups.html'

Though I still say you have more work, because though this display, the layout is not really pleasing well a bit of code tidying up. Say, something like this...

    Import-Module ActiveDirectory

    $a = @"
    BODY{background-color:White;}
    TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
    TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;}
    TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;}
    "@

    $body = @()

    $TargetBase = $(Get-ADOrganizationalUnit -Filter *).DistinguishedName -match 'users'
 
    ForEach ($Group in (Get-ADGroup -SearchBase "$TargetBase" -Filter *))  
    { 
        $Outfile = Get-ADGroupMember $Group | select @{Label="Group";Expression={$Group.Name}},Name
        [array]$body += $Outfile
    } 

    ForEach ($Group in (Get-ADGroup -SearchBase "$TargetBase" -Filter *))  
    { 
        $Outfile = Get-ADGroup $Group -Properties Members | where {-not $_.members} | select @{Label="Group";Expression={$Group.Name}}
        [array]$body += $Outfile
    } 

    ($body = $body | ConvertTo-Html -head $a -body "Corporate OU Groups and Membership") `
    | Out-File -FilePath "$env:USERPROFILE\Desktop\Ou_Report.html" -Force

    Start-Process -FilePath iexplore "$env:USERPROFILE\Desktop\Ou_Report.html"

January 30, 2018 at 6:14 am

Thanks for your reply.

Sorry I didn't post an example of the error I was getting, the resulting HTML has sensitive Company data in it. basically some of the groups have a very large membership, running the script.ps1 produces an HTML where the large groups are truncated . The code when run from ISE produces a HTML where all the results are visible. Your Code doesn't have the same issue.I would like to understand why it does that.