Reputation: 48547
I have a few servers that I currently have to remote onto each one and check the same component to see how much memory it is using so I can audit it on a daily basis. I was wondering if there was a way to write a script to do this? Maybe a PowerShell script?
Upvotes: 3
Views: 7323
Reputation: 4773
There's a couple of ways using Get-Process. You can invoke the Get-Process to find out different memory usage of processes.
If you have powershell remoting you can invoke this command through the session using Invoke-Command -Session $Session.
Not sure which one of the memory sets you would want to report on, but you can select the different memory sets of your process.
Example:
Get-Process|select ProcessName, Path, VirtualMemorySize, PrivateMemorySize, NonpagedSystemMemorySize, PagedMemorySize, PeakPagedMemorySize
If you know the name of your process you can do the following
$process = Get-Process -Name "MyProcess.exe"
# Access its properties or report based on its properties
$process.VirtualMemorySize
If you want the script for all servers from one host/machine you could do like following:
# Array of PSSessions or you could use $computerNames with Invoke-command -ComputerName $computerNames instead
$session = @();
$results = Invoke-Command -Session $sessions -ScriptBlock {
return Get-Process -Name "MyProcess"|Select Name, Path, VirtualMemorySize
}
$results|ForEach-Object {
# Do something with each and every server result
}
Alternatively; you can use the Get-process directly with ComputerName if you have direct access to invoke the cmdlet remotely.
Example:
Get-Process -ComputerName $computerNames
Upvotes: 6