Friday, May 11, 2018

How To List Files and Display In HTML Format


Overview

In this guide we will demonstrate as to how to list files on a server, store and export list to HTML file and launch the html file.

Applies To

Tested on Windows 10, Windows 2008 R2, Windows 2012.

Pre-Requisites

To run this script, Execution Policy should be set to either of these “AllSigned” or “RemoteSigned” or “Unrestricted”, you can get current execution policy by running the command; “Get-ExecutionPolicy”.

Policy Purpose
Restricted No scripts can be run. Windows PowerShell can be used only in interactive mode.
AllSigned Only scripts signed by a trusted publisher can be run.
RemoteSigned Downloaded scripts must be signed by a trusted publisher before they can be run.
Unrestricted No restrictions; all Windows PowerShell scripts can be run.

Current Execution Policy

To know the current run the PowerShell cmdlet; Get-ExecutionPolicy

To list execution policies that can be configured run the PowerShell cmdlet; Get-ExecutionPolicy -List



PowerShell Script – List File

In this PowerShell script, we will validate file exists in the folder and if a files are not found, a message box will be displayed.

PowerShell Snippet

The code snippet is for demonstrating “Specific Type of Files Listing in a directory”.

#
# Define HTML Styling
#
$Style = "
<style>
    TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
    TH{border-width: 2px;padding: 5px;border-style: solid;border-color: green;}
    TD{border-width: 2px;padding: 5px;border-style: solid;border-color: black;}
</style> "
#
Clear-Host
[console]::Beep(500,600)
$FilePath=Read-Host ("Enter the Path ")
$FileExtType=Read-Host ("Enter the File Type / Extension ")
if ((Test-Path -Path $FilePath)) {
    $OutputFileName="Files_List.html"
    Set-Location $FilePath
    Push-Location $FilePath
    #
    # Store File list into a variable for HTML formating
    #
    $FileListingValidate=(Get-ChildItem "$FilePath" | where {$_.Extension -eq "$FileExtType"}).count
    if ( $FileListingValidate -eq 0) {
        [console]::Beep(500,600)
        [System.Windows.MessageBox]::Show($Filepath + "\*" + $FileExtType + ' - No Files Found',"File Listing")
        return
    } 
        $FileListing=Get-ChildItem "$FilePath" | where {$_.Extension -eq "$FileExtType"} | Select-Object DirectoryName, Name, Length,mode | ConvertTo-Html -Fragment 
        #
        # Add header, Body as File Listing
        #
        ConvertTo-Html -Head $Style -Body $FileListing | Out-File $FilePath\$OutputFileName
        Invoke-Item $FilePath\$OutputFilename
    } else {
        # Popup Message Box - Invalid Directory
        [console]::Beep(500,600)
        [System.Windows.MessageBox]::Show($Filepath + ' - Invalid Directory',"Invalid Directory")
        return
    }

PowerShell Execution – List Files

When script is executed; user is requested to key-in directory and file type that has to be listed, as shown below.


PowerShell Output – List Files

When script is executed; when the files are found, list will be stored into HTML file and launch it.

PowerShell Output – No Files Found

When script is executed, if the files are not found, a message box will be displayed, as shown below;



SlideShare Information

A step by step guide with screenshot has been uploaded to slideshare.




No comments:

Post a Comment