locked
Powershell and get-mailbox filter on whenChanged RRS feed

  • Question

  • Hi,

    I've an interessting case with exchange powershell.

    I try to filter the mailboxes based on the whenchanged property.

    If I try via:

    $date = (get-date).addhours(-1)

    get-mailbox -filter {whenchanged -gt $date}

    then I recieve a lot of objects, even those they arent changed in the given timeframe.

    But if I try on the other way:

    Get-mailbox  | where-object {$_.whenChanged -gt (Get-Date).AddHours(-1)}

    I receive the right objects. Does anybody knows, why teh first script does not working proper?

    Thx,

    Akos

    Tuesday, October 9, 2012 3:35 PM

Answers

  • Try this:

    $date = (get-date).addhours(-1)

    $sb = [scriptblock]::create("whenchanged -gt '$date'") get-mailbox -filter $sb



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

    Tuesday, October 9, 2012 3:59 PM
  • I believe the (constrained) nature of the remote session is going to disallow creating any variable in that session.  If you connect to one of those remote sessions and then enter that session and try to create a variable there, you get:

     

     System.Management.Automation.RemoteException: Assignment statements are not allowed in restricted language mode or a Data section.

     After doing some testing, this also works:

    $date = (get-date).addhours(-1)
    get-mailbox -filter {whenchanged -gt $date}.getnewclosure()

     

    You can get a "one-liner" using the [scriptblock]::create() method, but I don't think it's worth it.

    get-mailbox -filter ([scriptblock]::create("whenchanged -gt '$((get-date).adddays(-1))'"))


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

    Wednesday, October 10, 2012 12:33 PM

All replies

  • Firstly, I always use the 2nd format.

    For the 1st format, the -filter carries a string value. I guess it probably does not work the way you expected.

    • Edited by Li Zhen Tuesday, October 9, 2012 3:57 PM
    Tuesday, October 9, 2012 3:57 PM
  • Try this:

    $date = (get-date).addhours(-1)

    $sb = [scriptblock]::create("whenchanged -gt '$date'") get-mailbox -filter $sb



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

    Tuesday, October 9, 2012 3:59 PM
  • Hi

    mjolinor's answer is right, I have tested, I think it is due to the format problem

    Cheers


    Zi Feng

    TechNet Community Support

    Wednesday, October 10, 2012 8:32 AM
    Moderator
  • I believe the problem is that the filter script is passed to the remote session without doing local variable expansion.

     

      You see the same kind of issue with the script block of invoke-command when you use it to run the script block on a remote computer or as a background job, but there you have an -argumentlist parameter that you can use to pass values from local variables to the script block.

     

      The proposed solution simply creates a new script block using the local variable in an expandable string, and the variable expansion happens there when the script block is created.


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

    Wednesday, October 10, 2012 11:08 AM
  • Hi,


    I think mjolinor has right. Is there any possibility to assign a variable to the remote session ?

    The following scripts is working:

    $date = (get-date).addhours(-1)

    $sb = [scriptblock]::create("whenchanged -gt '$date'") get-mailbox -filter $sb

    And I have a further question, is there any possibility to solve this as a one-liner script?

    Cheers,

    Akos



    Wednesday, October 10, 2012 11:58 AM
  • I believe the (constrained) nature of the remote session is going to disallow creating any variable in that session.  If you connect to one of those remote sessions and then enter that session and try to create a variable there, you get:

     

     System.Management.Automation.RemoteException: Assignment statements are not allowed in restricted language mode or a Data section.

     After doing some testing, this also works:

    $date = (get-date).addhours(-1)
    get-mailbox -filter {whenchanged -gt $date}.getnewclosure()

     

    You can get a "one-liner" using the [scriptblock]::create() method, but I don't think it's worth it.

    get-mailbox -filter ([scriptblock]::create("whenchanged -gt '$((get-date).adddays(-1))'"))


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

    Wednesday, October 10, 2012 12:33 PM