header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Problem with $matches variable
Last Post 02 Mar 2010 06:30 AM by PoSherLife. 5 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
ymullerUser is Offline
New Member
New Member
Posts:2
Avatar

--
25 Feb 2010 03:01 AM
    Hello.

    I created this script:
    $location = "c:\"
    Get-Content c:\case\result.txt|`
     foreach {if ($_ -match "(?[a-z,A-Z,0-9,_]+)\s+([a-z,A-Z,0-9,_]+)\s+(?[a-z,A-Z,0-9,_]+)")`
    {$matches["server"] + "," + $matches["owner"]}}|`
     out-file $location\$matches["server"].txt -app

    and I receive the following error:
    Out-File : Cannot perform operation because the wildcard path c:\case\System.Collections.Hashtable[server].txt did not resolve to a file. At line:4 char:9 + out-file <<<< $location\$matches["server"].txt + CategoryInfo : OpenError: (c:\case\System....ble[server].txt:String) [Out-File], FileNotFoundException + FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.OutFileCommand

    If I don't use the $matches["server"] it works perfectly.

    I tried several ways around this but didn't make it.

    Please assist.

    Thanks in advance.
    George HowarthUser is Offline
    Basic Member
    Basic Member
    Posts:360
    Avatar

    --
    25 Feb 2010 04:06 AM
    Try putting the file name in quotes to expand the variables: 

    out-file "$location\$matches["server"].txt" -app

    (Edited my answer)
    glnsizeUser is Offline
    Basic Member
    Basic Member
    Posts:193

    --
    25 Feb 2010 05:49 AM
    I would suggest using where-object in place of nesting an if statement within foreach.  The result is not only cleaner, but also more performant as you're not passing every line down the pipeline.


    $location = "C:\" Get-Content c:\case\result.txt | Where-Object { $_ -match "(?[a-z,A-Z,0-9,_]+)\s+([a-z,A-Z,0-9,_]+)\s+(?[a-z,A-Z,0-9,_]+)"} | Out-File -FilePath (Join-Path -Path $location -ChildPath $matches["server"]) -Append ` -InputObject ("{0},{1}" -f $matches["server"], $matches["owner"]) ~Glenn
    SureshUser is Offline
    New Member
    New Member
    Posts:31
    Avatar

    --
    25 Feb 2010 10:14 PM
    Yes, what glnsize said is right. It's simple.

    For the exception you're getting, it's first expanding $matches and confused, so try this in that kind of situations:

    out-file $location\$($matches["server"]).txt -app
    ymullerUser is Offline
    New Member
    New Member
    Posts:2
    Avatar

    --
    02 Mar 2010 03:34 AM
    Hello.

    glinzie I tried your script and I received the following error:
    Out-File : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input an
    d its properties do not match any of the parameters that take pipeline input.
    At line:3 char:11
    + Out-File <<<< -FilePath (Join-Path -Path c:\case\ -ChildPath $matches["server"]) -Append `
    + CategoryInfo : InvalidArgument: (medbwspsp002 ... :PSObject) [Out-File], ParameterBindingException
    + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.OutFileCommand


    This is part of the file for which I'm reading the content:
    hfacit100 Windows_NetServer Uni_Admin
    hfacit101 Windows_NetServer Uni_Admin

    sureshbabutadisetty I tried the location as you mentioned and it still gives error.


    Thanks.
    PoSherLifeUser is Offline
    Basic Member
    Basic Member
    Posts:364
    Avatar

    --
    02 Mar 2010 06:30 AM
    remove -InputObject ("{0},{1}" -f $matches["server"], $matches["owner"]) and try again
    When at first you don't succeed Step-Into

    http://theposherlife.blogspot.com
    http://www.jandctravels.com

    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