Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Script misbehaving when scheduled

$
0
0

hi all,

so i created the bottom half of the script. when run in interactive mode its fine.  it groups the files in one zip  file where the last modified date is in the same day. my problem is when i schedule it on sql server agent and run it, it only stores 1 file in the zip and the rest of the files are deleted for that day.

Any help would be appreciated!! Thanks!

Param(
  [string]$filePathSrc,
  [string]$fileType,
  [string]$datePart
)

#### 7 zip variable I got it from the below link 
#### http://mats.gardstad.se/matscodemix/2009/02/05/calling-7-zip-from-powershell/ 
# Alias for 7-zip
if (-not (test-path "$env:ProgramFiles\7-Zip\7z.exe")) {throw "$env:ProgramFiles\7-Zip\7z.exe needed"}
set-alias sz "$env:ProgramFiles\7-Zip\7z.exe"

# $filePathSrc = "C:\Script\IceFile"
# $datePart = "yyyyMMdd"
# $fileType = "csv"

foreach ($item in Get-ChildItem $filePathSrc | Where-Object {$_.Extension -eq ".$fileType"})
{
    $zipfile = $filePathSrc + "\" + $item.LastWriteTime.toString($datePart) + ".zip"
if (Test-Path $zipfile)
{
# Add file into existing zip
   sz u -tzip "$zipfile" "$filePathSrc\$item"
# Remove the file after adding it to the zip file
Remove-Item "$filePathSrc\$item"
}
else
{
# Create zip and add file
   sz a -tzip "$zipfile" "$filePathSrc\$item"
# Remove the file after adding it to the zip file
Remove-Item "$filePathSrc\$item"
}




Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>