header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Command line arguments
Last Post 18 Mar 2010 06:07 AM by Petri. 2 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages Informative
PetriUser is Offline
New Member
New Member
Posts:5
Avatar

--
14 Mar 2010 10:56 PM
    Hi there,

    Why my .ps1 script like below:
         Write-host Test space and arguments: $args[0].

    Gives me result:
    Test space and arguments: ball .

    When I call it as:
       .\test.ps1 ball


    Why there is additional space after "ball" ??
    cameronoveUser is Offline
    Basic Member
    Basic Member
    Posts:332
    Avatar

    --
    15 Mar 2010 01:53 PM
    I don't use write-host anymore (I had a hard time predicting out put from time to time).  I'm using PowerShell v2 RTM.  Not sure how this works on v1.  But when I want to write to the console I just put what I want in between double-quotes.  So all Test.ps1 needs to have is the following (including the double-quotes):

    "Test space and arguments: $($args[0])."

    .\test.ps1 ball

    Should produce what you want at the console.  If you want the output printed to a file you could do:

    "Test space and arguments: $($args[0])." > filename.txt

    The greater-than sign redirects the output to the file.

    Now a bit of explanation:
    You can use either single-quotes or double-quotes when specifying a string.  However, double-quotes resolve variables.
    For example:
    $name = "Spot"
    $Sentence = "See $name run."

    This produces
    See Spot run.

    If, instead, you used single-quotes for the $Sentence variable:
    $Sentence = 'See $name run.'

    The output would be
    See $name run.

    Sometimes you want to access a noteproperty or even a statement that returns a string in a string:
    $Pet.breed = 'Terrier'
    $Pet.name = 'Spot'
    you could access these noteproperties but not "straight up"
    If you typed
    "$Pet.name is a $Pet.breed"
    it would return
    @{breed=Terrier; name=Spot}.name is a @{breed=Terrier; name=Spot}.breed

    To actually get the value you need to put the variable inside parentheses like so...

    "$($Pet.name) is a $($Pet.breed)"

    Anything inside $() will be resolved for example:

    "$($Pet.name) ran a race on $(get-date -f MM/dd/yyyy)."

    Would produce:
    Spot ran a race on 03/15/2010.

    Also anything in quotations on it' own line will be output to the console.  So you don't have to write-host to output to the console.

    PetriUser is Offline
    New Member
    New Member
    Posts:5
    Avatar

    --
    18 Mar 2010 06:07 AM
    Many thanks to you Cameron. The difference between double quota and singles were the answer. So this was enough:
    "Test space and arguments: $args[0]."

    --
    Petri
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer