1

I have a search script to find files in some network / local locations. The full script works fine and returns the right results. I'm just cleaning up the html output result file so it can be read easily by other people.

Please Note: the $Paths = Get-Content H:\location.txt returns two values h:\test and c:\test

In my PowerShell script, when i pass the $paths variable to my HTML output i get the correct information but it is displayed in a single line i.e. h:\test c:\test however

i'd like the output to be formatted more like the below on screen:

h:\test
c:\test

$SubFolders = $true
$ReportPath = "h:\"
$Paths = Get-Content H:\location.txt
$text = ('*md5*','*build*')

foreach ($path in $paths) {

If ($SubFolders)
{   $SubFoldersText = "(including sub-folders)"
}
Else
{   $SubFoldersText = "(does <i>not</i> include sub-folders)"
}

$Header = @"

<style type='text/css'>
body { background-color:#DCDCDC;
}
table { border:1px solid gray;
  font:normal 12px verdana, arial, helvetica, sans-serif;
  border-collapse: collapse;
  padding-left:30px;
  padding-right:30px;
}
th { color:black;
  text-align:left;
  border: 1px solid black;
  font:normal 16px verdana, arial, helvetica, sans-serif;
  font-weight:bold;
  background-color: #FF0000;
  padding-left:6px;
  padding-right:6px;
}
td { border: 1px solid black;
  padding-left:6px;
  padding-right:6px;
}
</style>
<center>
<h1>Software Audit</h1>
<h2>Locations that are being searched: <font color="red"> <br/> $paths </font> 
<br/> Searching for files that contain the words: <font color="red"> <br/> $text </font> <br/> 
$SubFoldersText</h2>
<br>
"@

If ($FileExtension -eq "*")
{   $GCIProperties = @{
    Path = $Paths
    Recurse = $SubFolders
}
}
Else
{   $GCIProperties = @{
    Path = $Paths
    Include = $text
    Recurse = $SubFolders

}
}

$Report = @()
Foreach ($File in (Get-ChildItem @GCIProperties | Where { $_.PSisContainer -    eq $false }))
{
$Report += New-Object PSObject -Property @{
    Path = $File.FullName
    Owner = (Get-Acl $File.FullName).Owner
    'Created on' = $File.CreationTime
    'Last Write Time' = $File.LastWriteTime
    Size = "{0:N2} MB" -f ( $File.Length / 1mb )
}
}

}

$Report | Select Path,Size,'Created on','Last Write Time',Owner | Sort Path     | ConvertTo-Html -Head $Header | Out-File $ReportPath\Get-FileOwner.html

Invoke-Item $ReportPath\Get-FileOwner.html

Thanks for your help,

1
  • 1
    You just want to format the array $paths on separate lines? You should be able to use $($paths -join '<br/>') inside your string Commented Mar 4, 2016 at 0:54

2 Answers 2

2

Change $paths to $($paths -join '<br/>') when formatting to HTML.

<h2>Locations that are being searched: <font color="red"> <br/> $($paths -join '<br/>') </font>

The $() syntax inside a string allows you to put any PowerShell expression. In this case, a string join.

Sign up to request clarification or add additional context in comments.

Comments

1

Use $paths -join "<br/>" after you Get-Content.

$paths = Get-Content H:\location.txt
$paths = $paths -join "<br/>"
...

4 Comments

No good to wipe out the variable; it gets used as-is later. Instead, change the formatting where it's converted into HTML. <h2>Locations that are being searched: <font color="red"> <br/> $($paths -join '<br/>') </font>
I based my answer off Edit 1 which I was looking at when you posted your comment about 9 seconds before I hit Submit :) But yes doing this inline like you suggest is probably the right thing in the OPs code, or like mine in a general sense...depends on use.
@RyanBemrose your solution worked perfectly just what i wanted, cheers.
Thanks all for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.