mardi 17 juillet 2012

Watch-Process

salut,

Parceque la gestion des processus est vitale dans la vie d'un administrateur, powershell nous a offert 5 cmdlet complémentaires:

                   
Get-Process
Stop-Process        
Wait-Process 
Debug-Process
Start-Process      
le visionnage des process se fait simplement par "Get-Process" l'idéee de Watch-Process est d'étendrre les capacitées de 'Get-Process' pour monitorer un ou plusieurs process par un intervalle de temps personnalisable, en local ou à distance:

function Watch-Process {
<#

.ForwardHelpTargetName Get-Process
.ForwardHelpCategory Cmdlet

#>
[CmdletBinding(DefaultParameterSetName='Name')]
param(
    [Parameter(ParameterSetName='Name', Position=0, ValueFromPipelineByPropertyName=$true)]
    [Alias('ProcessName')]
    [ValidateNotNullOrEmpty()]
    [System.String[]]
    ${Name},

    [Parameter(ParameterSetName='Id', Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
    [Alias('PID')]
    [System.Int32[]]
    ${Id},

    [Parameter(ValueFromPipelineByPropertyName=$true)]
    [Alias('Cn')]
    [ValidateNotNullOrEmpty()]
    [System.String[]]
    ${ComputerName},

    [ValidateNotNull()]
    [Switch]
    ${Module},
   
     [Switch]
     ${Continous},
    
     [int32]
     ${Seconds}=0,

    [Alias('FV','FVI')]
    [ValidateNotNull()]
    [Switch]
    ${FileVersionInfo},

    [Parameter(ParameterSetName='InputObject', Mandatory=$true, ValueFromPipeline=$true)]
    [System.Diagnostics.Process[]]
    ${InputObject})

begin
{
    try {
        $outBuffer = $null
        if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
        {
            $PSBoundParameters['OutBuffer'] = 1
        }
       
        $wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Microsoft.PowerShell.Management\Get-Process', [System.Management.Automation.CommandTypes]::Cmdlet)
        'Seconds','Continous' | foreach { $null=$PSBoundParameters.Remove($_) }
        $scriptCmd = {& $wrappedCmd @PSBoundParameters }
        $steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
        $steppablePipeline.Begin($PSCmdlet)
    } catch {
        throw
    }
}

process
{
       if($Continous) {
          while($true) {
              Start-Sleep -Seconds $Seconds
              Get-Process @PSBoundParameters
          }
       }
     
        try {
            $steppablePipeline.Process($_)
        } catch {
            throw
        }
}

end
{

    try {
        $steppablePipeline.End()
    } catch {
        throw
    }
}

}
on peux l'utiliser ainsi:
PS> Watch-Process -Name n* -Continous -Seconds 2
PS> Watch-Process note*
PS> Watch-Process -Name n* -Continous

Aucun commentaire: