Creating & Passing Enums in Powershell

To create an enumeration in powershell:

$msgTypeEnum = " 
namespace MessageTypes 
{ 
	public enum MessageType 
	{ 
		NEW_JOB = 4, 
		FAILED_JOB = 5, 
		JOB_REMOVED = 6, 
		JOB_SCHED_CHANGE = 7, 
		SERV_NOT_REACHABLE = 8, 
		CLUST_FAILOVER = 9, 
		LOW_DRIVE_SPACE = 10,
		LONG_RUNNING_JOB = 11
	} 
} 
" 
Add-Type -TypeDefinition $msgTypeEnum -Language CSharpVersion3

To pass said enumeration to a function (notice the parentheses around the enumeration value):

SendMessage $server.ServerName ([MessageTypes.MessageType]::LONG_RUNNING_JOB) "Long Running Job Alert" $msg

And, in the function (if you’re inserting the value into sql server) you’ll need to convert it to an int:

function SendMessage([string]$ServerName, [MessageTypes.MessageType]$MessageTypeID, [string]$Subject, [string]$Message)
{
	#Write-Host "EXEC dbo.usp_AddMessage @ServerName='$ServerName', @MessageTypeID=$MessageTypeID, @Subject='$Subject', @Message='$Message'"
	$nMsgType = [int]$MessageTypeID
	SaveData "EXEC dbo.usp_AddMessage @ServerName='$ServerName', @MessageTypeID=$nMsgType, @Subject='$Subject', @Message='$Message'"
}
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

%d bloggers like this: