4

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.

C:\Users\UserName\Desktop\Test.ps1 -ADusername "Hello World"

Here is the sample script:

Param([string]$adusername)
$adusername
pause

I am Expecting the following output:

Hello World
Press Enter to continue...:

This is the actual output:

Press Enter to continue...:

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

5
  • 1
    Where are you running that script from? It works from powershell for me. (Though I'm not using powershell 5.0 yet.) Commented May 6, 2016 at 22:34
  • @EtanReisner it seems to work fine for me on 2, as well. I have found calling params to be an odd affair without calling PowerShell first, and often times ordered $args[x] seems the easiest route (especially for one arg)... Commented May 6, 2016 at 22:37
  • @AustinFrench What do you mean "without calling powershell first"? You mean from trying to run a powershell script from cmd.exe or something directly? Commented May 6, 2016 at 22:43
  • Script is currently on my desktop. but I'm running from run dialogue so as to emulate the command from the application i will be using. Running PS5 Commented May 6, 2016 at 22:53
  • @EtanReisner correct, such as from the run line, a shortcut, scheduled task etc. Commented May 9, 2016 at 14:10

3 Answers 3

8

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:

. .\scriptFile.ps1
function "Hello World"

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)

powershell c:\Path\Folder\Script.ps1 "Hello World!"

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

$adUserName = $args[0]
write-host $adUserName

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

Param([string]$ad)

Write-Host $args[0]
Write-Host $ad

Read-Host
pause

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

Sign up to request clarification or add additional context in comments.

4 Comments

This seems to only work intermittently. I will revisit this on Monday as i have to get my apartment ready to move. Here is my full script: pastebin.com/ktjpLQek
This worked only once so the proof of concept worked, i will mark this as answered. I'm just still at a loss as to why it isn't working for me.
@mttp1990 I am every bit as confused about a "it worked once"... Did you move the script file or anything? What are you execution policies?
Execution policy is unrestricted. I have a bunch of other scripts I use to speed my daily tasks along. Script hasn't moved locations. I'll try this in an isolated environment tomorrow and see if it is just my PC bugging out.
4

Add the following to the top of your script.

Param(
  [Parameter(Mandatory=$true)]
  [string]$Euser
)

Write-Host "Deactivating $EUser"

Calling example after cd to the script directory

.\ScriptName.ps1 -Euser "FOO" # Tab auto completion works

1 Comment

I i do this it prompts me to enter in the expected parameter withing the running script window, this tells me that no argument was passed to the script and because we listed the parameter as mandatory it seems to be expecting something.
0

The following in a new script works for me.

Param([string]$servers)
"You chose $servers"

PS C:\scripts> .\Untitled1.ps1 "this test"

You chose this test

PS C:\scripts>

6 Comments

So i replicated your script on my end and my output does not show the String Variable. I am only getting "You Chose"
Can you paste your script on pastebin.com and share it? That param notation works and I've got Powershell v5 installed. $PSVersionTable.PSVersion
Could you try following this Hey Scripting Guy article on parameter sets? This is for a function but it should work as described in the PowerShell ISE.
This did not seem to work either. I am able to pass an argument using this method from within the script and i have been able to do that for other projects in the past, something is just not letting the argument come through from outside of the powershell environment.
What is the type of object you intend to pass into a PowerShell script? I am very interested in figuring out why a simple script accepting a parameter isn't working in your environment. Being able to create and test scripts using parameters is vital, in my opinion.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.