• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Running in multiple comps

R

Rajeev

#1
Hi,

I'm trying to find a way to run the following script in multiple
remote computers.

Move-Item c:\test\*.log d:\test
$Size = Get-Item d:\test
if ($size.length/1MB -ge 1024)
{
$a = Get-ChildItem "d:\test"
foreach($x in $a)
{
$y = ((Get-Date) - $x.LastWriteTime).Days
if ($y -ge 10 -and $x.PsISContainer -ne $True)
{$x.Delete()}
}
}

Can anyone help please. Thanks
 
J

Joel (Jaykul) Bennett

#2
The best way would be using invoke-expression with the V2 CTP
bits ....

$cmd= @:
Move-Item c:\test\*.log d:\test
$Size = Get-Item d:\test
if ($size.length/1MB -ge 1024)
{
$a = Get-ChildItem "d:\test"
foreach($x in $a)
{
$y = ((Get-Date) - $x.LastWriteTime).Days
if ($y -ge 10 -and $x.PsISContainer -ne $True)
{$x.Delete()}
}

}
"@

invoke-expression -computername (get-content machines.txt) -command
$cmd
 
B

Brandon Shell [MVP]

#3
Use UNC paths for Computers

Something like this
foreach($comp in $computers)
{
Move-Item \\$comp\c$\test\*.log \\$comp\d$\test
$Size = Get-Item \\$comp\d$\test
if ($size.length/1MB -ge 1024)
{
$a = Get-ChildItem \\$comp\d$\test
foreach($x in $a)
{
$y = ((Get-Date) - $x.LastWriteTime).Days
if ($y -ge 10 -and $x.PsISContainer -ne $True)
{$x.Delete()}
}
}
}
Brandon Shell
---------------
Blog: http://www.bsonposh.com/
PSH Scripts Project: www.codeplex.com/psobject

R> Hi,
R>
R> I'm trying to find a way to run the following script in multiple
R> remote computers.
R>
R> Move-Item c:\test\*.log d:\test
R> $Size = Get-Item d:\test
R> if ($size.length/1MB -ge 1024)
R> {
R> $a = Get-ChildItem "d:\test"
R> foreach($x in $a)
R> {
R> $y = ((Get-Date) - $x.LastWriteTime).Days
R> if ($y -ge 10 -and $x.PsISContainer -ne $True)
R> {$x.Delete()}
R> }
R> }
R> Can anyone help please. Thanks
R>
 
M

Marco Shaw [MVP]

#4
Rajeev wrote:

> Hi,
>
> I'm trying to find a way to run the following script in multiple
> remote computers.
>
> Move-Item c:\test\*.log d:\test
> $Size = Get-Item d:\test
> if ($size.length/1MB -ge 1024)
> {
> $a = Get-ChildItem "d:\test"
> foreach($x in $a)
> {
> $y = ((Get-Date) - $x.LastWriteTime).Days
> if ($y -ge 10 -and $x.PsISContainer -ne $True)
> {$x.Delete()}
> }
> }
>
> Can anyone help please. Thanks
OK, you're looking for remoting functionality so you can run a
PowerShell command from where you're sitting, and have it run on remote
machines and return the info back to you?

There is this functionality in the v2 CTP (next version of PowerShell
currently under development), but this is not meant for a production
environment.

In version 1, there isn't a real efficient/easy way to do this.

Do you have winrm installed in your environment?

Marco

--
Microsoft MVP - Windows PowerShell
http://www.microsoft.com/mvp

PowerGadgets MVP
http://www.powergadgets.com/mvp

Blog:
http://marcoshaw.blogspot.com
 
S

Shay Levi

#5
$computers = "comp1","comp2","comp3"

$computers | foreach {
dir \\$_\c$\test\*.log | where {!$_.PsISContainer -and ($_.length -ge
1mb) -and ((Get-Date) - $_.LastWriteTime).Days -ge 10 )} | remove-item -force
-whatIf
}



-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com

> Hi,
>
> I'm trying to find a way to run the following script in multiple
> remote computers.
>
> Move-Item c:\test\*.log d:\test
> $Size = Get-Item d:\test
> if ($size.length/1MB -ge 1024)
> {
> $a = Get-ChildItem "d:\test"
> foreach($x in $a)
> {
> $y = ((Get-Date) - $x.LastWriteTime).Days
> if ($y -ge 10 -and $x.PsISContainer -ne $True)
> {$x.Delete()}
> }
> }
> Can anyone help please. Thanks
>
 
J

Joel (Jaykul) Bennett

#7
or you could use psexec to copy over the script and ...
But for this particular script, I gotta agree with Brandon
 
R

Rajeev

#8
Thanks Brandon. I had a thought about that. Still wanted to check if i
could take the names from a file. This should be fine.

One more question: how will i run this script again after say 10hrs

Thanks
 
B

Brandon Shell [MVP]

#9
schedule tasked.

Brandon Shell
---------------
Blog: http://www.bsonposh.com/
PSH Scripts Project: www.codeplex.com/psobject

R> Thanks Brandon. I had a thought about that. Still wanted to check if
R> i could take the names from a file. This should be fine.
R>
R> One more question: how will i run this script again after say 10hrs
R>
R> Thanks
R>
 
R

Rajeev

#10
Thanks Shay Levi,

How would i run the same process after 10 hrs... This should
automatically run every 10 hrs.

Thanks
 
M

Marco Shaw [MVP]

#11
R

RichS

#12
If you need to distribute the script and\or powershell to multiple computers
have a look at the Special Operations Software Command product
--
Richard Siddaway
Please note that all scripts are supplied "as is" and with no warranty
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk


"Marco Shaw [MVP]" wrote:

> Rajeev wrote:

> > Thanks Shay Levi,
> >
> > How would i run the same process after 10 hrs... This should
> > automatically run every 10 hrs.
> >
> > Thanks
>
> You may want to review:
> http://blog.sapien.com/current/2006/12/25/more-fun-with-scheduled-powershell.html
>
> Like Brandon says... Use the built-in Task Scheduler.
>
> --
> Microsoft MVP - Windows PowerShell
> http://www.microsoft.com/mvp
>
> PowerGadgets MVP
> http://www.powergadgets.com/mvp
>
> Blog:
> http://marcoshaw.blogspot.com
>