Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Functions within start-job scriptblock

$
0
0

I'm working on a process to archive (7zip) eventlogs that are archived to a network share.  I'm doing this because we are required to keep a year's worth of logs and as we upgrade systems to 2012, the amount of data increases.

For instance, a year ago, our domain controllers generated 5-6 80MB security eventlogs each day.  Now, there are over 40 192MB files for each.  We've already had to expand our SAN storage - eventlogs alone take almost 5TB.

I digress....

To start with, I create a queue:

$queue=[System.Collections.Queue]::Synchronized( (New-Object System.Collections.Queue) )

Then add to it:

gci \\Server\EventArchive -include archive-security* -recurse|?{$_.lastwritetime -le (get-date).adddays(-305)|group-object {$_.directory},{$_.lastwritetime.year},{$_.lastwritetime.month},{$_.lastwritetime.day}

 

The 'where' clause is just to limit the number of files to process while I'm working on it.

My script - that works if I run it as ".\archive-logs.ps1 $queue.dequeue()" is:

Param($item)

$folder=$item.values[0].fullname

$server=$item.values[0].name

$year=$item.values[1].tostring()

$month=$item.values[2].tostring().padleft(2).replace(" ","0")

$day=$item.values[3].tostring().padleft(2).replace(" ","0")

$files=@($item.group.fullname)

c:\7zip\7z.exe a -stl -mx9 -sdel -ms=off "$($folder)\$($server)_Security_$($year)$($month)$($day).7z" @files

 

If I run this in a background job, it seems that it loses where the files are.  In the job results, I get access denied errors point to "My Music" and other folders under my profile.

Where might this be reverting to looking in my profile?  I've tried using:

Start-job -filepath c:\powershell\archive-logs.ps1 -argumentlist $queue.dequeue()

Start-job -scriptblock {[code from script]} -argumentlist $queue.dequeue()

Start-Job -initializationscript {$variable containing code from script} -scriptblock {$variable} -argumentlist......

 

Results are all the same.  It's obviously a scoping problem, but I'm having trouble narrowing it down.  For what it's worth, when defined as a function I use function global:archive-logs.....

 

 


Viewing all articles
Browse latest Browse all 6937

Trending Articles