Working with Alternative Data Streams in Power Shell

Close to five years ago I made a post here on working with Alternative Data Streams on Windows with .Net. Today (within the last hour) I had to come up with a quick solution for which I used an ADS solution. I’m on a small break and am making this post hurriedly. A CMS was going to deposit some files onto a machine, including a zip file that contained a collection of assets. If the file were newer than the last set of assets, I needed to unzip them to some pre-decided destination. For this CMS, when a file version changes, the file name also changes (the file’s hash is a part of its name). This lets us use very simple logic. If the current name is not the same as the last name, unarchive the file somewhere.

Keeping track of the most recent file name is just a matter of writing some data to a file. I could have written to a file of any name. But I prefer to write such things in a way that lowers a user’s propensity to touch it. I decided to write it to an alternative data stream named version.info. This script has room for improvement. But for now we were working for a “good enough” situation until we can refine things more. This is the script that I used.

$source =$args[0]
$destination=$args[1]
$destinationMeta = "$destination`:version.info"
$fileName = Split-Path $source -Leaf
write-host $fileName
write "Destination: $destination"
write-host $destinationMeta
$contents = Get-Content -Path "$destinationMeta"
write-host "Contents: $contents"
write-host "Last version stamp is $lastStamp"
if($contents -ne $fileName) {
Write-Host "This is a new file name. Applying.";
Expand-Archive -Path $source -Destination $destination -Force
Write-Host "Executing [ Set-Content -Path $destinationMeta -Value ""$fileName""]"
Set-Content -Path "$destinationMeta" -Value "$fileName"
$x = Get-Content -Path "$destinationMeta"
Write-host $x
}

Let’s say the script is called unpackage.ps1. When invoked, the first argument is the path to the zip archive and the second the path to the destination folder (without a trailing backslash). If you run it with the same arguments multiple times, it will only unzip the archive once. For now, this means we could invoke the script daily with the CMS’s daily refreshes and it will only take action on the file system when there is an update.


Posts may contain products with affiliate links. When you make purchases using these links, we receive a small commission at no extra cost to you. Thank you for your support.

Mastodon: @j2inet@masto.ai
Instagram: @j2inet
Facebook: @j2inet
YouTube: @j2inet
Telegram: j2inet
Twitter: @j2inet

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.