You could always multi-thread it.  I use this all the time and it rocks.
https://github.com/RamblingCookieMonster/Invoke-Parallel

Date: Mon, 6 Jul 2015 12:44:52 -0500
Subject: Re: [powershell] How best to speed up Get-WmiObject process call
From: [email protected]
To: [email protected]

Couldn't you just do:
$RA_User = $RunningApp.GetOwner().User
instead of doing another query?
On Mon, Jul 6, 2015 at 11:53 AM, Michael Leone <[email protected]> wrote:
This is mostly for me to learn some PS techniques. What I am doing:



I have a list of RemoteApps that is published on my RDS servers. From

a workstation running Win2012 R2, I am trying to remotely query each

of my 3 session hosts, and look at a list of users running each

RemoteApp. (I get the list of RemoteApps like this:



--------------

$OneSlash = "\"

$TwoSlash = "\\"



$PublishedRA = (Get-RDRemoteApp -ConnectionBroker $ConnectionBroker |

Select -Property DisplayName,FilePath) | Sort -Property DisplayName



ForEach ($RemoteApp in $PublishedRA)

{

$RA = New-Object PSObject

$RA | Add-Member -MemberType NoteProperty -Name DisplayName -Value

$RemoteApp.DisplayName

$RA | Add-Member -MemberType NoteProperty -Name FilePath -Value

$RemoteApp.FilePath.Replace($OneSlash, $TwoSlash)

$RA | Add-Member -MemberType NoteProperty -Name TheExecutable -Value

(Split-Path $RemoteApp.FilePath -Leaf) -Force

$PublishedRemoteApps += $RA

}





That executes quickly enough. So I have a list of the RemoteApps and

their executable path. Then I proceed to query the session hosts .I am

doing a Get-WmiObject call, querying the processes on each session

host for a process which has an executable that matches my RemoteApp.





$RunningApps = Get-WmiObject -Class Win32_Process -ComputerName

$SessionHost -Filter "ExecutablePath='$RA_FilePath'"



This gives me all the processes on the remote server $SessionHost,

that have an executable path the same as my published RemoteApp. (say

there are 18 processes, meaning 18 users using this particular

RemoteApp on this session host)



(here comes the big part of my problem)

Then, I am querying each of those returned objects for the owner's

username, and constructing a list of those:



ForEach ($RunningApp in $RunningApps)

{

$RA_User = ""

$User_PID = $RunningApp.ProcessID

$RA_User = (Get-WmiObject -Class Win32_Process -ComputerName

$SessionHost -Filter "ProcessID='$User_PID'").GetOwner().User

$RunningAppUser += $RA_User

}



Molasses in January would be faster than that, especially looping

through 3 session hosts :-) But how else can I get the owner's

username, of the process on the remote server? Usually, there are

approx 60 users, spread across the 3 session hosts.



Thanks for any pointers. I usually don't do WMI calls, so this is a

way to learn ...





================================================

Did you know you can also post and find answers on PowerShell in the forums?

http://www.myitforum.com/forums/default.asp?catApp=1







================================================
Did you know you can also post and find answers on PowerShell in the forums?
http://www.myitforum.com/forums/default.asp?catApp=1                            
          

================================================
Did you know you can also post and find answers on PowerShell in the forums?
http://www.myitforum.com/forums/default.asp?catApp=1

Reply via email to