Search code examples
windowspowershellserver

Adding WSUS Computers with similar names to different TargetGroups


I am basically just filtering for computers with "wik" or "xwik" in their name like the following: Lets assume we have the servers: testwik.com and testxwik.com

Get-WsusServer | Get-WsusComputer -NameIncludes "wik" -ComputerTargetGroups "Unassigned Computers" | Add-WsusComputer -TargetGroupName "Friday_Patch"

Get-WsusServer | Get-WsusComputer -NameIncludes "xwik" -ComputerTargetGroups "Unassigned Computers" | Add-WsusComputer -TargetGroupName "DMZ_Patch"

Problem is, every Computer that has xwik in it's name obviously also has wik in it's name and after running the script all xwik computers would falsely be put into the "Friday_Patch" Target-Group and wouldn't be put in "DMZ_Patch" even tho its ran after the first prompt because it's not part of "Unassigned Computers" anymore. Does any one have an idea to solve this issue?

The -NameInCludes Parameter, to my understanding, acts like a wildcard, so "wik" is basically "* wik *" etc. but i have not found a way to exclude it? Maybe an if-Statement? But i cant figure it out properly, so I appreciate your help!


Solution

  • Use a simple if statement to add to one or the other:

    Get-WsusServer | Get-WsusComputer -NameIncludes "wik" -ComputerTargetGroups "Unassigned Computers" | ForEach-Object {
      if ($_.Computer -like '*xwik*'){
        $_ | Add-WsusComputer -TargetGroupName "DMZ_Patch"
      }
      else {
        $_ | Add-WsusComputer -TargetGroupName "Friday_Patch"
      }
    }