Kinook Software Forum

Go Back   Kinook Software Forum > Visual Build Professional > [VBP] Third Party Tools
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 06-23-2015, 01:04 PM
mevans mevans is offline
Registered User
 
Join Date: 04-21-2015
Posts: 32
Strange PowerShell behavior with arguments

When I pass command-line arguments to PowerShell, I can see them if I do this:

foreach ($i in $args)
{Write-Host $i}

And $args.count returns the correct number.

However, whenever I try to access a specific arg, I always get an empty value. For example:
Write-Host $args[0]
Write-Host $args[1]

This always generates nothing. Even looking at various posts online, I can see maybe I needed to do it like this:

Write-Host $($args[0])
Write-Host $($args[1])

But that still returns nothing.

I finally found one notation that works:

param(
$a,
$b
)

Write-Host $a
Write-Host $b

What I don't understand is why I can't access the values from $args[0], even though I can iterate through the array and get the values?
Reply With Quote
  #2  
Old 06-23-2015, 06:31 PM
kinook kinook is online now
Administrator
 
Join Date: 03-06-2001
Location: Colorado
Posts: 6,034
[ and ] are special characters in Visual Build step properties--you need to double them to escape (i.e., $args[[0]]).
http://www.kinook.com/VisBuildPro/Ma...ershelltab.htm
Reply With Quote
  #3  
Old 06-24-2015, 04:18 PM
mevans mevans is offline
Registered User
 
Join Date: 04-21-2015
Posts: 32
Ahhh...thank you. I doubled them elsewhere in the script and totally missed this.
Reply With Quote
Reply

Tags
powershell


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



All times are GMT -5. The time now is 11:11 AM.


Copyright © 1999-2023 Kinook Software, Inc.