Pass argument into Powershell

Question:

I have a powershell script that completes some tasks in Active Directory and MS Exchange. I need to pass in the Active Directory username from our call logging system. Having the Call log system pass the argument is simple.

The problem i am facing is having powershell read the argument into a variable.

I will provide a proof of concept example.

Here is a sample command passing the argument into powershell.

Here is the sample script:

I am Expecting the following output:

This is the actual output:

Just wrapping my head around this core concept will help me immensely. I was unable to find any examples or tutorials that worked when applied to my scenario. I apologize if this is a duplicate post, couldnt find anything on this site as well.

EDIT: per request, this is my full script: http://pastebin.com/ktjpLQek

Answer:

I think you will have much better luck if you avoid trying to use params and call the script exactly that way.

It is possible, but paramaters work better if you either inline the scriptfile like:

Staying closer to what you are doing however, you should be using $args and calling PowerShell (the exe directly)

If you call your scriptfile like: (I used the runbox)

and then replace your Param([string]$adusername) with:

Additionally, this should work for you (to dissect):

Call the script with the path,
powershell c:\Path\Folder\Script.ps1 "Hello World!" $ad = "JSmith"

If this does not work, you should ensure that your execution policy is set correctly. Get-ExecutionPolicy will tell you the current level. For testing you can set it very low with Set-ExecutionPolicy unrestricted

Source:

Pass argument into Powershell by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply