0

Code snippets : Restart IIS Application Pool in PowerShell

-

If you need to watch IIS application pool and start it once goes down and want to use PowerShell for that here is simple script for that.

Script includes also logging all restarts in log file which is created in same folder as script.

# AppPool to monitor
$AppPoolName = "AppPoolName"

# Log Destination
$LogToScreen = 1
$LogToFile = 1

# Determine script location for PowerShell
$ScriptDir = Split-Path $script:MyInvocation.MyCommand.Path

# Output log
$OutputLogPath = $ScriptDir + "\AppPoolRestart.csv"

#Check if file exists and create if it doesn't
If(!(Test-Path -Path $OutputLogPath)){
#Create file and start logging
New-Item -Path $OutputLogPath -ItemType File
}

###
# Function: LogMessage
###
function LogMessage($Msg, $ToScreen, $ToFile)
{
if ($ToScreen -eq 1)
{
Write-Host $Message
}

If ($ToFile -eq 1)
{
Add-Content -Path $OutputLogPath -Value $Message
}
}
###
# End Function: LogMessage
###

While (1 -eq 1){

Clear-Host

$AppPoolStatus = Get-WebAppPoolState -Name $AppPoolName

$DateTime = Get-Date

If ($AppPoolStatus.Value -eq "Stopped"){
Write-Host "Not Working"
Start-WebAppPool -Name $AppPoolName
$Message = "App Pool $AppPoolName restarted at $DateTime"
LogMessage $Message $LogToScreen $LogToFile
}Else{
Write-Host "Working"
}

Sleep 5
}
0

Code snippets : Execute SQL script using PowerShell

-

Recently I went through requirement of running SQL scripts on multiple databases on different servers. As the list of databases was different from script to script I decided to create universal solution, which will allow to easily prepare for deployment and can be simply reused at any time.

As a platform to execute scripts I went with PowerShell as that provides flexibility in case additional functionality will be required.

Whole solution contains 3 files:

  • SqlExecutionInflow.csv – file contains list of databases and servers where particular database is located. It is simple CSV file with 2 columns
  • SqlExecQuery.sql – contains SQL script which will be executed against all databases listed in SqlExecutionInflow.csv
  • SqlExec.ps1 – main script which load SqlExecInflow.csv and executes query from SqlExecQuery.sql

All files have to be placed in same folder. As a result script will create transcript file with output from all executed commands.

And here are example files and script itself…

(more…)

0

Tips & Tricks : MediaWiki 1.25.1 and VisualEditor

-

In case you want to deploy MediaWiki 1.25.1 with VisualEditor use following components:

Configuration details for LocalSettings.php for these components can be found here:

When you download VisualEditor from Git repository installation with MediaWiki 1.25.1 is more likely to fail.

0

Cheat Sheet : SCO Vi Quick Reference Guide

-

I just found vi reference from Santa Cruz Operation (SCO) 🙂 Nice piece of memorabilia as well as handy reference for vi beginners.

0

Tips & Tricks : Using EPEL with CentOS

-

If you use CentOS and want to add some extra packages you can use EPEL.

Extra Packages for Enterprise Linux is project created by Fedora.

In order to use repositories on CentOS first download and install RPM appropriate for your version of CentOS from here:

Method 1

http://dl.fedoraproject.org/pub/epel/

Then install it and enjoy more packages from new repo.

 

Method 2

[root@localhost ~]# yum install epel-release

Here is example from CenOS 7 how to add EPEL repositories:

(more…)

0

How-To : openSUSE & keepalived for Firewall HA

-

Current expectations from all type of systems and services is to be available without any disruption. One of mission critical systems is gateway/firewall. Of course you can use multiple products available on the market. One of them is openSUSE, which is perfect to act as gateway/firewall between Internet and production systems. So, how to provide redundancy for openSUSE and same time be able to keep it up-to-date without any disruption for production systems?

openSUSE 13.2 with keepalived provides all technology required to make your firewall high-available.

Here is diagram of Lab created to configure and test openSUSE with keepalived:

(more…)

0

Code snippet : Sending email from PowerShell script

-

If you want to send email from PowerShell script via Google Mail:

$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"
$SMTPUsername = "sender@gmail.com"
$SMTPPassword = "password"

$EmailSenderAddress = New-Object System.Net.Mail.MailAddress("sender@gmail.com", "PowerShell Automated Email")

$EmailFrom = $EmailSenderAddress
$EmailTo = "recipient@gmail.com"
$EmailCc = "recipient@hotmail.com"
$EmailAttachment = "C:\Scripts\Attachment.txt"
$EmailSubject = "Automated Email with Report."
$EmailBody = @"
Please find attached report.
"@

$EmailMessage = New-Object System.Net.Mail.MailMessage
$EmailMessage.Subject = $EmailSubject
$EmailMessage.Body = $EmailBody
$EmailMessage.To.Add($EmailTo)
$EmailMessage.CC.Add($EmailCc)
$EmailMessage.From = $SMTPUsername
$EmailMessage.Attachments.Add($EmailAttachment)

$SMTPSession = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
$SMTPSession.EnableSSL = $true
$SMTPSession.Credentials = New-Object System.Net.NetworkCredential($SMTPUsername, $SMTPPassword);
$SMTPSession.Send($EmailMessage)
Write-Host "Mail Sent"
0

Code snippets : Encrypted password in PowerShell

-

Many times there is a need to store password in PowerShell script. Unfortunately to leave password in script is insecure solution and might cause an issue.

So, what to do if there is a need to have password in a script and we don’t want to have it embedded in clear-text in script?

Best way is to encrypt it and store in the file and then re-use it when required.

Create file with encrypted password

$secureString = Read-Host -AsSecureString "Enter a secret password"
$secureString | ConvertFrom-SecureString | Out-File C:\Scripts\storedPassword.txt

Use encrypted password in script

$secureString = Get-Content -Path C:\Scripts\storedPassword.txt | ConvertTo-SecureString
Remember that password has to be encrypted on the account which will be running the script. So, in case you plat to use Task Scheduler to run PowerShell script, start PowerShell windows as particular user and then create file with encrypted password.
0

Tips & Tricks : Install Node.js on Debian 7.0 “Wheezy”

-

As on Debian 7.0 Node.js is not included in standard repositories, if you want to install it login to server as a root and issue following commands:

apt-get install curl
curl -sL https://deb.nodesource.com/setup | bash -
apt-get install -y nodejs
0

How-To : Autostart Parsoid on openSUSE 13.1

-

Once Parsoid is installed on openSUSE it might be handy to have is started automatically once system is restarted.

First we have to create startup script.

/etc/init.d/parsoid

#!/bin/sh

#
# description: Node.js /srv/parsoid/api/server.js
#

. /etc/rc.status

USER="root"

DAEMON="/usr/bin/node"
ROOT_DIR="/srv/parsoid/api"
LOG_ROOT="/var/log/nodejs"

SERVER="$ROOT_DIR/server.js"
LOG_FILE="$LOG_ROOT/parsoid.log"

LOCK_FILE="/var/lock/subsys/node-server"

WORKERS_NUMBER=8

do_start()
{
if [ ! -f "$LOCK_FILE" ] ; then
echo -n $"Starting $SERVER: "
runuser -l "$USER" -c "$DAEMON $SERVER -n $WORKERS_NUMBER >> $LOG_FILE &" && echo || echo
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch $LOCK_FILE
else
echo "$SERVER is locked."
RETVAL=1
fi
}
do_stop()
{
echo -n $"Stopping $SERVER: "
pid=`ps -aefw | grep "$DAEMON $SERVER" | grep -v " grep " | awk '{print $2}'`
kill -9 $pid > /dev/null 2>&1 && echo || echo
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f $LOCK_FILE
}

case "$1" in
start)
do_start
;;
stop)
do_stop
;;
restart)
do_stop
do_start
;;
*)
echo "Usage: $0 {start|stop|restart}"
RETVAL=1
esac

exit $RETVAL
Remember to adjust all paths and number of worker processes accordingly to your installation.

Once startup script is in place we have to enable Parsoid to be automatically started:

chkconfig parsoid on

From now on Parsoid will be automatically started after each restart.