How to know idle time for process running in system - powershell

All,
I am using windows 7 os
I would like to know the idle time for each application or process running in my machine.
Ex: I opened one process like notepad.exe but I am not working on it from last 5 minutes, I need to get the idle time as 5 minutes for the process.
like that at one time I would like to know the idle time of all process running in my machine.
I tried the below code but I am not getting the any data for IdleTime
$processname = Get-Process | Select-Object -Property ProcessName, IdleTime,ID,WS
Please help me

There seems to be no property named Idletime.
You can check that out by doing:
Get-Process | Get-Member -force
Perhaps parsing the WMI Win32_Process could help you achieve your goal?
Try this:
$kerneltime = Get-WmiObject Win32_Process -Filter "Name='System idle process'" | Select KernelModeTime
$timeSpan = (new-object System.TimeSpan $kerneltime.KernelModeTime)

Related

Command to query services

I wrote a powershell command to audit services in HyperV HV's and
$Auditservices = get-service -computername $ComputerName -name "*iscsi*","*winrm*","*scvmm*","*vmms*","vss"| Select-Object Status, Name, MachineName
And output for the same is showing like below
MSiSCSI SCVMMAgent vmms vss WinRM Running Running Running Running Running
Is there anyway we can change the output to this format ?
MSiSCSI,Running
SCVMMAgent,Running
vmms,Running
vss,Running
WinRM,Running
If you really want to get output exactly as you've described, you'll need to create some strings:
$Auditservices | Foreach-Object {"$($_.Name),$($_.Status)"}
You might instead actually want it in CSV format? If so, pipe it to Export-CSV

powershell takes a long time to close connection?

I'm trying to run this powershell script on several remote network servers, but it takes a long time to complete the task and move to the next one.
Here's a basic script that will query a single remote server and return the data I want in a fast manner, but takes a long time to complete the entire script.
Get-EventLog -ComputerName WebServer1 -LogName System -EntryType Error -After ((Get-Date).Date.AddDays(-1))
I was running it through the Powershell ISE and from the command line in Powershell and both have the same behavior.
Any help is appreciated!
Thanks
As for the reason: mjolinor outlined that - Get-EventLog parses the whole logfile, and then filters the output to meet your parameters. What we want to do right now is to parse the event log one by one from the newest entries while the condition is true.
$i = 0
$EventParser = do
{
Get-EventLog -ComputerName Localhost -LogName System -EntryType Error -Newest $i; $i++
}
while
(
(Get-EventLog -ComputerName Localhost -LogName System -EntryType Error -Newest $i | Select -Last 1).TimeWritten -ge ((Get-Date).Date.AddHours(-24))
)
$EventParser|Where-Object { $_.TimeWritten -ge ((Get-Date).Date.AddHours(-24)) } | Sort-Object -Property Index -Descending | Get-Unique -OnType
I have measured your command and the loop I have written - here is the output for my machine
For $EventParser:
TotalMilliseconds : 634,6669
For your command (I changed webserver1 to localhost)
TotalSeconds : 14,3049668
I hope the script above will do the trick to speed up log parsing.
PS.
I have ran into one thing that I couldn't figure out. As you see, I am actually filtering the output twice for, I believe, the same condition (!). If I remove the last-line Where-Object statement, I receive 35 entries for my machine and it lists output even from 4 days ago ?? (Normally I receive 19 entries both from mine and original command). I have had to, to my surprise, re-apply the filter to receive the desired scope. Can you assist with that in a comment or should I post a question on that?
EDIT:
Now the problem doesn't occur. Edit: I have tweaked the code a little bit, I noticed that in some cases it may return duplicated entries.
Best regards,
AlexP

when windows service was created

We have a program that installs itself as a service windows, runs for about 5 minutes and then uninstalls.
Sometimes (due to bugs) it fails to uninstall and the service processes are left hanging.
I'm trying to write a powershell script that will remove all processes running more than an hour. Now I cant find anything that could tell me how long the service is running (when it was installed).
Is there a way to find out when service was installed or how long it is running?
Thank you,
Vitaly
You can use Get-Process to see when the service process was started, like so:
Get-Process | select name, starttime
This will get the processid based on the service name:
$myservice = "Spooler"
$starttime = Get-Process -id ((Get-WmiObject win32_service -Filter "name=`'$myService`'").ProcessID) | select StartTime
$timerunning = (get-date).Subtract($starttime.StartTime)

Script uses a lot of Ram if it's running long

I have a PS Script (running all the time on a Powershell 3.0) and there is a loop, which consumes many RAM.
while(1)
{
$te = Get-Winevent -MaxEvents 1 -FilterHashtable #{logname='application';id=2}| select -expand properties
Sleep 1
}
I tried to delete the Object and call the GC explicit. But nothing works! The Script is consuming a lot of RAM
How can I solve this Issue?
Judging by your code, it seems that you want to find all events in the Application event log with the ID of 2. If so, you're going about it wrong. You should use the Get-EventLog cmdlet. Try this:
Get-EventLog -LogName Application -InstanceID 2

Retrieving real time info from VM's with PowerCLI

I have a couple of lines in a script that are giving me an issue:
Connect-VIServer "test-vcenter.test.com" -User user -Password pass
Get-VM -Name "test-vm" | Get-Stat -Stat cpu.ready.summation -Realtime | Select-Object -First 1 value | Format-List
When running this I receive this as output:
Operation is not valid due to the current state of the object.
At :line:0 char:0
If the second line is run a few seconds after the connection to vCenter is made I receive the output I expect. What I believe is happening is that my connection to vCenter hasn't completed before my second line has started. I am not sure of the best way to wait for or what to check for in a completed connection.
you might try using start-sleep command in PS
http://technet.microsoft.com/en-us/library/ee177002.aspx
however -- as with all "sleep" functions, this isn't necessarily the best way to fix a timing problem as your pushback never may never account for all possible latency issues.
a better solution would be to test for the completion of a command (and I'm not sure how to do that with the VMWare CLI)