Howdy all -<\/p>\n
I have two parts of a short script, and am missing a middle part… hoping someone can point me in the right direction.<\/p>\n
Part 1<\/strong> - Queries WMI for all users running a specific process and returns the specific process’s path along with the username.<\/p>\n It works great, returning things like:<\/p>\n c:\\path1\\app.exe user1<\/p>\n c:\\path2\\app.exe user2<\/p>\n The process name is always the same (“app.exe”) - it’s the path and user that is critical.<\/p>\n Part 2<\/strong> - ???<\/p>\n Part 3<\/strong> - Finds the session ID of a user, logs them off.<\/p>\n Right now, part 2 is manual since I need to separate people running from PATH1 from PATH2, so I export from Step 1 to a text file, “FIND” the right path, put it into a second text file, then run Step 3 against that text file.<\/p>\n I’m trying to figure out what Step 2 is so that $user is only populated with a username running from PATH1 without having to do the manual bit.<\/p>\n Any help is appreciated!<\/p>","upvoteCount":6,"answerCount":2,"datePublished":"2019-03-07T17:08:32.000Z","author":{"@type":"Person","name":"jvanabra","url":"https://community.spiceworks.com/u/jvanabra"},"suggestedAnswer":[{"@type":"Answer","text":" Howdy all -<\/p>\n I have two parts of a short script, and am missing a middle part… hoping someone can point me in the right direction.<\/p>\n Part 1<\/strong> - Queries WMI for all users running a specific process and returns the specific process’s path along with the username.<\/p>\n It works great, returning things like:<\/p>\n c:\\path1\\app.exe user1<\/p>\n c:\\path2\\app.exe user2<\/p>\n The process name is always the same (“app.exe”) - it’s the path and user that is critical.<\/p>\n Part 2<\/strong> - ???<\/p>\n Part 3<\/strong> - Finds the session ID of a user, logs them off.<\/p>\n Right now, part 2 is manual since I need to separate people running from PATH1 from PATH2, so I export from Step 1 to a text file, “FIND” the right path, put it into a second text file, then run Step 3 against that text file.<\/p>\n I’m trying to figure out what Step 2 is so that $user is only populated with a username running from PATH1 without having to do the manual bit.<\/p>\n Any help is appreciated!<\/p>","upvoteCount":6,"datePublished":"2019-03-07T17:08:32.000Z","url":"https://community.spiceworks.com/t/missing-glue-find-or-match-in-a-simple-script/701130/1","author":{"@type":"Person","name":"jvanabra","url":"https://community.spiceworks.com/u/jvanabra"}},{"@type":"Answer","text":" Give this a shot. I am unable to test this so it may need tweaking. I’m only going off of what I think you want. So, I may be totally off as well.<\/p>\nGet-WmiObject -ComputerName $host win32_process | ? {$_.Name -eq $appname} | Select-Object Path, @{Name=\"UserName\";Expression={$_.GetOwner().User}}\n<\/code><\/pre>\n
((quser /server:$host | Where-Object { $_ -match $user }) -split ' +')[3] ; logoff $sid /server:$host\n<\/code><\/pre>\n
Get-WmiObject -ComputerName $host win32_process | ? {$_.Name -eq $appname} | Select-Object Path, @{Name=\"UserName\";Expression={$_.GetOwner().User}}\n<\/code><\/pre>\n
((quser /server:$host | Where-Object { $_ -match $user }) -split ' +')[3] ; logoff $sid /server:$host\n<\/code><\/pre>\n
$Users = Get-WmiObject -ComputerName $host win32_process | ? {$_.Name -eq $appname} | Select-Object Path, @{Name=\"UserName\";Expression={$_.GetOwner().User}}\n\nForeach ($User in $Users) {\n\n\tIf (($User.split()[0]) -ieq \"c:\\path1\\app.exe\") {\n\n\t\t$User = $User.split()[-1]\n\t\t((quser /server:$host | Where-Object { $_ -match $User }) -split ' +')[3] ; logoff $sid /server:$host\n\n\t}\n\t\n}\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2019-03-07T18:18:15.000Z","url":"https://community.spiceworks.com/t/missing-glue-find-or-match-in-a-simple-script/701130/2","author":{"@type":"Person","name":"patrickdeno2","url":"https://community.spiceworks.com/u/patrickdeno2"}}]}}