none
Powershell: variable Where-Object not working as expected RRS feed

  • Question

  • Need documentation of some GPOs.

    To be flexible I pass arguments for Where-Object via command line to Powershell script.

    This code always returns all GPOs in domain:

    # get arguments
    PARAM (
     [string]$PathOutput = '`"c:\temp\gpotest`"',
     [string]$Objects = `$_.Displayname `-LIKE '*Test-GPO*' `-OR `$_.Displayname `-LIKE '*Dev-GPO*'
     )

    ...

    # create list of selected GPOs
    GET-GPO -ALL -Domain $Domain |
     Where-Object {
      $Objects
      } |
       Export-CSV -Path $Path -Delimiter ';'

    This code works well: only "my" GPOs are selected - (but I  hate hardcoding):

    GET-GPO -ALL -Domain $Domain |
     Where-Object {
      $_.Displayname -LIKE '*Test-GPO*' -OR $_.Displayname -LIKE '*Dev-GPO*'
      } |
       Export-CSV -Path $Path -Delimiter ';'

    No glue what's going wrong.
    Thanks for any hint.

    
    

    Tuesday, April 11, 2017 2:46 PM

Answers

  • True. But sometimes this can cause confusion when the user wants to expand variables within the string. But to your point, single quotes are probably more straightforward here.


    $sbString = '$_.DisplayName -like "Test GPO*"'
    Get-GPO -All | Where-Object ([Scriptblock]::Create($sbString))
    


    -- Bill Stewart [Bill_Stewart]


    Wednesday, April 12, 2017 4:50 PM
    Moderator

All replies

  • That will not work because $Objects contains a string rather than a scriptblock (Where-Object requires a scriptblock).

    You can dynamically pass a scriptblock like this:


    Get-GPO -All -Domain $Domain | Where-Object ([scriptblock]::Create($Objects))


    -- Bill Stewart [Bill_Stewart]

    Tuesday, April 11, 2017 4:29 PM
    Moderator
  • Thanks for the hint. But not working. Also tried some others including $executioncontext.invokecommand.NewScriptBlock($Objects).
    Checked type: is System.Management.Automation.ScriptBlock. Also not working.

    Now I'll try modifying my powershell script “on the fly” …

    Wednesday, April 12, 2017 11:13 AM
  • Your string is completely incorrect:

    [scriptblock[::Create('$_.Displayname  -LIKE "*Test-GPO*" -OR  $_.Displayname  -LIKE "*Dev-GPO*"')

    Forget about bactics in a string quoted with single quotes.  They do not work.

    Better -

    [scriptblock[::Create('$_.Displayname  -match "Test-GPO|Dev-GPO"')


    \_(ツ)_/


    • Edited by jrv Wednesday, April 12, 2017 11:55 AM
    Wednesday, April 12, 2017 11:54 AM
  • Thanks for the hint. But not working.

    I tested before posting, and yes it does work. Don't forget to escape your variables that are embedded within the string from which you intend to create the scriptblock. For example:


    $sbString = "`$_.DisplayName -like `"Test GPO*`""
    Get-GPO -All | Where-Object ([Scriptblock]::Create($sbString))
    

    Note how I escaped the $ and " characters in the string using the backquote (`). This is one way to embed these characters inside a double-quoted string.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, April 12, 2017 2:14 PM
    Moderator
  • No need for escaping if you use single quotes.

    $sb = [Scriptblock]::Create('$_.DisplayName -like "Test GPO*"')
    Get-GPO -All | Where-Object ($sb)


    \_(ツ)_/

    Wednesday, April 12, 2017 2:57 PM
  • True. But sometimes this can cause confusion when the user wants to expand variables within the string. But to your point, single quotes are probably more straightforward here.


    $sbString = '$_.DisplayName -like "Test GPO*"'
    Get-GPO -All | Where-Object ([Scriptblock]::Create($sbString))
    


    -- Bill Stewart [Bill_Stewart]


    Wednesday, April 12, 2017 4:50 PM
    Moderator
  • This will expand all variables.

    $sb = [Scriptblock]::Create('$_.DisplayName -notlike $var')


    \_(ツ)_/

    Wednesday, April 12, 2017 5:35 PM
  • In a script block the variables are always "expanded"

    D:\scripts>  $sb = [Scriptblock]::Create('$var')
    D:\scripts> $var = 'hello'
    D:\scripts> $sb.Invoke()
    hello
    D:\scripts>


    \_(ツ)_/

    Wednesday, April 12, 2017 5:36 PM
  • Right, I understand that. I was trying to say "variable expansion in the initial string."

    -- Bill Stewart [Bill_Stewart]

    Wednesday, April 12, 2017 7:53 PM
    Moderator