PowerShell, how to pass a variable as individual parts

Answered PowerShell, how to pass a variable as individual parts

  • Friday, February 08, 2013 9:50 PM
     
      Has Code

    Hi, 

    How can I make the following works:

    $logParams = '-noclobber -append'
    start-transcript -path ".\log.txt" $logParams
    

    So far I get 

    A positional parameter cannot be found that accepts argument '-noclobber -append'

    error. 

    Thanks

All Replies

  • Friday, February 08, 2013 9:55 PM
     
      Has Code

    This will work:

    $logParams=@{
         NoCLobber=$true
         Append=$true
         Path-".\log.txt"
    }
    start-transcript @logParams


    ¯\_(ツ)_/¯

  • Friday, February 08, 2013 10:07 PM
     
     

    thanks. That works. Now, 

    - how to write that $logParams=@ assignment in one line?

    - how to construct it gradually?

    $logParams=@{
         NoCLobber=$true
         Append=$true
    }

    $logParams+= @{Path=".\log.txt"}

    ?

    Thanks

  • Friday, February 08, 2013 10:14 PM
     
      Has Code

    There is no point to what you are doing.

    $logFile='.\log.txt'
    Start-Transcript @{
         NoCLobber=$true
         Append=$true
        Path=$logfile
    }
    OR ---
    Start-Transcript -NoCLobber -Append -Path $logfile
    # both are equivalent.


    ¯\_(ツ)_/¯

  • Friday, February 08, 2013 10:17 PM
    Moderator
     
     Answered Has Code
    That will work, provide that key isn't already present in the hash table (if it is, you'll get an error)  Also 
    $logparms[Path] = '.\log.txt'
    
    $logparms.Path = '.\log.txt'
    will work, and will replace the existing value if there is already a key by that name in the hash table.

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


  • Friday, February 08, 2013 10:33 PM
     
     

    There is no point to what you are doing.

    FYI, I want to warp it with my function 

    function MyLogging ($logFile, $logParams = '-noclobber -append')

    then call start-transcript from within MyLogging. 

  • Friday, February 08, 2013 10:37 PM
     
     

    There is no point to what you are doing.

    FYI, I want to warp it with my function 

    function MyLogging ($logFile, $logParams = '-noclobber -append')

    then call start-transcript from within MyLogging. 

    Why?

    What does it do that the regular calling methods cannot do?


    ¯\_(ツ)_/¯

  • Friday, February 08, 2013 10:57 PM
     
      Has Code
    That will work, provide that key isn't already present in the hash table (if it is, you'll get an error)  Also 
    $logparms[Path] = '.\log.txt'
    
    $logparms.Path = '.\log.txt'
    will work, and will replace the existing value if there is already a key by that name in the hash table.

    PS C:\temp> $logParams
    
    Name                           Value
    ----                           -----
    NoCLobber                      True
    Append                         True
    
    
    PS C:\temp> $logParams[Path] = '.\log.txt'
    At line:1 char:12
    + $logParams[Path] = '.\log.txt'
    +            ~
    Array index expression is missing or not valid.
    At line:1 char:12
    + $logParams[Path] = '.\log.txt'
    +            ~~~~~
    Unexpected token 'Path]' in expression or statement.
    
    
    

    The other one works. 

    Thanks