Windows Vista Forums

Invoke-Expression Blues.

  1. #1


    Kryten Guest

    Invoke-Expression Blues.

    Hi,

    I'm using Powershell to generate MBSAcli scan strings. Been working
    great forever, till today!

    Turns out that a username on a particular box was as shown
    below...this was causing
    Invoke-Expression to crash out with a "cannot index into a null array"
    error.

    I mean, what were the chances of someone picking a username the same
    as a powershell array?

    Set-Location "C:\Pro* Fil*\Micro*Bas*"
    [string]$username = '$User[5]'
    [string]$password = 'mypassword'
    $path = "C:\test.txt"
    $target = "myhost"
    $con = ".\mbsacli /target $target /u $username /p $password /nd /n "
    "IIS+OS+SQL" " /catalog " "C:\MBSACatalog\wsusscn2.cab" " > $path"

    Invoke-Expression $con

    Seems like the Powershell parser is interpreting $user[5] as an array,
    not a simple string value while execute the invoke-expression cmdlet
    and trying to index into it.

    Would be grateful for any suggestions for how to deal with this in an
    elegant way.

    Thanks,
    Stuart

      My System SpecsSystem Spec

  2. #2


    Kryten Guest

    Re: Invoke-Expression Blues.

    Thats it! Superb. Thanks Jaykul, it never occurred to me that
    the invoke-expression cmdlet was also unwrapping. Must watch out
    for that.

    Many thanks,
    Stuart



    On 3 Nov, 13:32, "Jaykul" <Jay...@xxxxxx> wrote:

    > It's because your variable actually gets unwrapped when you put it into
    > $con, and then again by Invoke-Expression. *You should be able to avoidthe
    > first unwrapping by single-quoting it:
    >
    > $con = '.\mbsacli /target $target /u $username /p $password /nd /n '
    >
    > --
    > Joel "Jaykul" Bennetthttp://HuddledMasses.org/
    > qotd: However, never daunted, I will cope with adversity in my traditional
    > manner ... sulking and nausea. -- Tom K. Ryan
    >
    > "Kryten" <Kryte...@xxxxxx> wrote in message
    >
    > news:634dd4cc-83a2-42d1-9cb5-f39ab5eeb531@xxxxxx
    >
    >
    >

    > > Hi,
    >

    > > I'm using Powershell to generate MBSAcli scan strings. Been working
    > > great forever, till today!
    >

    > > Turns out that a username on a particular box was as shown
    > > below...this was causing
    > > Invoke-Expression to crash out with a "cannot index into a null array"
    > > error.
    >

    > > I mean, what were the chances of someone picking a username the same
    > > as a powershell array?
    >

    > > Set-Location "C:\Pro* Fil*\Micro*Bas*"
    > > [string]$username = '$User[5]'
    > > [string]$password = 'mypassword'
    > > $path = "C:\test.txt"
    > > $target = "myhost"
    > > $con = ".\mbsacli /target $target /u $username /p $password /nd /n "
    > > "IIS+OS+SQL" " /catalog " "C:\MBSACatalog\wsusscn2.cab" " > $path"
    >

    > > Invoke-Expression $con
    >

    > > Seems like the Powershell parser is interpreting $user[5] as an array,
    > > not a simple string value while execute the invoke-expression cmdlet
    > > and trying to index into it.
    >

    > > Would be grateful for any suggestions for how to deal with this in an
    > > elegant way.
    >

    > > Thanks,
    > > Stuart- Hide quoted text -
    >
    > - Show quoted text -

      My System SpecsSystem Spec

Invoke-Expression Blues.

Similar Threads
Thread Forum
invoke-expression problem
People I'm having a problem making a string from a number of variables (I've simplified the stuff below) then executing it, and have tried a...
PowerShell
problem with invoke-expression
trying to open some documents, but some of the filenames have spaces in them, and i'm not able to get invoke-expression to properly read it. I've...
PowerShell
CTP: Invoke-Expression error
Hi I'm trying to run remote commands on a computer named "spare". Attached the error description. Both machines (source and destination) runs...
PowerShell
Invoke-expression vs &
What's the difference? For example, $t='notepad c:\temp\x.txt' &$t # this fails invoke-expression $t #this works More generally, I'm...
PowerShell
Invoke-expression help for logparser
Hi, I have been using logparser in windows command prompt. I thought to simplify my logparser scripts using powershell, But I have not been able...
PowerShell
invoke-expression not appending
Hi, I have a situation where when I use invoke-expression, I cannot output to a file. It just outputs to the console but when I run the command,...
PowerShell
Issue: Invoke-Expression with $args in the expression
The following result of Invoke-Expression with $args in the expression seems unexpected: CODE: function fn1 { Write-Host fn1:, $($args +...
PowerShell