Support Forums

Pass argument Exchange Management Shell command

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.
User avatar
Helocheck
Posts: 29
Joined: Tue Apr 29, 2014 1:55 am

Pass argument Exchange Management Shell command

Postby Helocheck » Fri Feb 12, 2016 10:03 am

Hello all,

I know I will win some prize like the newbie of the year but I need again your help. I have a form which will (at least should) be used in place of the now gone Exchange message tracking GUI.

My issue is that when I try to pass arguments to the Get-MessageTrackingLog I get the following error

Get-MessageTrackingLog : A positional parameter cannot be found that accepts argument '-Recipients 
'
.
At line:75 char:3
+ Get-MessageTrackingLog $argumen1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-MessageTrackingLog], ParameterBindingExcep
tion
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.Exchange.Management.TransportL
ogSearchTasks.GetMessageTrackingLog


I have a ttached a demo form so you can have a better idea of what I am doing, the problem seems to arise when I pass the argument to the command from a variable something like this:

[System.String]$argumen1 = "-Recipients "
et-MessageTrackingLog $argumen1


But if I pass the argument directly as part of the command everything works like this

Get-MessageTrackingLog -Recipients $mbx


My main problem I have multiple textboxes in the form and I build the command depending on what the user has selected so would need a way to pass arguments via variables.

Knowing myself I know I'm overlooking something really stupid but cannot find myself, any pointer?

As usual thanks.
Attachments
DEmoForm.psf
(18.15 KiB) Downloaded 94 times
User avatar
jvierra
Posts: 10026
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Pass argument Exchange Management Shell command

Postby jvierra » Fri Feb 12, 2016 11:48 am

Hi Helpcheck. We all have to be newbies at some point

Start by reviewing how the CmdLet works. I would also review the basics of how PowerShell CmdLets are designed.

"-Recipients" is a named argument tag. It is NOT an argument. We call the items received be a function or method Parameters. When we send the parameters the values sent are referred to as arguments.

We cannot send the Parameters tag names as string variables. They must be sent as defined.

You will have to build the command up incrementally depending on what you form is intending to do. The easiest way to do this is in the controls validated event. You can use splatting to accomplish this more transparently.

https://technet.microsoft.com/en-us/lib ... 72955.aspx
User avatar
Helocheck
Posts: 29
Joined: Tue Apr 29, 2014 1:55 am

Re: Pass argument Exchange Management Shell command

Postby Helocheck » Fri Feb 12, 2016 12:50 pm

Ahaha so true J, just sometimes I feel the eternal newbie. Sooner or later I'll send you a PM asking for an address where I can send you compoensation for all the replies and help you're giving me.

Now that I am starting to build GUI and somehow adding complexity to my scripts I am realizing how much I still have to learn which is great as keeps motivating me.

Many thanks for the article which I'm going to read and hopefully fix the script.

Until next question ;-)
User avatar
jvierra
Posts: 10026
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Pass argument Exchange Management Shell command

Postby jvierra » Fri Feb 12, 2016 12:59 pm

No compensation necessary just keep having fun.

I am sorry that there are no books r foundation articles on using Forms with PowerShell. The C# example code is close but not exact and may be hard to understand if you are not a programmer. You can and should spend time looking at as many good examples of PowerShell scripting as you can find. Read the major blogs. All will become clear in time.

Good luck.
User avatar
Helocheck
Posts: 29
Joined: Tue Apr 29, 2014 1:55 am

Re: Pass argument Exchange Management Shell command

Postby Helocheck » Fri Feb 12, 2016 1:56 pm

You know coming from a Unix background (long story) sometimes I forget PowerShell/.Net is not the same, what I mean in the *nix world $arg1 is just taht you don't worry too much about the type etc. with all the limitation of the case.

As sometimes life goes I found myself deep into Microosft stuff and some old habits are hard to die, my original question is a good example of this, and not having good foundation on PowerShell and GUI does not help even if C# examples are not taht bad in the end.

Well enough talking have some reading and fixing to do which all good fun for a rainy Friday evening.

L.
User avatar
jvierra
Posts: 10026
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Pass argument Exchange Management Shell command

Postby jvierra » Fri Feb 12, 2016 2:14 pm

Well most of PowerShell was copied from Korn shell and Bash as well as most of C++ (C# flavor). I was running Unix before Windows was thought of. We ran it on a VAX as a partition under VMS.

Unix arguments/parameters are the same. Actually the structures come from how early C passed arguments to a program and how the early command parsers liked to see the command arguments.

There are ways to stringify a list of arguments and parameter names. THeeasy way is to use a @splat. It can be added to and subtracted from without being concerned about order or nulls.

$mysplat=@{
Param1=$value1
Param2=$value2
SwithParm=$true
}
 
$mysplat.Param3=$value3
$mysplat.Remove('Param2'')
 
Some-Cmdlet @mysplat

Return to “PowerShell GUIs”

Who is online

Users browsing this forum: No registered users and 1 guest