none
Ignore spaces in names powershell RRS feed

  • Question

  • I have created a script which checks in all Laptop OUs in AD and checks if those laptops are member of a particular group.

    If not it adds them to that group.

    The problem is script does not work with OU's which have space in name  for example in xyz.com domain there is one OU ABC DEF in which Laptops OU is there , so the members of this OU does not get added to group.

    How to ignore the space in name ?

    My code is as folows :

    #=========================================================================================
    import-module activedirectory
    $group=Get-ADGroup LaptosWIFI
    $FileName = "AddedToday" + $((get-date).tostring("MMddyyyy"))
    Get-ADOrganizationalUnit -Filter {Name -like '*laptop*'} | 
         ForEach-Object{
    	$comp = Get-ADComputer -Filter * -Searchbase "OU=Laptops,OU=XYZ ABC,DC=UAT,DC=ABCUAT,DC=COM"
        
         $string = $comp.name 
         $names= @()
         $names+= $string -split "\s+"
         foreach ($element in $names) {
         Write-Host $element
         Get-ADComputer $element -Properties MemberOf | %{if (-Not ($_.MemberOf -like "*LaptopsWIFI*")) {$element | Out-File -append E:\WiFiLaptopsAdded\$($FileName).txt }}
         Get-ADComputer $element -Properties MemberOf | %{if (-Not ($_.MemberOf -like "*LaptopsWIFI*")) {Add-ADGroupMember gIN-WIFI-Laptops -Members $_}} -ErrorAction SilentlyContinue
         }}

    in this code OU "XYZ ABC" is considered as different and i get following error when i run the script

     Get-ADComputer <<<<  $element -Properties MemberOf | %{if (-Not ($_.Memb
    erOf -like "*gIN-WIFI-Laptops*")) {Add-ADGroupMember gIN-WIFI-Laptops -Members
    $_}} -ErrorAction SilentlyContinue
        + CategoryInfo          : WriteError: (:) [Get-ADComputer], ParameterBindi
       ngException
        + FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.ActiveDirectory
       .Management.Commands.GetADComputer

    Get-ADComputer : Cannot bind parameter 'Identity' to the target. Exception sett
    ing "Identity": "Cannot validate argument on parameter: 'Identity'. The argumen
    t is null or empty. Supply an argument that is not null or empty and then try t
    he command again."
    At C:\Scripts\LaptopAdditionWIFI\LaptopAddition.ps1:23 char:20
    +      Get-ADComputer <<<<  $element -Properties MemberOf | %{if (-Not ($_.Memb
    erOf -like "*gIN-WIFI-Laptops*")) {$element | Out-File -append D:\WiFiLaptopsAd
    ded\$($FileName).txt }}
        + CategoryInfo          : WriteError: (:) [Get-ADComputer], ParameterBindi
       ngException
        + FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.ActiveDirectory
       .Management.Commands.GetADComputer


    • Edited by theAbyKumar Friday, November 28, 2014 12:34 PM
    • Moved by Bill_Stewart Tuesday, December 30, 2014 2:41 AM Unanswerable drive-by question
    Friday, November 28, 2014 12:33 PM

Answers

  • This is closer to what you might be trying to do:

    import-module activedirectory $fileName = "E:\WiFiLaptopsAdded\AddedToday$((get-date).tostring('MMddyyyy'))" Get-ADOrganizationalUnit -Filter {Name -like '*laptop*'} | ForEach-Object{ $computers=Get-ADComputer -Filter * -Searchbase $_ -Properties MemberOf foreach($computer in $computers){ if($computer.MemberOf -like "*LaptopsWIFI*"){ Write-Host "$($computer.Name) is already included" -ForegroundColor green }else{ Write-Host "Adding $($computer.Name) to group" -ForegroundColor green $computer.Name | Out-File -append $filename Add-ADGroupMember gIN-WIFI-Laptops -Members $computer } }
    }




    ¯\_(ツ)_/¯




    • Edited by jrv Friday, November 28, 2014 2:10 PM
    • Proposed as answer by jrv Saturday, November 29, 2014 4:43 AM
    • Marked as answer by Just KarlModerator Thursday, April 30, 2015 4:49 PM
    Friday, November 28, 2014 2:05 PM

All replies

  • Look this over very carefully.  Try to see why your code won't work.  I fixed a couple of things but I still cannot guess at what you are trying to do:

    import-module activedirectory
    $group=Get-ADGroup LaptosWIFI
    $FileName = "AddedToday" + $((get-date).tostring("MMddyyyy"))
    Get-ADOrganizationalUnit -Filter {Name -like '*laptop*'} | 
        ForEach-Object{
            Get-ADComputer -Filter * -Searchbase $_
            $string = $comp.name 
            $names= @()
            $names+= $string -split "\s+"
            foreach($element in $names){
                Write-Host $element
                Get-ADComputer $element -Properties MemberOf | 
                    %{
                        if (-Not ($_.MemberOf -like "*LaptopsWIFI*")){
                            $element | Out-File -append E:\WiFiLaptopsAdded\$($FileName).txt 
                        }
                    }
                Get-ADComputer $element -Properties MemberOf | 
                    %{
                        if (-Not ($_.MemberOf -like "*LaptopsWIFI*")){
                            Add-ADGroupMember gIN-WIFI-Laptops -Members $_
    			        }
    	            }
    	        }
            }


    ¯\_(ツ)_/¯

    Friday, November 28, 2014 1:59 PM
  • This is closer to what you might be trying to do:

    import-module activedirectory $fileName = "E:\WiFiLaptopsAdded\AddedToday$((get-date).tostring('MMddyyyy'))" Get-ADOrganizationalUnit -Filter {Name -like '*laptop*'} | ForEach-Object{ $computers=Get-ADComputer -Filter * -Searchbase $_ -Properties MemberOf foreach($computer in $computers){ if($computer.MemberOf -like "*LaptopsWIFI*"){ Write-Host "$($computer.Name) is already included" -ForegroundColor green }else{ Write-Host "Adding $($computer.Name) to group" -ForegroundColor green $computer.Name | Out-File -append $filename Add-ADGroupMember gIN-WIFI-Laptops -Members $computer } }
    }




    ¯\_(ツ)_/¯




    • Edited by jrv Friday, November 28, 2014 2:10 PM
    • Proposed as answer by jrv Saturday, November 29, 2014 4:43 AM
    • Marked as answer by Just KarlModerator Thursday, April 30, 2015 4:49 PM
    Friday, November 28, 2014 2:05 PM
  • I have created a script which checks in all Laptop OUs in AD and checks if those laptops are member of a particular group.

    If not it adds them to that group.

    The problem is script does not work with OU's which have space in name  for example in xyz.com domain there is one OU ABC DEF in which Laptops OU is there , so the members of this OU does not get added to group.

    How to ignore the space in name ?

    My code is as folows :

    #=========================================================================================
    import-module activedirectory
    $group=Get-ADGroup LaptosWIFI
    $FileName = "AddedToday" + $((get-date).tostring("MMddyyyy"))
    Get-ADOrganizationalUnit -Filter {Name -like '*laptop*'} | 
         ForEach-Object{
    	$comp = Get-ADComputer -Filter * -Searchbase "OU=Laptops,OU=XYZ ABC,DC=UAT,DC=ABCUAT,DC=COM"
        
         $string = $comp.name 
         $names= @()
         $names+= $string -split "\s+"
         foreach ($element in $names) {
         Write-Host $element
         Get-ADComputer $element -Properties MemberOf | %{if (-Not ($_.MemberOf -like "*LaptopsWIFI*")) {$element | Out-File -append E:\WiFiLaptopsAdded\$($FileName).txt }}
         Get-ADComputer $element -Properties MemberOf | %{if (-Not ($_.MemberOf -like "*LaptopsWIFI*")) {Add-ADGroupMember gIN-WIFI-Laptops -Members $_}} -ErrorAction SilentlyContinue
         }}

    in this code OU "XYZ ABC" is considered as different and i get following error when i run the script

     Get-ADComputer <<<<  $element -Properties MemberOf | %{if (-Not ($_.Memb
    erOf -like "*gIN-WIFI-Laptops*")) {Add-ADGroupMember gIN-WIFI-Laptops -Members
    $_}} -ErrorAction SilentlyContinue
        + CategoryInfo          : WriteError: (:) [Get-ADComputer], ParameterBindi
       ngException
        + FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.ActiveDirectory
       .Management.Commands.GetADComputer

    Get-ADComputer : Cannot bind parameter 'Identity' to the target. Exception sett
    ing "Identity": "Cannot validate argument on parameter: 'Identity'. The argumen
    t is null or empty. Supply an argument that is not null or empty and then try t
    he command again."
    At C:\Scripts\LaptopAdditionWIFI\LaptopAddition.ps1:23 char:20
    +      Get-ADComputer <<<<  $element -Properties MemberOf | %{if (-Not ($_.Memb
    erOf -like "*gIN-WIFI-Laptops*")) {$element | Out-File -append D:\WiFiLaptopsAd
    ded\$($FileName).txt }}
        + CategoryInfo          : WriteError: (:) [Get-ADComputer], ParameterBindi
       ngException
        + FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.ActiveDirectory
       .Management.Commands.GetADComputer


    Your error messages are for your $element variable.  Unless you are using \s+ in your computer names the attribute doesn't need to be trimmed.

    Might have to tweak it a bit but here's some syntax for your loop

    foreach {$OU in Get-AdADOrganizationalUnit -Filter {Name -like "*laptop*"})
    {
    Get-ADComputer -LdapFilter "(!MemberOf=$group)" -SearchBase $OU.DistinguishedName -SearchScope 2 -Properties MemberOf|
    Tee-Object output|%{Add-AdGroupMember $group -Members $_.DinstinguishedName}
    $Output
    $Output| Out-File -append E:\WiFiLaptopsAdded\$($FileName).txt
    }



    MCSE/MCSA/MCP/Security+,A+

    Saturday, November 29, 2014 4:40 AM