PowerShell get running explorer process and their documents - powershell

How can i access document property of already running explorer processes. i am using following line of code to get process.
$ie2 = Get-Process |where {$.mainWindowTItle -eq "Windowtitletext"} | where {$.ID -ne $ieParentProcessNumber}
now i want to do some processing on this processes like $ie2.Document etc.

It seems like you are trying to access the Document (i.e a webpage's data) directly from the process. This is not possible using the get-process.
You would need to create a instance of a IE com object for example or use the System.Net.WebClient if you want to just read data from a web site. Post more info about what you are trying to do and we can possibly help you out better

You can attach to the ie window:
$app = New-Object -ComObject shell.application
$popup = $app.Windows() | where {$_.LocationName -like "*foo*"}
$popup.document

If you know that you'll receive 1 object:
(Get-Process explorer).CPU
If you want to know what are the available properties:
Get-Process explorer | Get-Member
If you have more than one object in your result set (e.g. Get-Process returning mutiple processes mathing search criteria):
Get-Process | Where-Object { $_.Handles -ge 200 } | Foreach-Object { $_.CPU }

Related

Kill a task based on its windows title

I'm looking to kill a process using it PID when a particular file is open on my computer, so that it doesn't interfere with another script
I have an excel sheet that is referenced by other parts of my system, it is fed information from many different sources I want to automate my workflow to work more smoothly.
Get-Process |where {$_.mainWindowTItle -eq "RASP transfer.xlsx - Excel"} |Write-Output
I'm stuck on how to extract the PID and use it as a variable in a task Stop-Process -ID PID
maybe I'm missing something help
in the end it should look something like this
Get-Process |
Where {$_.mainWindowTItle -eq "RASP transfer.xlsx - Excel"} |
Write-Output $pid=id |
Stop-Process -ID $PID
edit: 6/6/19
Currently I'm able to identify the PID of the specific workbook
by running
Get-Process |where {$_.mainWindowTItle -eq "RASP transfer.xlsx - Excel"} |Write-Output
which I can identify here in the taskmanager
However when the files are run with scripts the excel file is hidden and can't be searched with the windows title....
One of the nice features of Powershell is that you can pass an object on the pipeline. You don't even need the Process ID:
Get-Process | Where {$_.mainWindowTItle -eq "RASP transfer.xlsx - Excel"} | Stop-Process
However it's important to know what type of object you're passing along at each stage in the pipeline. The input to Stop-Process in the line above is a Process object, that has an ID property.
If you wanted to get the Process ID you could assign it to a variable.
$PID = (Get-Process | Where {$_.mainWindowTItle -eq "RASP transfer.xlsx - Excel"}).ID
... which will usually only get a single Process ID because Excel doesn't allow opening the same spreadsheet in two windows. But you can't rely on the window title to be unique because it would be the same if you opened two copies of the same spreadsheet from different folders.
If you wanted to close all Excel windows, this code would get all the Excel windows' processes into an Object[] array:
$ExcelProcs = Get-Process | Where {$_.mainWindowTItle -like "* - Excel"}
Stop-Process is smart enough to use the array of [Object] as input and stop all their processes:
$ExcelProcs | Stop-Process
Why not just re- enable the visibility before you check for the open sheet with:
$x1.Visible = $true
$PID = (Get-Process | Where {$_.mainWindowTItle -eq "RASP transfer.xlsx - Excel"}).ID

how to bring forward an IE window with powershell

I have the following powershell command that will run on users pc on a scheduled basis, based on a REST API Call to know if they havent submitted the timesheet.
It works fine, but I would like to put the IE window as the main window at that moment.
$msgBoxInput = [System.Windows.MessageBox]::Show('You havent submitted the timesheet. Please do it now','xyz is watching you','OK','Error')
$IE=new-object -com internetexplorer.application
$IE.navigate2("www.microsoft.com")
$IE.visible=$true
Is it doable with powershell? in an easy way
It should be doable.
Have a look at this example:
## Find all Active Windows Titles
$windows=Get-Process | Where-Object {$_.MainWindowTitle -ne ""} | Select-Object MainWindowTitle
## Find Specific name
$WindowTitle=($windows | ? {$_ -match "Internet Explorer"} ).MainWindowTitle
## Add Type and Focus Activate the Window
$wshell = New-Object -ComObject wscript.shell;
$wshell.AppActivate($WindowTitle)
You just have to make sure that you have the correct MainWindowTitle.
Hope it helps

Ouput Server Name Along With InstallState of Service

I'm pretty new to Powershell. I have the following simple little script:
$serversall = (Get-Content ".\Servers.txt")
foreach($vm in $serversall){
Get-WindowsFeature Web-Server -ComputerName $vm |
Select-Object InstallState |
Export-Csv -path E:\Scripts\Output\IISWebServersStatus.csv -Append
}
This just gives me one column to let me know if the status is "Installed" or "Available". I want another column that would also show the server name next to the status of the service. Any idea how I would accomplish that?
I would recommend the following approach:
$serversall = (Get-Content ".\Servers.txt")
$output = #()
foreach($vm in $serversall) {
$installed = (Get-WindowsFeature Web-Server -ComputerName $vm).InstallState
$output += New-Object PSCustomObject -Property #{'ComputerName'=$vm; 'Status'=$installed }
}
$output | Export-Csv -path E:\Scripts\Output\IISWebServersStatus.csv -Append
Create an array, $output that you can use to store all the information. On each loop iteration, build an object that holds the the server name and the install state and append it to the output array.
After the loop is done, write the output array to the csv file.
Doing one file write at the end will save on i/o operations, so could save time. In this example, Get-WindowsFeature is a relatively slow operation so it probably makes little difference, but good to understand the theory nonetheless.

Pipe sc query output to powershell method?

I'd like to pipe output from sc query to a method in powershell. For example, checking the status of a service and finding the part that says "STOPPED", and performing an action based on that output.
Is there a way to do this right from the output of sc query? Or do I need to output the results to a text file, and then.. I'm not sure, run a for-loop to find the piece I'm looking for to make an if condition true / false.
This is what I have so far:
Function IsStopped {
sc.exe query remoteregistry >> RemoteRegistry.txt
Get-Content -Path C:\RemoteRegistry.txt | Where-Object {$_ -like '*stopped*'} | ForEach-Object {
}
Not sure where to go next?
PowerShell has a cmdlet for examining services. Running Get-Service without parameters gives you all of the running services in the same way sc.exe does (actually while researching this I reminded myself that in PowerShell sc, without .exe, is an alias for Set-Content, so I ended up generating some useless files. This might be another good reason to use Get-Service to avoid confusion with Set-Content).
Running Get-Service | Get-Member gives a list of the properties and methods from the output of the command. Status is the Property of interest, so we run:
Get-Service | Where-Object { $_.Status -eq 'Stopped' }
The output of this command can then be piped into a for each loop as you have suggested, and each service's properties or methods can be accessed using the $_ shorthand:
Get-Service | Where-Object { $_.Status -eq 'Stopped' } | ForEach-Object {
Write-Host "Do something with $($_.ServiceName)"
}
It is possible to restart services in this manner, using $_.Start(), but I would recommend writing some error handling into the process if that's your ultimate aim.'
If you need more information, such as the executable, you might want to look here:
How can I extract "Path to executable" of all services with PowerShell

PowerShell 3.0 - Setting Affinity to CPU per USER's PROCESS

my first post here. I am working on a script using powerShell, the objective is to set a certain amount of CPU-threads per USER's process, using the forum here, i was able to find most of the answers, and even got my script to run, except, if it sets the affinity, it sets it to EVERY-Process, not just the user i need.
here is the code(with comments):
# GET LIST of all process running
$pList = get-wmiobject win32_process
# loop through created array and get the OWNER of the processes
foreach ($p in $pList) {
#If "myUserName" is found:
if ($p.getowner().User -eq 'myUserName') {
# get process name
$procName = $p.ProcessName
# trim STRING to remove EXE
$procName = $procName.Replace('.exe','')
# use get-process to make array of processes run by "myUserName"
$activeProc = Get-Process -name $procName
# Loop to set affinity for each process
foreach ($i in $activeProc){
$i.ProcessorAffinity=0xFE
}
}
}
when i execute this command, all of the process are set to new Thread Count,
any suggestions how to make it ONLY adjust threads for SPECIFIC user?
Thanks a lot guys!
this is pretty urgent.
By calling get-process -name $procName you are finding all processes that have the same name as one run by the user.
Instead of using the ProcessName, use ProcessId.
In PowerShell version 4.0, you can use the -IncludeUserName parameter on the Get-Process cmdlet. Once you have a list of processes, you can then filter then using the Where-Object cmdlet, which has a default alias of ?.
Get-Process -IncludeUserName | Where-Object -FilterScript { $PSItem.UserName -match 'system' };
Or short-hand might look like this:
gps -inc | ? { $_.UserName -match 'system' };
Note: Using the -IncludeUserName parameter requires privilege elevation.