PowerShell v1 strange behaviour

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Nick Chard Nick Chard 3 years ago.

  • Author
    Posts
  • #14124
    Profile photo of Nick Chard
    Nick Chard
    Participant

    Hi

    I have written a script to automatically zip log files. It works well and does exactly what I need it to, but I've noticed some strange behaviour on PS v1. The script is below. The aim of the script is to look at all the sub folders of D:\Logs. Any folders that are over 500Mb, the files between 30 – 90 days in the folder will be zipped into a subfolder called archive, into a file called archiveDate.zip. It uses 7za.exe to zip.

    The problem is this. I have created a scheduled task to run this script. When it runs from the scheduled task, it creates folder C:\archive which contains a zipped file called archiveDate.zip. The files that have been zipped are the contents of C:\WINDOWS\system32\windowspowershell\v1.0

    This only happens with PowerShell v1 – can anyone explain how this is happening?

    The scheduled task command to run the script is "Powershell -command D:\scripts\Archive-FilesPSv1.ps1 -MyPath D:\Logs -SizeLimit 250 -UpperLimit 31 -LowerLimit 15"

    Script:

    param
    (
    [string]$MyPath = "D:\Logs",
    [int]$SizeLimit = "500",
    [int]$UpperLimit = "90",
    [int]$LowerLimit = "30"
    )

    [datetime]$90days=(get-date).AddDays(-$UpperLimit)
    [datetime]$30days=(get-date).AddDays(-$LowerLimit)
    [string]$date=(get-date -format ddMMyy)

    $Collection = (gci $MyPath | where {$_.PSISContainer})

    foreach ($item in $Collection)
    {
    $SubFolders = gci $item.fullname -recurse | where { !$_.PSISContainer } | Measure-Object -property length -sum
    [int]$size = "{0:N0}” -f ($SubFolders.sum / 1Mb)

    If ( $Size -gt $SizeLimit )
    {
    $ZipPath = $item.fullname
    $ToZip = gci $ZipPath -Recurse -Exclude "*.zip" | where { !$_.PSISContainer -and $_.CreationTime -gt $90days -and $_.CreationTime -lt $30days}

    foreach ($file in $ToZip)
    {
    $directory = $file.DirectoryName
    $ZipFIle = $file.FullName

    D:\Tools\7za\7za.exe a "$directory\archive\archive$date.zip" "$ZipFile"
    Remove-Item $ZipFIle
    }

    }

    }

  • #14132
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Honestly, I'm not sure, but I wouldn't recommend running PowerShell v1 anymore. You can deploy PowerShell 2.0 on any system that 1.0 supported, as far as I know (and PowerShell 3.0 / 4.0 on more current OS platforms.) Aside from all of the enhancements you'd get by upgrading, there are also plenty of bug fixes.

  • #14146
    Profile photo of Richard Siddaway
    Richard Siddaway
    Moderator

    There's nothing obvious I can see but I suspect that you've tripped over some change between versions. What version of PowerShell are you using on the systems that work?

    Looking at your script there's a few things I'd point out:

    – Don't use aliases in the script. use Get-ChildItem instead of gci etc
    – your default values – you have a type of int but are giving the value as a string! You don't need the quotes.

  • #14220
    Profile photo of Nick Chard
    Nick Chard
    Participant

    Thanks for the tips on the script, I've made the changes you suggested. It works on Powershell v2 & 3. This is not the first time that v1 has done something unexpected when running a script so I have decided I will not run this script on anything that uses v1. I've published PowerShell updates through our WSUS server so will make the effort to get Powershell up to date.

You must be logged in to reply to this topic.