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.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 8 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
PsCustomObject
Posts: 137
Last visit: Thu Mar 28, 2024 3:34 am

Pass argument Exchange Management Shell command

Post by PsCustomObject »

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
  1. Get-MessageTrackingLog : A positional parameter cannot be found that accepts argument '-Recipients
  2. '.
  3. At line:75 char:3
  4. +         Get-MessageTrackingLog $argumen1
  5. +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  6.     + CategoryInfo          : InvalidArgument: (:) [Get-MessageTrackingLog], ParameterBindingExcep
  7.    tion
  8.     + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.Exchange.Management.TransportL
  9.    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:
  1. [System.String]$argumen1 = "-Recipients "
  2. et-MessageTrackingLog $argumen1
But if I pass the argument directly as part of the command everything works like this
  1. 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 238 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Pass argument Exchange Management Shell command

Post by jvierra »

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
PsCustomObject
Posts: 137
Last visit: Thu Mar 28, 2024 3:34 am

Re: Pass argument Exchange Management Shell command

Post by PsCustomObject »

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 ;-)
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Pass argument Exchange Management Shell command

Post by jvierra »

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
PsCustomObject
Posts: 137
Last visit: Thu Mar 28, 2024 3:34 am

Re: Pass argument Exchange Management Shell command

Post by PsCustomObject »

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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Pass argument Exchange Management Shell command

Post by jvierra »

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.
  1. $mysplat=@{
  2.      Param1=$value1
  3.      Param2=$value2
  4.      SwithParm=$true
  5. }
  6.  
  7. $mysplat.Param3=$value3
  8. $mysplat.Remove('Param2'')
  9.  
  10. Some-Cmdlet @mysplat
This topic is 8 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked