Skip to content

Instantly share code, notes, and snippets.

@tothandras
Last active August 29, 2015 14:19
Show Gist options
  • Save tothandras/dbe2885d8a2f4bfc8341 to your computer and use it in GitHub Desktop.
Save tothandras/dbe2885d8a2f4bfc8341 to your computer and use it in GitHub Desktop.
WSMan configuration management: Windows 8 client; Windows 8, Fedora servers
<#
.SYNOPSIS
Gather memory usage of processes on remote machines
.DESCRIPTION
This script uses WS-Management to gather memory usage of running
processes on remote machines listed in the input csv file.
.PARAMETER Machines
The input csv file. It must containt the following informations of each machine:
machineName,port,protocol,user,password
.PARAMETER OutFile
The output csv file the script will create.
.NOTES
- Author: Tóth András - O8POUA, 2015.04.11.
#>
#Requires -Version 4.0
[CmdletBinding()]
param (
# -Machines <string>
[Parameter(Mandatory=$True)]
[ValidateNotNullOrEmpty()]
[String] $Machines,
# -OutFile <string>
[Parameter(Mandatory=$True)]
[ValidateNotNullOrEmpty()]
[String] $OutFile,
# [-Name <string>]
[Parameter(Mandatory=$False)]
[ValidateNotNullOrEmpty()]
[String] $Name
)
# Check the existence and file type of the input file
if (!(Test-Path $Machines -Type Leaf -Include *.csv)) {
throw "Input file doesn't exist or it isn't a CSV file: $Machines"
}
# Check the existence of the output file directory
$outPath = Split-Path $OutFile
if ($outPath -and !(Test-Path $OutFile -Type Container)) {
throw "Output file directory doesn't exist: $OutFile"
}
# Import input CSV file
try {
$in = Import-Csv $Machines
} catch {
throw "Can't read input file: $Machines"
}
# Query to run on the CIM server
$query = 'SELECT Name, WorkingSetSize FROM CIM_Process'
# Filtering by name
if ($Name) {
$query += ' WHERE Name = "' + $Name + '"'
}
# Array to store processes
$processes = @()
# For each machine
foreach ($m in $in) {
if ($m.machineName -and $m.port -and $m.protocol -and $m.user -and $m.password) {
# Extract values
$machineName = $m.machineName
$port = [UInt32] $m.port
$protocol = $m.protocol
$secPassword = ConvertTo-SecureString $m.password -AsPlainText -Force
$credential = New-Object PSCredential($m.user, $secPassword)
# Try to connect using different authentication type (Fedora: Basic, Windows8: Default)
$types = 'Basic', 'Default'
foreach ($auth in $types) {
try {
# Test connection and open the session
$session = New-CimSession -ComputerName $machineName -Authentication $auth -Port $port -Credential $credential -ErrorAction Stop
} catch {}
}
# If the session is not null
if ($session) {
# Get instances
$instances = Get-CimInstance -CimSession $session -QueryDialect WQL -Query $query
foreach ($i in $instances) {
$p = New-Object PSObject -Property @{
machineName = $machineName
Name = $i.Name
WorkingSetSize = $i.WorkingSetSize
}
$processes += $p
}
} else {
Write-Error "Connection error: $m"
}
# Reset session to null
$session = $null
} else {
throw "Missing arguments in CSV file: $m"
}
# Write processes to the output file
try {
$processes | ConvertTo-Csv -NoTypeInformation | %{$_.Replace('"','')} | Out-File $OutFile
} catch {
throw "Can't write output file: $OutFile"
}
}
machineName port protocol user password
10.6.16.142 5985 http 104158 ****
10.6.17.32 5985 http 104158 ****
machineName Name WorkingSetSize
10.6.16.142 systemd 0
10.6.16.142 kthreadd 0
10.6.16.142 ksoftirqd/0 0
10.6.16.142 kworker/0:0H 0
10.6.16.142 rcu_sched 0
10.6.16.142 rcu_bh 0
10.6.16.142 rcuos/0 0
10.6.16.142 rcuob/0 0
10.6.16.142 migration/0 0
10.6.16.142 khelper 0
10.6.16.142 kdevtmpfs 0
10.6.16.142 netns 0
10.6.16.142 perf 0
10.6.16.142 writeback 0
10.6.16.142 ksmd 0
10.6.16.142 khugepaged 0
10.6.16.142 crypto 0
10.6.16.142 kintegrityd 0
10.6.16.142 bioset 0
10.6.16.142 kblockd 0
10.6.16.142 ata_sff 0
10.6.16.142 md 0
10.6.16.142 devfreq_wq 0
10.6.16.142 kswapd0 0
10.6.16.142 fsnotify_mark 0
10.6.16.142 kthrotld 0
10.6.16.142 acpi_thermal_pm 0
10.6.16.142 kworker/u2:1 0
10.6.16.142 scsi_eh_0 0
10.6.16.142 scsi_tmf_0 0
10.6.16.142 scsi_eh_1 0
10.6.16.142 scsi_tmf_1 0
10.6.16.142 kpsmoused 0
10.6.16.142 dm_bufio_cache 0
10.6.16.142 ipv6_addrconf 0
10.6.16.142 deferwq 0
10.6.16.142 kauditd 0
10.6.16.142 mpt_poll_0 0
10.6.16.142 mpt/0 0
10.6.16.142 scsi_eh_2 0
10.6.16.142 scsi_tmf_2 0
10.6.16.142 ttm_swap 0
10.6.16.142 kworker/0:1H 0
10.6.16.142 jbd2/sda2-8 0
10.6.16.142 ext4-rsv-conver 0
10.6.16.142 systemd-journal 0
10.6.16.142 lvmetad 0
10.6.16.142 systemd-udevd 0
10.6.16.142 ext4-rsv-conver 0
10.6.16.142 auditd 0
10.6.16.142 smartd 0
10.6.16.142 dbus-daemon 0
10.6.16.142 chronyd 0
10.6.16.142 systemd-logind 0
10.6.16.142 crond 0
10.6.16.142 openwsmand 0
10.6.16.142 cimserver 0
10.6.16.142 vmtoolsd 0
10.6.16.142 dhclient 0
10.6.16.142 dhclient 0
10.6.16.142 httpd 0
10.6.16.142 httpd 0
10.6.16.142 httpd 0
10.6.16.142 httpd 0
10.6.16.142 sshd 0
10.6.16.142 httpd 0
10.6.16.142 httpd 0
10.6.16.142 slapd 0
10.6.16.142 agetty 0
10.6.16.142 sshd 0
10.6.16.142 sshd 0
10.6.16.142 systemd 0
10.6.16.142 (sd-pam) 0
10.6.16.142 sshd 0
10.6.16.142 bash 0
10.6.16.142 polkitd 0
10.6.16.142 kworker/0:1 0
10.6.16.142 kworker/0:4 0
10.6.16.142 kworker/u2:2 0
10.6.16.142 kworker/0:0 0
10.6.16.142 cimprovagt 0
10.6.16.142 kworker/u2:0 0
10.6.16.142 kworker/0:2 0
10.6.17.32 System Idle Process 24576
10.6.17.32 System 1658880
10.6.17.32 smss.exe 368640
10.6.17.32 csrss.exe 1417216
10.6.17.32 wininit.exe 557056
10.6.17.32 services.exe 3444736
10.6.17.32 lsass.exe 6053888
10.6.17.32 svchost.exe 5140480
10.6.17.32 svchost.exe 3993600
10.6.17.32 svchost.exe 12632064
10.6.17.32 svchost.exe 32100352
10.6.17.32 svchost.exe 7962624
10.6.17.32 svchost.exe 30978048
10.6.17.32 svchost.exe 50241536
10.6.17.32 spoolsv.exe 1662976
10.6.17.32 svchost.exe 15843328
10.6.17.32 vmtoolsd.exe 5017600
10.6.17.32 MsMpEng.exe 32632832
10.6.17.32 svchost.exe 2220032
10.6.17.32 dllhost.exe 2146304
10.6.17.32 msdtc.exe 1028096
10.6.17.32 NisSrv.exe 3461120
10.6.17.32 SearchIndexer.exe 21893120
10.6.17.32 svchost.exe 3047424
10.6.17.32 cygrunsrv.exe 2654208
10.6.17.32 conhost.exe 741376
10.6.17.32 sshd.exe 5816320
10.6.17.32 csrss.exe 946176
10.6.17.32 winlogon.exe 585728
10.6.17.32 LogonUI.exe 14852096
10.6.17.32 dwm.exe 27623424
10.6.17.32 wmpnetwk.exe 462848
10.6.17.32 csrss.exe 12685312
10.6.17.32 winlogon.exe 925696
10.6.17.32 dwm.exe 34975744
10.6.17.32 rdpclip.exe 4063232
10.6.17.32 taskhostex.exe 4206592
10.6.17.32 explorer.exe 100212736
10.6.17.32 rdpinput.exe 1011712
10.6.17.32 TabTip.exe 3719168
10.6.17.32 TabTip32.exe 561152
10.6.17.32 vmtoolsd.exe 5251072
10.6.17.32 powershell.exe 102588416
10.6.17.32 conhost.exe 8417280
10.6.17.32 notepad.exe 8454144
10.6.17.32 WmiPrvSE.exe 5881856
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment