Thanks everyone for the help. ________________________________ From: Crawford, Scott [mailto:[email protected]] Sent: Tuesday, November 22, 2011 9:30 PM To: NT System Admin Issues Subject: RE: PSKill
Cool. Thanks much. From: Michael B. Smith [mailto:[email protected]] Sent: Tuesday, November 22, 2011 9:23 PM To: NT System Admin Issues Subject: RE: PSKill You can do that in PS v3 (in the Win8 beta) for simple pipelines. But in v1 and v2, you don't have access to $_ outside of a scriptblock subordinate to the pipeline. (The rules are more complicated than that, but that's more-or-less the rule.) This allows you to do things like inject the output of a pipeline into a complex switch statement. It's a syntactic requirement. There is no real reason for it from a scripter's perspective other than "it is the way it is". There is logic behind it, but that's only of interest to language designers. Regards, Michael B. Smith Consultant and Exchange MVP http://TheEssentialExchange.com From: Crawford, Scott [mailto:[email protected]]<mailto:[mailto:[email protected]]> Sent: Tuesday, November 22, 2011 10:11 PM To: NT System Admin Issues Subject: RE: PSKill Hmm, so that works. The % stands for "foreach" right? So why can't the end just be Stop-Process $_ since there's only one object in the pipeline? Scratch that - because, it's not an array at that point, right? So, does running it through foreach convert it to an array? From: Michael B. Smith [mailto:[email protected]]<mailto:[mailto:[email protected]]> Sent: Tuesday, November 22, 2011 8:25 PM To: NT System Admin Issues Subject: RE: PSKill Even when you expand Id. That results in a base object of [Int32]. A simple type. But the Id parameter is an [Int32[]]. An array type. Select-Object is type-safe (that is, it doesn't coerce types {normally}). So a single integer isn't an array of integers - and in fact, if it were, the pipeline would unroll it. So effectively you've got an option to pipe an object into Stop-Process. The object can be a psobject (containing properties named Name or Id) or a [Process] object or any other compatible type that ETS knows has the Name and/or Id properties. You could: Get-Process notepad | Sort -Property StartTime | Select -Last 1 -ExpandProperty Id |% { Stop-Process $_ } Regards, Michael B. Smith Consultant and Exchange MVP http://TheEssentialExchange.com From: Crawford, Scott [mailto:[email protected]]<mailto:[mailto:[email protected]]> Sent: Tuesday, November 22, 2011 9:08 PM To: NT System Admin Issues Subject: RE: PSKill Even when I expand ID? Or does that then pass a string? And, if so, could I have tacked a new Get-Process onto the end to reselect it and then pass that to Stop-Process? From: Michael B. Smith [mailto:[email protected]]<mailto:[mailto:[email protected]]> Sent: Tuesday, November 22, 2011 8:01 PM To: NT System Admin Issues Subject: RE: PSKill Because of the ExpandProperty. That causes the Select to return [DateTime] instead of [Process] Regards, Michael B. Smith Consultant and Exchange MVP http://TheEssentialExchange.com From: Crawford, Scott [mailto:[email protected]]<mailto:[mailto:[email protected]]> Sent: Tuesday, November 22, 2011 8:53 PM To: NT System Admin Issues Subject: RE: PSKill Get-Process <process-name> | Sort -Property StartTime | Select -First 1 | Stop-Process -Force Why doesn't it work to pipe your result into stop-process? I get: Get-Process notepad | Sort -Property StartTime | Select -Last 1 -ExpandProperty StartTime | Stop-Process Stop-Process : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input. At line:1 char:105 + Get-Process notepad | Sort -Property StartTime | Select -Last 1 -ExpandProperty StartTime | Stop-Process <<<< + CategoryInfo : InvalidArgument: (11/22/2011 7:46:52 PM:PSObject) [Stop-Process], ParameterBindingException + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.StopProcessCommand Even if I change to pass the Id: Get-Process notepad | Sort -Property StartTime | Select -Last 1 -ExpandProperty Id | Stop-Process Stop-Process : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input. At line:1 char:98 + Get-Process notepad | Sort -Property StartTime | Select -Last 1 -ExpandProperty Id | Stop-Process <<<< + CategoryInfo : InvalidArgument: (6036:PSObject) [Stop-Process], ParameterBindingException + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.StopProcessCommand -----Original Message----- From: Michael B. Smith [mailto:[email protected]]<mailto:[mailto:[email protected]]> Sent: Tuesday, November 22, 2011 6:56 PM To: NT System Admin Issues Subject: RE: PSKill Get-Process <process-name> | Sort -Property StartTime | Select -Last 1 -ExpandProperty StartTime Regards, Michael B. Smith Consultant and Exchange MVP http://TheEssentialExchange.com -----Original Message----- From: Ben Scott [mailto:[email protected]]<mailto:[mailto:[email protected]]> Sent: Tuesday, November 22, 2011 7:30 PM To: NT System Admin Issues Subject: Re: PSKill On Tue, Nov 22, 2011 at 7:23 PM, N Parr <[email protected]<mailto:[email protected]>> wrote: > Is there a way to kill the oldest process of multiple identical > process'? Looks like PSkill will just kill them all. Maybe get the > id's of the identical process and script it to kill the oldest identifier? You can't just pick the lowest PID, as PIDs aren't guaranteed to always increase, AFAIK. You'd need to look at the process start time. PowerShell prolly already has some obscure option to do it. ;) -- Ben ~ Finally, powerful endpoint security that ISN'T a resource hog! ~ ~ <http://www.sunbeltsoftware.com/Business/VIPRE-Enterprise/> ~ --- To manage subscriptions click here: http://lyris.sunbelt-software.com/read/my_forums/ or send an email to [email protected]<mailto:[email protected]> with the body: unsubscribe ntsysadmin ~ Finally, powerful endpoint security that ISN'T a resource hog! ~ ~ <http://www.sunbeltsoftware.com/Business/VIPRE-Enterprise/> ~ --- To manage subscriptions click here: http://lyris.sunbelt-software.com/read/my_forums/ or send an email to [email protected]<mailto:[email protected]> with the body: unsubscribe ntsysadmin ~ Finally, powerful endpoint security that ISN'T a resource hog! ~ ~ <http://www.sunbeltsoftware.com/Business/VIPRE-Enterprise/> ~ --- To manage subscriptions click here: http://lyris.sunbelt-software.com/read/my_forums/ or send an email to [email protected]<mailto:[email protected]> with the body: unsubscribe ntsysadmin ~ Finally, powerful endpoint security that ISN'T a resource hog! ~ ~ <http://www.sunbeltsoftware.com/Business/VIPRE-Enterprise/> ~ --- To manage subscriptions click here: http://lyris.sunbelt-software.com/read/my_forums/ or send an email to [email protected]<mailto:[email protected]> with the body: unsubscribe ntsysadmin ~ Finally, powerful endpoint security that ISN'T a resource hog! ~ ~ <http://www.sunbeltsoftware.com/Business/VIPRE-Enterprise/> ~ --- To manage subscriptions click here: http://lyris.sunbelt-software.com/read/my_forums/ or send an email to [email protected]<mailto:[email protected]> with the body: unsubscribe ntsysadmin ~ Finally, powerful endpoint security that ISN'T a resource hog! ~ ~ <http://www.sunbeltsoftware.com/Business/VIPRE-Enterprise/> ~ --- To manage subscriptions click here: http://lyris.sunbelt-software.com/read/my_forums/ or send an email to [email protected]<mailto:[email protected]> with the body: unsubscribe ntsysadmin ~ Finally, powerful endpoint security that ISN'T a resource hog! ~ ~ <http://www.sunbeltsoftware.com/Business/VIPRE-Enterprise/> ~ --- To manage subscriptions click here: http://lyris.sunbelt-software.com/read/my_forums/ or send an email to [email protected]<mailto:[email protected]> with the body: unsubscribe ntsysadmin ~ Finally, powerful endpoint security that ISN'T a resource hog! ~ ~ <http://www.sunbeltsoftware.com/Business/VIPRE-Enterprise/> ~ --- To manage subscriptions click here: http://lyris.sunbelt-software.com/read/my_forums/ or send an email to [email protected]<mailto:[email protected]> with the body: unsubscribe ntsysadmin ~ Finally, powerful endpoint security that ISN'T a resource hog! ~ ~ <http://www.sunbeltsoftware.com/Business/VIPRE-Enterprise/> ~ --- To manage subscriptions click here: http://lyris.sunbelt-software.com/read/my_forums/ or send an email to [email protected]<mailto:[email protected]> with the body: unsubscribe ntsysadmin ~ Finally, powerful endpoint security that ISN'T a resource hog! ~ ~ <http://www.sunbeltsoftware.com/Business/VIPRE-Enterprise/> ~ --- To manage subscriptions click here: http://lyris.sunbelt-software.com/read/my_forums/ or send an email to [email protected] with the body: unsubscribe ntsysadmin
