Archive | Weird things / bugs RSS for this section

Weird get-process behaviour

Today I encountered some weird behaviour by the get-process command. I was working on a tool to get a list of open programs on a remote computer when I encountered some strange behaviour/inconsistencies, even within the host itself (without remotely connecting and querying).

Once I use the get-process -name command to get the session ID for each instance, it works fine on the local machine, if I run it like this:

(get-process -Name Explorer).SessionId

But one I run this ‘remote’ by specifying a computername (or even localhost on host itself), it returns session 0 for all instances

(get-process -Name Explorer -ComputerName "LOCALHOST").SessionId

Once I use Invoke-Command to ‘remotely’ run the first command, It again works like expected.

Invoke-Command {(get-Process -Name "Explorer").SessionId} -ComputerName 'LOCALHOST'

These are the commands and their output:

Get-Process strange behaviour

Does anyone know why it would lose the session information once I specify the computer name?

It appears that if I would like to check for CPU usage, the first and the third command again work fine, but the 2nd command doesn’t return anything at all!

Add-WindowsFeature Error: 0x800f0902 – The operation cannot be completed because Windows is currently performing another servicing operation

Today I encountered a problem, for which I couldn’t find a solution (which is pretty rare), so thought I’d post a blog about it and maybe someone can help me.

I sometimes have a problem with the Add-WindowsFeature. In powershell, overall it works fine, but sometimes it gives me the following error:

Add-WindowsFeature: The request to add or remove features on the specified server failed.

Installation of one or more roles, role services, or features failed.

The operation cannot be completed because Windows is currently performing another servicing operation.

Wait a few minutes and try running the command again. Error: 0x800f0902

At line:1 char:1

+ Add-WindowsFeature NET-Framework-Features,NET-Framework-Core,NET-Framework-45-Fe…

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidOperation: (@{Vhd=;Credent…Name=localhost}:PSObject) [Install-WindowsFeature],    Exception

    + FullyQualifiedErrorId : DISMAPI_Error__Failed_To_Enable_Updates,Microsoft.Windows.ServerManager.Commands.AddWindowsFeatureCommand

It seems that either the System Center Endpoint Protection (update and/or scan) or the Automatic Maintenance (in the Action center) causes the Add-WindowsFeature to fail (not sure which of the two yet).

Edit: I could localize the problem to be the Automatic Maintenance (in the Action Center). Is there a way to pause or disable this during the running of my Powershell script? Or at least is there a check if it is active or not? So I can check that and wait with installing the Windows Feature until automatic maintenance is finished.

Edit: I found the solution by checking the information MS has about the Automatic Maintenance as described at MSDN on , which led me to the task scheduler.

Get-ScheduledTask | Where TaskPath -like "*TaskScheduler*"
Led me to show all possible automatic maintenance tasks. Since I don’t want to disable or delete the task, I got several options. Two of which will follow.

– I can stop all running instances by using the following code:
Get-ScheduledTask | Where TaskPath -like "*TaskScheduler*" | Stop-ScheduledTask

– I can wait until maintenance tasks are finished and then continue with my code:

$CheckIfMaintenanceIsRunning = $true
$RunningTasks = Get-ScheduledTask | Where TaskPath -like "*TaskScheduler*" | Where State -eq "Running"
Start-Sleep -Seconds 5
$CheckIfMaintenanceIsRunning = $True
$CheckIfMaintenanceIsRunning = $false

Since in my case the probability of maintenance running while I execute my script is pretty low and also shouldn’t take too long to finish and the fact that I will run my code on systems owned and maintained by other system administrators, I will choose the wait option.


Edit: as _Emin_ stated in the comments, it is better to use

Get-ScheduledTask -TaskPath *TaskScheduler*

Thus the corresponding code above would become:

Get-ScheduledTask -TaskPath *TaskScheduler* | Stop-ScheduledTask


$RunningTasks = Get-ScheduledTask -TaskPath *TaskScheduler*| Where State -eq "Running"