Output files from scripts can be useful for assessments, tracking changes, documenting settings and more. These files may be named in such a way that it is understandable to you, but may not be understandable to another person. If a script produces a list of output files that may get confusing. What can you do to mitigate issues like this?
A manifest file might just be the solution.
A manifest is typically used for shipping lists and general lists of what is contained either in a shipment or delivery. We can use this concept to create a file that lists all files produced by the script as well as a good, brief explanation of what the file contains. for our example we will take a look at a script that is used in an assessment and creates a list of files.
File Name and Variable Storage
First place to start if to create a file name and path to store the manifest file. In this example we will use a filename prefix of ‘_FileManifest’ and we will also add a date to the name of the file to keep track of when the script / assessment was performed:
[sourcecode language=”powershell”]
# Create Manifest File for all files created in a run
$Date = Get-Date -Format "MM.dd.yyyy-hh.mm-tt"
$Path = (Get-Item -Path ".\" -Verbose).FullName
$ManifestFile = "_FileManifest-$Date.txt"
$ManifestDestination = $Path+"\"+$ManifestFile
[/sourcecode]
Now that we have a manifest file set up and ready for input, let’s walk through how we can add the file list to the Manifest file. One key here, though we can predefined all possible files that could be created, we should not populate the Manifest until AFTER the file is created. That might seem obvious, but it takes a bit more work. For example, in the function below, we process some steps, store the information in an output file, and then when the function is done processing, then we populate the Manifest file with the name and purpose of the output file:
Theoretical layout:
[sourcecode language=”powershell”]
Function Function1 {
< Function Steps – output goes to $OutputFile1>
$Line = "$OutputFile1 – Tasks performed by Function1." | Out-file $ManifestDestination
}
[/sourcecode]
Real Function – Queries Mail Contacts (Exchange or Exchange Online):
[sourcecode language=”powershell”]
Function Contacts {
Write-host 'Reporting on Contacts' -ForegroundColor Yellow
$MailContacts = Get-MailContact -Resultsize Unlimited -ErrorAction STOP
$ContactCount = $MailContacts.Count
If ($Count -gt $Null) {
$Line = "Mail Contacts Found – $ContactCount" | Out-File $Destination -Append
$Line = Get-MailContact -ResultSize Unlimited | Ft DisplayName, EmailAddress | Out-File $MailContactsDestination
}
$Line = "$MailContactFile – List of all Mail contacts in Exchange." | Out-file $ManifestDestination -Append
}
[/sourcecode]
As we can see from the code above, the function runs it’s query, exports it’s results to a failed name stored in the $MailContactDestination variable and then the name of that file is stored in the Manifest. We can also add a descriptive header to the Manifest file as well:
[sourcecode language=”powershell”]
$Line = "——————————————–" | Out-File $ManifestDestination
$Line = "List of files created:" | Out-file $ManifestDestination -Append
$Line = "——————————————–" | Out-file $ManifestDestination -Append
[/sourcecode]
A sample manifest file is show below:
Now, with this code we can hand the script off to another person and they can run it and understand what the output produced are used for.