PowerShell script for recovering a service

First of all; Happy new year!

This script I’m about to show you is more of a workaround than a solution. Sometimes the recovery function for services is not enough to recover a service. I’ve found that in a number of occurrences the service doesn’t start successfully and stays in status “starting”. Windows doesn’t pick this up and the service stays in status “starting” until some third party monitoring software or someone else discovers the failed start.

Now as I wrote earlier this script is more of a workaround than a solution, but it has helped me more than once to serve as a quick-fix and buys some more time to get to the root cause of the issue.

# Check service status
$SrvName = "Service name"
# Enter the name of the service. E.g. "SQL Server Reporting Services (MSSQLSERVER)"

$ServiceStatus = Get-Service $SrvName

# Execute Start-Service or no action based on status of the service
if ( $ServiceStatus.status –eq "Running" )
{
Echo "No Action - The service is running"
}
ELSE
{
Echo "The service has status starting, stopping or stopped."
Echo "Attempting to start the service."
Start-Service -Name $SrvName
}

Great editor for PowerShell scripting

Hello!

I’ve posted some scripts and other (hopefully) useful tips and tricks for PowerShell scripting. However, I’ve never told you about PowerGUI. Microsofts editor is very simple and is just fine, but PowerGUI takes PowerShell scripting to a whole new level! It’s free, easy to use and there are a lot of youtube videos out there showing you how to utilize it’s best features. You can get it here.

Also; the PowerGUI site also has a big library of PowerShell scripts for you to use.

Clean up your log directories with PowerShell

Well now! It’s been a long time since the last post and I’m sorry. Things have been quite hectic!

Today I’d like to share with you a script for cleaning up your log directories. Very practical if you don’t have the need for log files older than xx days.
As usual this is a small script, but it can be incorporated with other existing scripts, or you could build more logic to delete files with certain file extensions or the like.


## Set UNC path. This is the folder which will be cleaned by the script.
$folderPathRemoteFolder = \\ServerName\c$\foldername

## Keep files for x days. The script will delete all files older than the number of days configured below.
$KeepFiles = "7"

#Removing files older than x days
Get-ChildItem $folderPathRemoteFolder -recurse | ?{!$_.PSIsContainer -and ($_.LastWriteTime -lt (Get-Date).adddays("-" + "$KeepFiles"))} | Remove-Item -force

PowerShell – Add logging to your scripts

This isn’t very advanced, but still something which has helped me a lot when troubleshooting powershell scripts refusing to execute correctly.
Especially if it works when you test it manually, but fails with automatic execution…

It will set working directory, so you won’t need to worry about hard coded paths when making your scripts.
It also creates a log directory if it does not already exist and ads a timestamp to the log file name.

Happy PowerShelling :D

#Set working directory
$WorkingDir = Get-Location

#Set logs directory
$LogsDir = "$WorkingDir\Logs\"

#Set timestamp for log file
$TimeStamp = Get-Date -Format "yyyyMMdd_hhmm"

#Determine log file name
$LogFile = "$LogsDir" + $TimeStamp + "_ScriptName.log"

#Check if logs directory exists. If not, create logs directory.
if(test-path $LogsDir)
{ echo "Logs directory exists."
}
else
{
New-Item .\Logs -type Directory
}

#Start logging
Start-Transcript $LogFile

Your script goes here

#Stop logging
Stop-Transcript