Go through MSMQ one message at a time in powershell - powershell

I am currently trying to write a script that will go through a message queue and delete/send to another subqueue each message if a certain string is in the body.
In Pseudo code I am currently trying to implement
$queue = 'My Queue'
foreach($message in $queue)
{
if ($message.body.Contains("matchstring")
{
$message.delete OR $message.movequeue
}
}
I am currently using [Reflection.Assembly]::LoadWithPartialName("System.Messaging") in powershell but I am not sure if it contains all the functionality I need.

You can do this in PowerShell. Here's some simple code to get you started and connected to a queue:
$queuePath = ("Direct=OS:SERVER\private$\PRIVATEQUEUE")
$queue = New-Object System.Messaging.MessageQueue $queuePath
$queue.GetAllMessages()
Use $queue | gm and the MSDN to get you where you need to be.

Related

Using SendAndReceive() with ImapX

I am trying to use the ImapX DLL effectively. I have got $client.Folders.Inbox.Search() and $client.Folders.Inbox.Messages.Download() to work for downloading messages but they're very slow. I was looking into using $client.SendAndReceive() instead. I thought it would work like Outlook's send and receive but it wants a lot of parameters I don't understand.
This is the method definition:
bool SendAndReceive(string command, [ref] System.Collections.Generic.IList[string] data, ImapX.Parsing.CommandProcessor processor, System.Text.Encoding encoding, bool pushResultToDatadespiteProcessor)
I don't know what any of the parameters are for. I searched online for documentation for ImapX, but I couldn't find anything helpful.
Is SendAndReceive() a good method for downloading messages? If so, how do I use it?
This is my PowerShell code so far:
$DLL = [Reflection.Assembly]::LoadFile(“$PSScriptRoot\DLL\ImapX.dll”)
$client = New-Object ImapX.ImapClient
$client.IsDebug = $true
$client.ValidateServerCertificate = $true
$client.ThrowConnectExceptions = $true
$client.UseSsl = $false
$client.Behavior.MessageFetchMode = "Full"
$client.Host = $mailHost
$client.Port = $Port
$client.Connect()
$client.Login($user,$password)
# Downloads all messages but very slow
$client.Folders.Inbox.Search("ALL", $client.Behavior.MessageFetchMode)
$client.Folders.Inbox.Messages.Download()

PowerShell CloseHandle on EventWaitHandle

I have two PowerShell scripts. One of them has to wait at the other at one point. Here are the relevant parts:
WaitingScript.ps1:
$StopEventName = 'MyEvent'
function Wait-StopEvent {
$EventResetModeManualReset = 1
$StopEventObject = New-Object -TypeName System.Threading.EventWaitHandle -ArgumentList $false, $EventResetModeManualReset, $StopEventName
$StopEventObject.WaitOne()
}
SignallingScript.ps1:
$StopEventName = 'MyEvent'
function Signal-StopEvent {
$StopEventObject = [System.Threading.EventWaitHandle]::OpenExisting( $StopEventName )
$StopEventObject.Set()
}
It works well, I'm just not sure if I should call something like CloseHandle, or Close on $StopEventObject in either script.
Yes - at least I don't see a reason why you should not close the handle - otherwise the resources used by the handle would not be released. See WaitHandle.Close at Microsoft

Return output from Powershell script to UIPath using Invoke power shell

I am trying to get a value from an input box from a Powershell script back to a UI Path Sequence. I have created a simple sequence as an example. Yes, I know I could do this all in UI Path, I am just using an easy example as a way to try and test this for future use cases. Here is my sequence:
My text file from "Read text file" is:
$test = "Desktop/PassingArgs2of2.ps1 -Message foo"
Invoke-Expression -Command $test
The activity in UiPath looks like so:
The psCmd that I am running in the Invoke power shell activity looks like this:
Param(
[parameter(Mandatory=$true)]
[string]
$Message)
try{
$Global:fooVar = $null
function Test-InputBox(){
[void][Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
$msg = "fooMsg"
$title = "fooTitle"
$localtest = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title)
$Global:fooVar = $localtest.ToString()
}
Test-InputBox
}
catch{}
I tried setting fooVar equal to testLocal in the PowerShellVariables within Invoke power shell and then writing it, but that did not work.
Basically I want to get fooVar back into UI Path. Any help would be greatly appreciated. Thank you!
You're almost there. First, your Powershell script has to return a value. Take this for example:
[void][Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
$title = 'Your title goes here'
$msg = 'Your favorite color:'
$text = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title)
return $text
Here's the script in action (note that I called it twice and provided "red" the first time:
Then, just use this script directly in the Invoke Powershell activity. Note that the most important part here is the Output property - here, I decided to go for an array of strings. Naturally, as we only return a single value, you can just access the text provided by the user by accessing output(0).ToString().

Getting status of olMailItem visibility (Powershell)

I'm doing some scripting in PowerShell involving sending some mail automatically. I'm aware that the olMailItem object (on 2003 at least) has a couple of methods, Display() and Close() but is it possible to get the current visibility status?
If I run the following:
$Outlook = New-Object -ComObject Outlook.Application
$Mail1 = $Outlook.CreateItem(0)
$Mail1.To = $_.UserID
$Mail1.SentOnBehalfOfName = "me#mydomain.com"
$Mail1.Subject = $Subject1
$Mail1.Body = $BodyText1
$Mail1.Display()
$a = $Mail1
$Mail1.Close()
$b = $Mail1
I can't see any difference between $a and $b
What I was hoping for was a $Mail.IsVisible bool property or something.
Can it be done?
You can loop through the Application.Inspectors collection and compare each Inspector.CurrentItem.EntryId with MailItem.EntryId property of the item in question. If it is you who create the item and do not save it first, how can be opened in one of the Inspectors? You are the only one who can show it.

Pass ProcessParameters runtime (MSBuildArguments) using Powershell to tfs 2010 build definitions

I am executing builds using powershell script. I need to pass the process parameters run time based on the commandline arguments passed to the script. I am using TFS 2010.
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Build.Client")
$projectName = "test"
$buildName = "test.Build"
$tfsServer="xxx"
$tfsInstance = [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer($tfsServer)
$buildService = $tfs.GetService([Microsoft.TeamFoundation.Build.Client.IBuildServer])
$buildDefinations = $buildService.QueryBuildDefinitions($projName)
Loop through all the builds to find the one we are looking for
foreach ($build in $buildDefinations)
{
Get the name of this build instance
$bNameInstance = $build.Name
$ClientName = "test1" #default set in the builddefination is "/p:xxx=test"
#Get the process parameters. I need to update the default value. How can we process the dictionary and update the value
$bMSBuildArguments = $build.ProcessParameters
#Once setting is done."/p:xxx=test1"
$build.ProcessParameters = $bMSBuildArguments
[Void]$buildService.QueueBuild($build)
}
I need help in updating the processparameters using the powershell code. I came across the C# (http://blogs.msdn.com/b/jpricket/archive/2010/03/25/tfs2010-queuing-a-build-from-code-with-custom-process-parameter-values.aspx)solution but not able convert that to Powershell
The answer is in the blog post provided. Try something like this:
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Build.Workflow")
$request = $build.CreateBuildRequest()
$process = [Microsoft.TeamFoundation.Build.Workflow.WorkflowHelpers]::DeserializeProcessParameters($build.ProcessParameters)
#make changes to your process parameters in $process
$process.Item("asdf") = "new value"
$request.ProcessParameters = [Microsoft.TeamFoundation.Build.Workflow.WorkflowHelpers]::SerializeProcessParameters($process)
$buildService.QueueBuild($request)