Fwd: Powershell query

Hi everyone,

I'm hoping someone can help me.

I have a scheduled task that will run every 15 minutes and delete files that are older than 2 minutes.

This is the code I use:-

pass_day = Time.now.strftime("%Y-%m-%d")
$passdaydir = 'Z:\s3_temp\' + "#{pass_day}"
windows_task 'deletion-of-logs-every-fifteen-minutes' do
user 'Administrator'
cwd 'Z:\s3_temp\' + "#{pass_day}"
command 'powershell.exe Get-ChildItem $passdaydir | Where-Object {$_.LastwriteTime -lt (Get-Date).addMinutes(-2) } | Remove-Item -Recurse -Force'
run_level :highest
frequency :minute
frequency_modifier 15
force true
action :create
end

I'm trying to pass a variable $passdaydir into this command: 'powershell.exe Get-ChildItem $passdaydir | Where-Object {$_.LastwriteTime -lt (Get-Date).addMinutes(-2) } | Remove-Item -Recurse -Force' however when this runs, it doesn't complete successfully (i.e. the files in Z:\s3_temp\2015-08-10) do not get deleted.

How would you pass a variable into this command?

Many thanks

Sent from iCloud

Begin forwarded message:

From: ANGELA EBIRIM aebirim@icloud.com
Date: July 28, 2015 10:47:44 PM
To: chef@lists.opscode.com
Subject: Re: [chef] Powershell query

Hi Matt,

Will use that.

Thanks!
Sent from iCloud

On Jul 28, 2015, at 06:19 AM, Matthew Moretti werebus@gmail.com wrote:

Scheduled tasks are certainly the direction you want to go. The Windows cookbook has the windows_task resource. Underneath the hood it relies on schtask, but abstracts a good deal of the complexity away under a resource. Hope that gets you pointed in the right direction.

Matt Moretti

On Tue, Jul 28, 2015 at 4:27 AM, ANGELA EBIRIM aebirim@icloud.com wrote:

Hi everyone,

I'm hoping someone could help me with this as I'm stuck!

I have a powershell command: powershell.exe Get-ChildItem C:\Users\ae\AppData\Local\Temp\3\logstash | Where-Object {_.LastwriteTime -lt (Get-Date).addhours(-12} | Remove-Item -Recurse -Force

which essentially deletes folders within the logstash directory that are older than 12 hours. This needs to be done at a certain time each day.

This command will be included in a Chef recipe and deployed to a Windows Server box.

I'm having difficulty getting this to run at a certain time and was wondering if anyone knew of a way to do this. I've been using the SCHTASKS windows function but this works on a command line but not within a script.

Is anyone aware of a Chef resource that could do this?

Many thanks
 

Sent from iCloud

Not at a place to test, and neither am I a ruby expert, but i think the
initial problem is that the command is inside single-quotes, which in many
cases means "do not interpolate anything inside the single quotes, just
treat it as characters, )ie, don't try to read a variable, and replace it"
You need ruby to first interpolate the variable $passdaydir, BEFORE
executing the powershell command. I suspect that what is actually
happening is the that its erroring out, because in the powershell session
$passdaydir" has no value (because the value was declared in the ruby
context.). Try forcing ruby to expand the value first, like this:

command "powershell.exe Get-ChildItem #{$passdaydir} | Where-Object
{$_.LastwriteTime -lt (Get-Date).addMinutes(-2) } | Remove-Item -Recurse
-Force"

Someone with Ruby syntax-foo can tell us both what is wrong with what i
suggested, but i believe the theory is correct.

On Wed, Aug 19, 2015 at 8:02 AM, ANGELA EBIRIM aebirim@icloud.com wrote:

Hi everyone,

I'm hoping someone can help me.

I have a scheduled task that will run every 15 minutes and delete files
that are older than 2 minutes.

This is the code I use:-

pass_day = Time.now.strftime("%Y-%m-%d")
$passdaydir = 'Z:\s3_temp\' + "#{pass_day}"
windows_task 'deletion-of-logs-every-fifteen-minutes' do
user 'Administrator'
cwd 'Z:\s3_temp\' + "#{pass_day}"
command 'powershell.exe Get-ChildItem $passdaydir | Where-Object
{$_.LastwriteTime -lt (Get-Date).addMinutes(-2) } | Remove-Item -Recurse
-Force'
run_level :highest
frequency :minute
frequency_modifier 15
force true
action :create
end

I'm trying to pass a variable $passdaydir into this command:
'powershell.exe Get-ChildItem $passdaydir | Where-Object {$_.LastwriteTime
-lt (Get-Date).addMinutes(-2) } | Remove-Item -Recurse -Force' however when
this runs, it doesn't complete successfully (i.e. the files in
Z:\s3_temp\2015-08-10) do not get deleted.

How would you pass a variable into this command?

Many thanks

Sent from iCloud

Begin forwarded message:

*From: *ANGELA EBIRIM aebirim@icloud.com
*Date: *July 28, 2015 10:47:44 PM
*To: *chef@lists.opscode.com
*Subject: *Re: [chef] Powershell query

Hi Matt,

Will use that.

Thanks!

Sent from iCloud

On Jul 28, 2015, at 06:19 AM, Matthew Moretti werebus@gmail.com wrote:

Scheduled tasks are certainly the direction you want to go. The Windows
cookbook https://supermarket.chef.io/cookbooks/windows has the
windows_task resource. Underneath the hood it relies on schtask, but
abstracts a good deal of the complexity away under a resource. Hope that
gets you pointed in the right direction.

Matt Moretti

On Tue, Jul 28, 2015 at 4:27 AM, ANGELA EBIRIM aebirim@icloud.com wrote:

Hi everyone,

I'm hoping someone could help me with this as I'm stuck!

I have a powershell command: powershell.exe Get-ChildItem
C:\Users\ae\AppData\Local\Temp\3\logstash | Where-Object {_.LastwriteTime
-lt (Get-Date).addhours(-12} | Remove-Item -Recurse -Force

which essentially deletes folders within the logstash directory that are
older than 12 hours. This needs to be done at a certain time each day.

This command will be included in a Chef recipe and deployed to a Windows
Server box.

I'm having difficulty getting this to run at a certain time and was
wondering if anyone knew of a way to do this. I've been using the SCHTASKS
windows function but this works on a command line but not within a script.

Is anyone aware of a Chef resource that could do this?

Many thanks

Sent from iCloud

--

Kenneth Barry
TuneIn | Build and Release Engineer
M: 409-673-0544