Create variable -like from split CSS file

This topic contains 2 replies, has 2 voices, and was last updated by  Chris Cass 5 months, 1 week ago.

  • Author
    Posts
  • #70540

    Chris Cass
    Participant

    I have a web based application that has a file called login.css. Like any .css file, it's an extremely long, single line of text. I've found I can split it by ('}') to return every single value as a single line. My problem is that those values can be different, or in a different order, depending on the version of the software. I know, if all things were equal, I could create my variable like:

    $v1, $v2, $v3 = $logincss.split('}')

    But all things are not equal... I'm currently trying to turn a result of my split containing *loginsplashscreenlogo* into variable. I've tried:

    $loginCSS = gc $filepath
    $logintest = $logincss.split('}')
    get-item $logintest | where-object -like '*loginsplashscreenlogo*'
    

    and gotten:

    Get-Item : Cannot bind argument to parameter 'Path' because it is an empty string.
    At line:3 char:10
    + get-item $logintest | where-object -like '*loginsplashscreenlogo*'
    + ~~~~~~~~~~
    + CategoryInfo : InvalidData: (:) [Get-Item], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.GetItemCommand

    I've tried:

    $loginCSS = gc $filepath
    $logintest = $logincss.split('}') | where-object -like '*loginsplashscreenlogo*'
    

    and gotten:

    where-object : The specified operator requires both the -Property and -Value parameters. Provide values for both parameters, and then try the
    command again.
    At line:2 char:37
    + ... = $logincss.split('}') | where-object -like '*loginsplashscreenlogo*'
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (:) [Where-Object], PSArgumentException
    + FullyQualifiedErrorId : ValueNotSpecifiedForWhereObject,Microsoft.PowerShell.Commands.WhereObjectCommand

    I figure there's got to be a fairly simple way to do this, but I haven't been able to figure it out... Anyone have an idea?

  • #70543

    Don Jones
    Keymaster

    This:

    where-object : The specified operator requires both the -Property and -Value parameters. Provide values for both parameters, and then try the

    Is because of this:

    where-object -like '*loginsplashscreenlogo*'

    You're using Where-Object incorrectly. I'd suggest using the "real" syntax.

    where-object { $_ -like '*loginsplashscreenlogo*' }

    Which is probably what you meant. For your Get-Item problem – I've truly no idea what you're attempting to do with Get-Item, there, and I suspect PowerShell doesn't either.

    I think your last example is on the right track, you're just doing it wrong. You were assuming that Where-Object would automatically compare the incoming pipeline object, but it doesn't. It wants to compare PROPERTIES. In your case, because you're passing in a simple string, you need to use the syntax I've suggested, which will let you refer to the object itself in the comparison, rather than a property of the object. That "simplified" Where-Object syntax always gets people into trouble. It's a bit of a hack, actually.

    • #70544

      Chris Cass
      Participant

      Thank you sir! I wasn't sure what I was doing at all, so the get-item was just a "hey, let's see if this might work" kind of thing. Your suggestion worked.

You must be logged in to reply to this topic.