Configure MSDTC using PowerShell

As of blackpearl v4.6.7, MSDTC is a requirement to deploy workflows.

http://help.k2.com/helppages/k2blackpearlGettingStarted4.6.5/webframe.html?msdtc.html

You can use the following script to automate the MSDTC configuration during VM provisioning.

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

# ———————————
# Enable MSDTC for Network Access
# ———————————
Write-Host “Enabling MSDTC for Network Access…” foregroundcolor yellow
$System_OS=(Get-WmiObject class Win32_OperatingSystem).Caption
If ($System_OS match “2012 R2”)
    {
    Set-DtcNetworkSetting DtcName Local AuthenticationLevel Incoming InboundTransactionsEnabled OutboundTransactionsEnabled RemoteClientAccessEnabled confirm:$false
    }
Else
    {
    .\ConfigureMSDTC.ps1 Out-Null
    }
    Restart-Service MSDTC
Write-Host “——MSDTC has been configured—–” foregroundcolor green

The above script uses the inbuilt cmdlet if your OS is Windows Server 2012 R2, else it will use the traditional approach of modifying the registry.

# Save the following script as a separate file: ConfigureMSDTC.ps1
$DTCSecurity “Incoming”
$RegPath “HKLM:\SOFTWARE\Microsoft\MSDTC\”

#Set Security and MSDTC path
                $RegSecurityPath “$RegPath\Security”
                Set-ItemProperty path $RegSecurityPath name “NetworkDtcAccess” value 1
                Set-ItemProperty path $RegSecurityPath name “NetworkDtcAccessClients” value 1
                Set-ItemProperty path $RegSecurityPath name “NetworkDtcAccessTransactions” value 1
                Set-ItemProperty path $RegSecurityPath name “NetworkDtcAccessInbound” value 1
                Set-ItemProperty path $RegSecurityPath name “NetworkDtcAccessOutbound” value 1
                Set-ItemProperty path $RegSecurityPath name “LuTransactions” value 1             

                if ($DTCSecurity eq “None”)
                {
                    Set-ItemProperty path $RegPath name “TurnOffRpcSecurity” value 1
                    Set-ItemProperty path $RegPath name “AllowOnlySecureRpcCalls” value 0
                    Set-ItemProperty path $RegPath name “FallbackToUnsecureRPCIfNecessary” value 0
                }
                elseif ($DTCSecurity eq “Incoming”)
                {
                    Set-ItemProperty path $RegPath name “TurnOffRpcSecurity” value 0
                    Set-ItemProperty path $RegPath name “AllowOnlySecureRpcCalls” value 0
                    Set-ItemProperty path $RegPath name “FallbackToUnsecureRPCIfNecessary” value 1
                }
                else
                {
                    Set-ItemProperty path $RegPath name “TurnOffRpcSecurity” value 0
                    Set-ItemProperty path $RegPath name “AllowOnlySecureRpcCalls” value 1
                    Set-ItemProperty path $RegPath name “FallbackToUnsecureRPCIfNecessary” value 0
                }

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 }
}


How to export Exchange 2007 mailbox to a PST file

What is a PST file?

Taken from Wikipedia: a Personal Storage Table (.pst) is a file used to store local copies of messages, calendar events, and other items within Microsoft software such as Microsoft Exchange Client, Windows Messaging, and Microsoft Outlook.

If an employee leaves the company and you want to export the mailbox to a PST file and store it as a backup, it is very easy to do this in Exchange 2007.

Pre-requisites:

  • Remote computer that has Outlook 2007 installed. This is a must. Don’t install Outlook on the Exchange server itself
  • Exchange Management Shell installed from Exchange 2007 media
  • Windows Power Shell installed

Login to the remote computer with your domain credentials. Note that the domain credentials that you use to login must have FULLACCESS permission on the mailbox you are exporting. If not, this will fail.

If you want to give yourself FullAccess permission on a mailbox, you can run the following script from either PowerGUI or Exchange Management Shell. Change the script according to your requirements.

Add-MailboxPermission -Identity “John Crawford” -User “Contoso\Administrator” -AccessRights FullAccess

After the permission has been added, open up the Exchange Management Shell (EWS) on the remote computer and run this script.

Export-Mailbox -Identity john.crawford@sujeeth.net -PSTFolderPath C:\PSTBackup\John_Crawford.pst

You will get a confirmation prompt:

After pressing Y, the exporting starts

You can remove the FullAccess permission that you added earlier as follows:

Remove-MailboxPermission -Identity “John Crawford” -User “Contoso\Administrator” -AccessRights FullAccess

You can also set filters and actions to be performed while exporting. Some examples as follows:

Get-User john | Export-Mailbox -SenderKeywords info@sujeeth.net -DeleteContent

Get-User john | Export-Mailbox -TargetMailbox sujeeth -TargetFolder reports -SubjectKeywords “laser eye” -DeleteContent

Get-User john | Export-Mailbox -PSTFolderPath C:\PST_Backup\john_crawford.pst -SubjectKeywords “laser eye” -DeleteContent

Get-User john | Export-Mailbox -TargetMailbox sujeeth -TargetFolder MessageCleanup -SenderKeywords info@sujeeth.net -DeleteContent -MaxThreads 10