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

Copy-ToZip and Start-Job

$
0
0

I've written some code but its not working as intended.

I'm using copy-toZip which comes from the Powershell Powerpack but it seems to try to compress files out of sync and gets premission issues.

function zipMeUp ($filemonth, $file, $filepath, $Fileyear){

 #Concat filename
 $ZipFileName = "u_ex" + "0" + "$filemonth" + "$fileyear" + ".zip"
 $parentpath = "C:\scripts\"
 $FullZipPath =  $parentpath + $zipfilename


copy-tozip -File $filepath -zipfile $FullZipPath

}

 

$getFiles1 = get-childitem "c:\scripts" -exclude *.zip
foreach ($file in $getfiles1){
 #Get-Item $file.lastwritetime.month
 #$file.lastwritetime.month
 if ($file.lastwritetime.month -eq 1){$filepath = $file.fullname ; $Fileyear = $file.lastwritetime.year ; $filemonth = 1 ; start-job -scriptblock {ZipMeUp $filemonth $file $filepath $Fileyear}}
  
 elseif ($file.lastwritetime.month -eq 2)
  {$filepath = $file.fullname ; $Fileyear = $file.lastwritetime.year ; $filemonth = 2 ; start-job -scriptblock {ZipMeUp $filemonth $file $filepath $Fileyear}}

 elseif ($file.lastwritetime.month -eq 3)
  {$filepath = $file.fullname ; $Fileyear = $file.lastwritetime.year ; $filemonth = 3 ; start-job -scriptblock {ZipMeUp $filemonth $file $filepath $Fileyear}}

 elseif ($file.lastwritetime.month -eq 4)
  {$filepath = $file.fullname ; $Fileyear = $file.lastwritetime.year ; $filemonth = 4 ; start-job -scriptblock {ZipMeUp $filemonth $file $filepath $Fileyear}}

 elseif ($file.lastwritetime.month -eq 5)
  {$filepath = $file.fullname ; $Fileyear = $file.lastwritetime.year ; $filemonth = 5 ; start-job -scriptblock {ZipMeUp $filemonth $file $filepath $Fileyear}}

 elseif ($file.lastwritetime.month -eq 6)
  {$filepath = $file.fullname ; $Fileyear = $file.lastwritetime.year ; $filemonth = 6 ; start-job -scriptblock {ZipMeUp $filemonth $file $filepath $Fileyear}}

 elseif ($file.lastwritetime.month -eq 7)
  {$filepath = $file.fullname ; $Fileyear = $file.lastwritetime.year ; $filemonth = 7 ; start-job {ZipMeUp $filemonth $file $filepath $Fileyear}}

 elseif ($file.lastwritetime.month -eq 8)
  {$filepath = $file.fullname ; $Fileyear = $file.lastwritetime.year ; $filemonth = 8 ; start-job -scriptblock {ZipMeUp $filemonth $file $filepath $Fileyear}}

 elseif ($file.lastwritetime.month -eq 9)
  {$filepath = $file.fullname ; $Fileyear = $file.lastwritetime.year ; $filemonth = 9 ; start-job -scriptblock {ZipMeUp $filemonth $file $filepath $Fileyear}}

 elseif ($file.lastwritetime.month -eq 10)
  {$filepath = $file.fullname ; $Fileyear = $file.lastwritetime.year ; $filemonth = 10 ; start-job -scriptblock {ZipMeUp $filemonth $file $filepath $Fileyear}}

 elseif ($file.lastwritetime.month -eq 11)
  {$filepath = $file.fullname ; $Fileyear = $file.lastwritetime.year ; $filemonth = 11 ; start-job -scriptblock {ZipMeUp $filemonth $file $filepath $Fileyear}}

 else {}
  {$filepath = $file.fullname ; $Fileyear = $file.lastwritetime.year ; $filemonth = 12 ; start-job -scriptblock {ZipMeUp $filemonth $file $filepath $Fileyear}}

 }

Without the start-job files are actually zipped but I get the issue mentioned above.  When using start-job the job is created but othing gets zipped.

Thanks


Alter De Ruine


Viewing all articles
Browse latest Browse all 21975

Trending Articles



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