Important Notice: On February 29th, this community was put into read-only mode. All existing posts will remain but customers are unable to add new posts or comment on existing. Please feel to join our Community Discord for any questions and discussions.

Powershell step gets different results in package than PS console

Hi All, I have a PoSh script that, when I run it in the PoSh ISE Console, I get my desired results, but when I run it in a Deploy custom package step, I do not get the desired results.

A bit of background on the package and script. I created the package to create and send me a daily report on all of the backups taking place across my systems. All of the steps are PoSh scripts. There is one step for each system that it checks. The script checks for the latest N files in a shared directory and puts the results into a csv file. The final step takes the results of all of the csv files and assembles them into an HTML email that it sends to me.

One particular system that I am checking does a full backup each Saturday and incremental backups all other days. So, I just want to see the latest full backup file (.abc) and each incremental backup file (.123) since the latest full backup file. All backup files are kept for 10 days.

Here is the script in question:

$backupdir = "\\server\share\directory\"
$reportfile_full = "C:\Temp\backupreport\productname_full.csv"
$lastfiles_full = 1
$reportfile_incr = "C:\Temp\backupreport\productname_incr.csv"
$lastfiles_incr = 6
 
function Get-FriendlySize {
param($Bytes)
$sizes='Bytes,KB,MB,GB,TB,PB,EB,ZB' -split ','
for($i=0; ($Bytes -ge 1kb) -and 
    ($i -lt $sizes.Count); $i++) {$Bytes/=1kb}
$N=2; if($i -eq 0) {$N=0}
"{0:N$($N)} {1}" -f $Bytes, $sizes[$i] }
 
gci $backupdir -filter *.abc | sort LastWriteTime | select Directory,LastWriteTime,Name,@{Name="Size";Expression={Get-FriendlySize -Bytes $_.length/1MB}} -last $lastfiles_full | export-csv $reportfile_full -notypeinformation | foreach ($file) {$lastfullwrite = $_.LastWriteTime}
gci $backupdir -filter *.123 | Where-Object {$_.LastWriteTime -gt $lastfullwrite} | sort LastWriteTime | select Directory,LastWriteTime,Name,@{Name="Size";Expression={Get-FriendlySize -Bytes $_.length/1MB}} -last $lastfiles_incr | export-csv $reportfile_incr -notypeinformation

When I run this script from the PoSh ISE console, I get exactly what I want: the latest full backup file, and only the incremental backup files since that full backup. When I run it as part of the package, I get the latest full backup file, but then I get the last 6 incremental backup files, including ones that came before the latest full backup file. It's certainly not the end of the world, because the results include what I need to see. They just also include extra stuff to make an already long report even longer.

You may be thinking that there is a permissions difference, and there is. The package runs as a domain admin account and the PoSh console does not. So, I get the results I want when I run with RO access to the directory, but not when I have Full Access. When I run the package, I target my desktop, so it is the same machine that I run the PoSh ISE on. I have tried running the package with my user credentials, and running as the local logged on user (me), but still the same results.

My guess is that, for some reason that I am not aware, the foreach ($file) {$lastfullwrite = $_.LastWriteTime} at the end of the first gci line does not process in the package run the same way it does in the ISE, but I don't know how to prove or fix that.

Incidentally, the Get-FriendlySize function was a cool little bit that I found here.

I hope someone can help!

0

Comments

0 comments