none
using the notlike in powershell RRS feed

  • Question

  • I have written several similar scripts using the -contains and -Like with no issue. However I ha not trying to apply that using the -notlike it however does not give the desires response.

    if ($strUser.memberof -notlike "*NSeries*" -and  $strUser.scriptpath -like "*BAT*"  ) {write-output "$strUserName, is NOT a member of Nseries and DOES have a log on profile. Their current logon batch file is  $scriptP "} else {"This user does not meet the critera "}
    
    

    In the above example the goal is if the user is NOT a member of Nseries anything and does have a log on batch file it will tell me.

    It does only list the uses with a logon script so that works but it still list the users that are part of any Nseries group.

    I know I am overlooking something. 

    Thanks!


    R White

    Tuesday, December 23, 2014 8:38 PM

Answers

  • Okay.  Does this get you the results you want?

    Get-ADUser -SearchBase "OU=Adm,DC=East,DC=GA,DC=gov" -filter * -Properties memberof |
     where { (-not ($_.memberof -like "*NSeries*") }|
      select SamAccountName,MemberOf | Ft -autosize | out-string -width 8096 


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

    • Marked as answer by tnetplus Wednesday, December 24, 2014 2:45 PM
    Wednesday, December 24, 2014 1:30 PM
    Moderator

All replies

  • That only checks direct membership.  You have to use do recursive group membership. Check repository for examples.


    ¯\_(ツ)_/¯



    • Edited by jrv Tuesday, December 23, 2014 10:52 PM
    Tuesday, December 23, 2014 10:49 PM
  • This:

    $strUser.memberof -notlike "*NSeries*" 

    will be $true if the user belongs to any group that is not like '*NSeries*'

    This:

    ((-not ($strUser.memberof -like "*NSeries*"))

    will be $true if the user does not belong to any group that is like '*NSeries*'

    While at first glance they might seem like they would produce the same result, they do not.


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



    Tuesday, December 23, 2014 11:09 PM
    Moderator
  • Thanks so very much to both of you i will try them first thing tomorrow.  

    R White

    Wednesday, December 24, 2014 1:07 AM
  • Well i looked into both options.  

    JRV yours was a great call but we use no nesting. So direct membership is all i need for this script.

    Mjolinor this example (see below) was different so i had hopes for it.  I worked at it now or a couple of hours and i am so frustrated.  Pulling the -like was easy.  I have written several scripts where if the user was a group member of group1 and of group2 the script would act out a set of actions and it went so well.  I have not been hung up for a day on this.     I decided to scrp the one way and try a different still using the building blocks.  If i can get a list of user from a OU that are not members of any group with nseries in them it will work.   

    I have had a list in a text file to work against but i figured i would just pull the list right from AD still no luck.  Below is my latest attemt however please keep in mind you are seeing this only after the more logical attempts havefailed.  

    ((-not ($strUser.memberof -like "*NSeries*")) 

    Get-ADUser -SearchBase "OU=Adm,DC=East,DC=GA,DC=gov" -filter * -Properties memberof ((-not ($_.memberof -notlike "*NSeries*")) | select SamAccountName,MemberOf | Ft -autosize | out-string -width 8096 
    

    Thanks as always 


    R White


    • Edited by tnetplus Wednesday, December 24, 2014 1:20 PM
    Wednesday, December 24, 2014 1:19 PM
  • Okay.  Does this get you the results you want?

    Get-ADUser -SearchBase "OU=Adm,DC=East,DC=GA,DC=gov" -filter * -Properties memberof |
     where { (-not ($_.memberof -like "*NSeries*") }|
      select SamAccountName,MemberOf | Ft -autosize | out-string -width 8096 


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

    • Marked as answer by tnetplus Wednesday, December 24, 2014 2:45 PM
    Wednesday, December 24, 2014 1:30 PM
    Moderator
  • Okay.  Does this get you the results you want?

    Get-ADUser -SearchBase "OU=Adm,DC=East,DC=GA,DC=gov" -filter * -Properties memberof |
     where { (-not ($_.memberof -like "*NSeries*") }|
      select SamAccountName,MemberOf | Ft -autosize | out-string -width 8096 


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

    Genius --   Thanks so so much.    Why would -notlike not work?  starting this morning about 30 minutes ago or so  have been believing that i just could not use -notlike perhaps because  i was using framework 5.0 and was going to go back to 4.5. I am just ecstatic.     THANK YOU!!!!! 

    R White

    Wednesday, December 24, 2014 2:49 PM
  • I tried to explain that in my first post.

    $_.memberof -notlike "*NSeries*"

    will be $true if the user belongs to any group that is not an "NSeries" group.

    It can only be $false if the user belongs to ONLY "NSeries" groups, and no others.



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

    Wednesday, December 24, 2014 2:59 PM
    Moderator
  • I tried to explain that in my first post.

    $_.memberof -notlike "*NSeries*"

    will be $true if the user belongs to any group that is not an "NSeries" group.

    It can only be $false if the user belongs to ONLY "NSeries" groups, and no others.



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

    Man thanks.   I am slow more often then not.  You have no idea how happy i am this morning.  Thanks!


    R White

    Wednesday, December 24, 2014 3:38 PM