Enable or Disable K2 blackpearl hostserver logging using PowerShell

If you are on a development machine and you need to frequently enable or disable file based host server logging, the following script will come in handy.

Note: Execution policy has to be changed to run this script. (Set-ExecutionPolicy RemoteSigned –Force)

## set-hostserverlogging.ps1 ##

#Set K2HostServer parameters
$k2Host=“localhost”
$k2WorkflowPort=“5252”
[int]$SecondsToWaitForResponse 30
$HostServerLogFile “C:\Program Files (x86)\K2 blackpearl\Host Server\Bin\HostServerLogging.config”
$Status=“”

#Check the current setting
if (Test-Path $HostServerLogFile)
{
$xmldoc (Get-Content $HostServerLogFileas [xml]
$loglevelsetting $xmldoc.SelectSingleNode(‘//LogLocationSettings/LogLocation[@Name=”FileExtension”]/@LogLevel’).‘#text’
}
else
{
write-host(“$HostServerLogFile does not exist”Foregroundcolor Red
Break
}

Function Restart-K2Service
{
[Reflection.Assembly]::LoadWithPartialName(“SourceCode.Workflow.Client”)
Write-Host “Stopping and restarting K2 blackpearl Server Service on ‘$k2Host'” Foregroundcolor Red
Restart-Service displayname “K2 blackpearl Server” EA “Stop”
Write-Host “K2 blackpearl Server Service is restarted successfully” Foregroundcolor Green
Write-Host “Trying to open a connection within $SecondsToWaitForResponse seconds”
$x=0
$success $false
$k2con New-Object SourceCode.Workflow.Client.Connection
while (($x lt $SecondsToWaitForResponseand (!$success))
{
$success $true
$error.clear();
t
rap {write-host “..Failed. will try again” Foregroundcolor Yellow ;continue}
$k2con.Open($k2Host,
“Integrated=True;IsPrimaryLogin=True;Authenticate=True;EncryptedPassword=False;Host=$k2Host;Port=$k2WorkflowPort”);
If($error)
{
$success $false
}
sleep 1;
$x++ 
;
}

If ($Success)
{
$k2con.Dispose()
write-host “K2 service is running and File logging is $Status” Foregroundcolor Green
if ($Status eq “Enabled”)
{
write-host “Check the Host Server\Bin directory for the latest log file”
}
}
else
{
write-host “”
Throw (New-Object System.Management.Automation.RuntimeException “K2 server did not respond in a timely fashion”)
}
}

Function Enable-FileLogging
{
if ($loglevelsetting ne “All”)
{
$xmldoc.SelectSingleNode(‘//LogLocationSettings/LogLocation[@Name=”FileExtension”]/@LogLevel’).‘#text’ ‘All’
$xmldoc.Save((Resolve-Path $HostServerLogFile))
$Status “Enabled”
write-host(“$HostServerLogFile is updated”Foregroundcolor Green
}
else
{
write-host(“File logging is already enabled. No changes are made to the config file”)
Break
}
Restart-K2Service
}

Function Disable-FileLogging
{
if ($loglevelsetting eq “All”)
{
$xmldoc.SelectSingleNode(‘//LogLocationSettings/LogLocation[@Name=”FileExtension”]/@LogLevel’).‘#text’ ‘Error’
$xmldoc.Save((Resolve-Path $HostServerLogFile))
$Status
“Disabled”
write-host(“$HostServerLogFile is updated”Foregroundcolor Green
}
else
{
write-host(“File logging is already disabled. Current setting is $loglevelsetting. No changes are made to the config file”)
Break
}
Restart-K2Service
}

Function Select-Item
{
Param([String[]]$choiceList[String]$Caption=“Please make a selection”[String]$Message=“Choices are presented below”[int]$default=)
$choicedesc 
New-Object System.Collections.ObjectModel.Collection[System.Management.Automation.Host.ChoiceDescription]
$choiceList foreach $choicedesc.Add((New-Object “System.Management.Automation.Host.ChoiceDescription” ArgumentList $_))}
$Host.ui.PromptForChoice($caption$message$choicedesc$default)
}

#Present the choice to the user
Switch (select-item Caption “Set HostServer Logging” Message “Do you want to: ” choice “&Enable”,“&Disable”,“&Cancel” default 0)
{
{ Enable-FileLogging }
{ Disable-FileLogging }
}


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s