Question
2011 Scripting Games : Advanced Event 4
- Matthew BETTON
- Auteur du sujet
- Hors Ligne
- Membre platinium
-
- Messages : 968
- Remerciements reçus 0
Ci-après le script que j'ai posté lors des Scripting Games 2011, dans le cadre de l'Advanced Event 4.
Le scénario était le suivant :
You are the network administrator for a large company with multiple locations around the world. Your performance team has expressed concern with the large amount of memory that is consumed by one particular instance of the SvcHost process. To investigate this process, the team lead has requested that you write a Windows PowerShell script that will list each instance of the SvcHost process, the amount of committed memory, the number of page faults, and the command line that launched each SvcHost process. In addition, the lead requires that the script list each service that is running inside each instance of the SvcHost process. An appropriate output from the script is shown in the following image.
Mon script (une solution) :
[code:1]#
# 2011 Scripting Games : Advanced Event 4
# Script : Get-SvcHostsStatus.ps1
# Author : Matthew BETTON (France / Basse-Normandie / Manche (50))
# Date : 04/07/2011
# Synopsis : Use PowerShell to Investigate the SvcHost Process
#
param(
[Parameter(Mandatory=$true)]
[String]$ComputerName,
[string]$ExportCSV
)
# Get the svchost processes list (WMI)
$SvcHostProcess = Get-WmiObject -Class Win32_Process -ComputerName $ComputerName -Filter \"Name = 'svchost.exe'\"
$Tab = $null
$Tab = @()
foreach($Process in $SvcHostProcess){
# Get the service(s) that is (are) running inside the instance of the current svchost process
$svc = get-wmiobject win32_service -ComputerName $ComputerName -filter \"processid=$($Process.ProcessId)\"
foreach($Service in $svc){
# Create a new object
$ProcObj = New-Object PSObject
# Add new properties to the object
$ProcObj | Add-Member -MemberType NoteProperty -Name \"ProcessID\" -Value $Process.ProcessID -PassThru `
| Add-Member -MemberType NoteProperty -Name \"PageFaults\" -Value $Process.PageFaults -PassThru `
| Add-Member -MemberType NoteProperty -Name \"CommitedMemory\" -Value $Process.VirtualSize -PassThru `
| Add-Member -MemberType NoteProperty -Name \"CommandLine\" -Value $Process.CommandLine -PassThru `
| Add-Member -MemberType NoteProperty -Name \"StartMode\" -Value $Service.StartMode -PassThru `
| Add-Member -MemberType NoteProperty -Name \"State\" -Value $Service.State -PassThru `
| Add-Member -MemberType NoteProperty -Name \"ServiceName\" -Value $Service.Name
# Commited Memory
# Add the object to the collection
$Tab += $ProcObj
}
}
# If '-ExportCSV' parameter is used
if($ExportCSV){
# Export data to the specified CSV file
$Tab | Export-Csv -NoTypeInformation -Path $ExportCSV
}
else{
# Writte objects to the output
$Tab
}[/code:1]
Pour les personnes que cela intéresse, voici le lien vers une solution d'un expert .
Toutes les remarques seront les bienvenues !
Connexion ou Créer un compte pour participer à la conversation.
- Vous êtes ici :
-
Accueil
-
forum
-
PowerShell
-
Discussions générales
- 2011 Scripting Games : Advanced Event 4