Monday, August 13, 2012

Get-Content -wait and Set-Content incompatibility

Under Powershell, there is a cmdlet named 'Set-Content' which is used to replace the content of a text file with something else. And under Powershell 3.0 there is the possibility to tail a file by using the well known cmdlet 'Get-Content' with the option '-wait'.

When reading the help file for 'Set-Content', it states that:

"The Set-Content cmdlet is a string-processing cmdlet that writes or replaces the content in the specified item, such as a file. Whereas the Add-Content cmdlet appends content to a file, Set-Content replaces the existing content. You can type the content in the command or send content through the pipeline to Set-Content."

The truth is that 'Set-Content' deletes the original file and creates a new one.

If by any chance you are having a look at the same file with 'Get-Content -wait', you will get this error here:

Get-Content : The process cannot access the file 'C:\file1.log' because it is being used by
another process.
At line:1 char:1
+ Get-Content C:\file1.log -wait| ?{$_ -like "*com* ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ReadError: (C:\file1.log:String) [Get-Content], IOException
    + FullyQualifiedErrorId : GetContentReaderIOError,Microsoft.PowerShell.Commands.GetContentCommand

As you can see this error message doesn't help you to understand where the problem could lie.

The bad news is that there is no solution to this for me. The only thing that I could invent to work around this issue and that works well, is to use 'Set-Content' to write into a buffer text file, use 'Add-Content' to append the buffer text file to the text file that I am monitoring with 'Get-Content -wait' and then remove the buffer log file with 'Remove-Item -force'.

This way 'Get-Content' will never loose grip on the monitored text file.

Hope this might help someone.

Friday, August 10, 2012

Enabling and starting a service in Powershell

Just a quick post to show you my new preferred method to enable and start a service. Until yesterday I opened a RDP connection to the remote server, then I fired the services.msc console and selected the service to enable, double-clicked on it and modified its settings.

Today things have changed and Powershell does it better. Just fire a Powershell session and execute the following cmdlet:

get-service -computername yourserver -name "windows installer" | set-service -StartupType "automatic"
get-service -computername yourserver -name "windows installer" | start-service
get-service -computername yourserver -name "windows installer"

Status   Name               DisplayName
------   ----               -----------
Running  MSIServer          Windows Installer

Of course you can use aliases to be quicker. Start by finding them with:

Get-Alias | ?{$_.definition -like "*service*"}

CommandType     Name            Definition
-----------     ----            ----------
Alias           gsv             Get-Service
Alias           sasv            Start-Service
Alias           spsv            Stop-Service

The previous command lines can now be rewritten like this:

gsv -computername yourserver -name "windows installer" | set-service -StartupType "automatic"
gsv -computername yourserver -name "windows installer" | sasv
gsv -computername yourserver -name "windows installer"

Status   Name               DisplayName
------   ----               -----------
Running  MSIServer          Windows Installer

Hope this helps.

Error 2147750687

This morning I was checking a failing scheduled task on one of my Windows 2008 R2 servers and for the first time I met a "task start failed" with the following error code: 2147750687. Nothing hard to solve, because at first sight I saw that this scheduled was set to run at a very high frequency and what happened is that, being the task configured to allow one instance only, new instances of the same task were denied to start.

Here's the full error event:

Log Name:      Microsoft-Windows-TaskScheduler/Operational
Source:        Microsoft-Windows-TaskScheduler
Date:          10/08/2012 09:19:36
Event ID:      101
Task Category: Task Start Failed
Level:         Error
Keywords:      (1)
User:          SYSTEM
Computer:      server.happysysadm.com
Description:
Task Scheduler failed to start "\check_memory_pools" task for user "company\username". Additional Data: Error Value: 2147750687.

If you have this kind of error, I can suggest two solutions: the first one is to reduce the frequency of the task. The second one is to change the task behavior. If you choose the latter, your options are:
  • Do not start a new instance
  • Run a new instance in parallel
  • Queue a new instance
  • Stop the existing instance   
Task scheduler options on Windows 2008 R2

For further information on these options check here.

Related Posts Plugin for WordPress, Blogger...