1

I'm trying to use param within Powershell 5 My script is as following:

param(
[parameter(mandatory=$true)]
[string]$IP,

[parameter(mandatory=$true)]
[string]$Time
)
Write-Host $IP
Write-Host $Time

Suppose the script is named test.ps1, I tried running it in 2 ways:

.\test.ps1 -IP 10.10.10.10 -Time 10:10
.\test.ps1 -IP "10.10.10.10" -Time "10:10"

None of these work. Instead, it prompts me to enter a value to IP. The error is as following:

cmdlet test.ps1 at command pipeline position 1
Supply values for the following parameters:
IP:

Obviously this little script is just a phase in integrating a piece of code into a bigger script system. Help would be appreciated, after hours of searching I don't see what I've done wrong.

2
  • 2
    Works fine for me, copying and pasting your script: i.imgur.com/1NKn9AL.png are you running it from a command prompt or on a remote machine or anything? What's the bigger system? Commented Nov 2, 2016 at 19:30
  • Can confirm that it is working for me as well. Can you maybe post some screenshots of how your calling the code and what your seeing? Commented Nov 3, 2016 at 15:54

1 Answer 1

3

I can confirm this runs on my computer. I had to switch directories to the (ex: cd (directory where script is located). directory where the .ps1 script is located. I ran this from the Windows command window.

powershell.exe .\test.ps1 -IP "10.10.10.10" -Time "10:10"

This should work as well.

powershell -File C:\somedirectory\test.ps1 -IP "10.10.10.10" -Time "10:10"

Can kick this off via a .bat file as well. Such as test.bat

In command prompt C:\>test.bat "10.10.10.10" "10:10"

Contents of .bat file below.

@ECHO OFF
PowerShell -File C:\somedirectory\test.ps1 -IP %1 -Time %2

Not the answer you're looking for? Browse other questions tagged or ask your own question.