How to find out what my computer is doing when I run a process? - debugging

Edit: I've been reading about Windows debugging. It's pretty complicated. Can I use that find out what's going on when I execute those commands?
I posted a question about some PowerShell code, in which every access of the recycle bin object is taking about 3 to 4 seconds. Some people commented that they ran the code and those actions reported taking zero seconds. So the code is okay, but something is slowing down the process on my computer. This is serious because I need to do that operation over 17,000 times, which adds up to over 14 hours.
I wondered if Event Viewer could help me see what's going on. I've never used it before, but read up on it and created three custom views for all event levels in:
Windows Logs
Applications > SentinelOne (my Internet security software)
Applications > PowerShell
When I run the code, none of those logs adds any entries at all.
The PowerShell code that is taking about 3 or 4 seconds is, after setting:
$oShell = New-Object -com shell.application
$ssfBitBucket = 10
$oRecycleBin = $oShell.Namespace($ssfBitBucket)
both:
echo $oRecycleBin.Items().count
and
$oRBItem = $oRecycleBin.Items().Item(0)
The context of those commands is in the other question.
How can I find out what my computer is doing when I execute those commends?

Related

Invalid query on "Win32_PerfFormattedData_PerfOS_Processor"

My software (with admin priviledges) periodically performs the following query via WMI to know the status of the CPU:
ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from Win32_PerfFormattedData_PerfOS_Processor");
ManagementObjectCollection collection = searcher.Get();
Suddenly (meaning after months where the result of searcher.Get() was always available) the command started sitting down and throwing an "Invalid query" Exception after a timeout. I cannot say what's changed in the machine before this happens.
I confirmed this error by testing it with tool wbemtest:
The error happens everytime, even after reboot. How can I debug it?
System is Windows 10 x64 IoT Enterprise v1607.
The solution is explained in this article.
Anyway, I still have not understood the reason why the counters may get corrupted. Any comment on this?
I ran into this issue on a Windows 7 machine separate from a network and required to be kept around for various reasons.
At first I thought it was an admin thing (the program runs as a normal user) because system event viewer error 2011 hints at that.
However, it's fixed with this:
lodctr /r
I don't fully understand why, but this article was my source:
https://social.msdn.microsoft.com/Forums/vstudio/en-US/4d574e10-17f5-4599-95d6-2492ede3cfef/wmi-query-from-net-application-cause-2011-error-event?forum=netfxbcl

Prevent screen-lock / sleep-lock / password-lock / etc on Windows 10

I have a computer-based test that takes several hours to complete.
However, the test is timed-out at some point, because my PC "goes to sleep in one way or another".
This is possibly related to the fact that the test consists of two processes which communicate with each other via port, so I'm suspecting that perhaps networking is disabled in some way (even if it's completely "local networking").
I have disabled both screen turn off and sleep in the Settings "page", under Power & Sleep.
Still no luck, the screen is locked with a password at some point, which I suspect causes the test to stop running in the background.
I even followed a procedure that I found on the web to disable screen-lock via Regedit in something like 18 steps (why on earth did this company figure out that this is a reasonable user experience).
Is there a solution to this problem?
Found a (very hacky) solution:
If you keep all windows minimized, then the screen doesn't get locked.
What a great operating system, by such a great company!!!

Slow Windows shell performance using _hotkeys folder shortcuts for volume control

I'm trying to create a native, global, focus-independent hotkey control for incrementing/decrementing/mute-toggling system volume on Windows, specifically W10, though I expect whatever solution we find will likely work back through 7 and 8/8.1 as well.
I have found this - How can I mute/unmute my sound from PowerShell - and it works a treat, from within a PowerShell window. Ok, but I need this Type to be persistent and global then?
I then created three files, -.ps1, +.ps1, and 0.ps1, which include the entire type definition from the linked post, and then a one liner each, as follows:
[Audio]::Volume = [Audio]::Volume - .2
[Audio]::Volume = [Audio]::Volume + .2
[Audio]::Mute = [Audio]::Mute -bxor 1
Technically, all that works, but running each one pops open a PS window for 50 milliseconds or so. That's ugly, but maybe unavoidable.
Then I found this, - How to run a PowerShell script without displaying a window? - to use PSRun.exe. Turns out that in no case could I get that to successfully execute any of these scripts. I used shortcuts (.lnk) with arguments and .cmd files, and only got compilation errors.
So I tried this - https://www.faqforge.com/windows/how-to-execute-powershell-scripts-without-pop-up-window/ - using a VBS helper to hide the window. That works too, but I'd rather not use VBS if possible.
I then created a folder, %homedrive%\Users\\AppData\Roaming\Microsoft\Windows\Start Menu_hotkeys, and put one shortcut each to the .ps1 files, and assigned Alt-F6, Alt-F7, and Alt-F8 to the shortcuts.
When I run one of the shortcuts by GUI clicking, it takes about 100 milliseconds for either the .vbs or the .ps1 directly. However, when running either shortcut via hotkey, it takes more than a second!
I imagine there might be two (or more) solutions:
Can I increase the performance of a Windows shell hotkey that runs a shortcut to a script file? I realize that call stack looks silly long for something that seems like it should be so simple of a task.
OR
Can I create a persistent PowerShell window and run scripts into it by use of a global, focus-independent hotkey?
I'd even entertain building a TSR that does nothing but silently awaits hotkeys for volume control, but that seems less "nice" than working this out with PowerShell.
And before you ask, it's not a performance issue from this hardware, I'm running W10x64 on an i7-7700K with 16GB of RAM and a 128GB M.2. Running 40 Firefox tabs and 25 applications gets me to about 20% CPU use and 40% RAM use.
Feel free to slap me around if this question already has a solid, functional answer and my Google-Fu just didn't find it. My searches turned up things from across the last 5 years, but nothing conclusive or properly functional.

Bash console crashed and 6GB storage space got used up

I'm using windows 10 and yesterday I installed bash.
I have had no prior coding experience and using bash was a bit of a nightmare for me. I just needed it to run a simple code to join 2 images into one image for a batch of 800 pairs of images.
I made a lot of mistakes in the code as I was using code found online.
In the process, some code would result in an ever increasing output file and I had to close the console using task manager just to end the process. System resources would go very high.
After I had ended the console a couple of times I realized over 6GB of disk space has been used up.
The problem is I can't find any way of clearing that space since I can't see what's using it. I've tried using disk cleanup but that didn't help.
How can I clear all that 6GB space that's used up by a bad code and prematurely ended bash console?
You have a couple of options:
You can try to execute "history" in bash and work out where the file is from the command line history
alternatively run:
find / -size 6G -ctime 0
This will search the whole file system for files changed in the last 24 hours that used 16G of data. Increment ctime if you don't find the file in question.

Speed Up PowerShell Scripts in V2?

I'm running a number of scripts using PowerShell V2, and I have noticed a long pause when the console window first loads. What can I do to improve the performance of my scripts?
Thanks, MagicAndi
Other than minimize what you put in your various profile scripts (shown below) there isn't much you can do:
C:\PS> $profile | fl * -force
AllUsersAllHosts : C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
AllUsersCurrentHost : C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1
CurrentUserAllHosts : C:\Users\hillr\Documents\WindowsPowerShell\profile.ps1
CurrentUserCurrentHost : C:\Users\hillr\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
One way to check to see if profile scripts are causing the lag is to start powershell using the -noprofile option. If the startup time is different it would be due to your profile scripts. You can use a .NET stopwatch like so:
function TimeThis([scriptblock]$scriptblock, $msg)
{
if (!$stopWatch)
{
$script:stopWatch = new-object System.Diagnostics.StopWatch
}
$stopWatch.Reset()
$stopWatch.Start()
. $scriptblock
$stopWatch.Stop()
if ($msg -eq $null) { $msg = "$scriptblock" }
"Execution time: $($stopWatch.ElapsedMilliseconds) mS for $msg"
}
. TimeThis {Import-Module $Module -args ~\Pscx.UserPreferences.ps1}
While you could use Measure-Command, it doesn't show what is executed and you get no command output (only time in a very verbose fashion).
There used to be an issue in earlier CTPs where the installer wouldn't ngen the PowerShell assemblies and that could cause noticeable load time delays. However I'm pretty sure that has been fixed as of the final 2.0 install (and certainly with PowerShell built into Windows 7 and Windows Server 2008 R2). If the follow dir and its contents exist, you should be ngen'd:
dir 'C:\Windows\assembly\NativeImages_v2.0.50727_32\Microsoft.PowerShel#' -r
Tracking down a performance issue like this can be tricky, but there are a few things you can do to improve / fix things.
First off, starting PowerShell cold versus warm. At least on my workstation, the first time I run PS in the morning, it takes a bit longer to launch than subsequent times. Is there a way you can keep it warm to minimize the load times?
Second, use a tool like Process Monitor from the fine folks working for Windows Sysinternals. Set it to monitor the powershell.exe process and see what it is doing that is taking so long. For me, I have a number of mapped network drives and shared scripts that get sourced from the network. In my testing, I measured about a two second delay in starting per remote script I was loading.
PowerShell does need to load a bunch of resources from disk into memory, so it should go without saying that having your I/O system perform optimally will help as well. Defrag, make sure you have ample free RAM, etc. It even queries quite a bit from the registry so you may want to ensure your registry is completely defragged - though that is quite a long shot.
It may be completely unrelated but I have once had a massive pause during powershell startup.
It was somehow related to the fact that my laptop was in a domain, I then hibernated it and started it again when the laptop was not in the domain anymore. I looked at the startup with dottrace and could only see that somewhere in the initialization of Providers the code got stuck.
Restarting the machine helped in this case. Also it doesn't always happen (in fact, only once so far).
use inline C# speedup x100 max
Add-Type -Language CSharpVersion3 #"
"#
Add-Type #"
"#
if may not use -TypeDefinition #"
use 32bit powershell
speedup x1,5
ps-run.cmd :
SET PS32=%windir%\SysWOW64\WindowsPowerShell\v1.0\powershell
%PS32% .\%*

Resources