Active Directory Help

This topic contains 6 replies, has 4 voices, and was last updated by  scott s 5 months ago.

  • Author
    Posts
  • #70699

    scott s
    Participant

    I am fairly new and need help on 2 active Directory tasks.

    1) Checking if a pc name is in AD or not
    2) adding a PC to AD

    I saw Get-ADComputer but the module needed is not on all pc's. these will be freshly imaged pc's that need to be added to the domain. Is there another way or way to include any needed module in my script (it will be eventually compiled using Powershell studio)

    For item 2 I want to add a pc if not in AD to the domain and a specific OU.

    Any help would be great.

    thanks

  • #70814

    Juliën Zweverink
    Participant

    Here is a function to test if an Computer Account is in AD or not, and returns a Boolian(True,False)

    Function Test-ADComputer {
    #	.SYNOPSIS
    #		Helper Funtion that check's if an ADgroup (still) Exist in Active Directory.
    #	
    #	.DESCRIPTION
    #		Helper Funtion that check's if an ADgroup (still) Exist in Active Directory.
    #	
    #	.PARAMETER ComputerName
    #		The ActiveDirecory Computername / Identity
    #	
    #	.EXAMPLE
    #		PS C:\> Test-ADComputer -ComputerName $ComputerName
    #		
    #		If (Test-ADComputer -ComputerName $ComputerName) {Do-SomeThing}
    #	
    #	.OUTPUTS
    #		System.Boolean
    #	
    #	.NOTES
    #		Made By Julien Zweverink
    	[CmdletBinding(ConfirmImpact = 'None',
    				   PositionalBinding = $true,
    				   SupportsPaging = $false,
    				   SupportsShouldProcess = $false)]
    	[OutputType([bool])]
    	param
    	(
    		[Parameter(Mandatory = $true,
    				   ValueFromPipeline = $true,
    				   ValueFromPipelineByPropertyName = $true,
    				   ValueFromRemainingArguments = $false,
    				   Position = 0)]
    		[ValidateNotNullOrEmpty()]
    		[Alias('CSName', 'PSComputerName', '__SERVER', 'HostName', 'Identity')]
    		[string]$ComputerName
    	)
    	
    	Begin {
    		Write-Verbose -Message "[BEGIN  ] Starting: $($MyInvocation.Mycommand)"
    	} #Begin
    	
    	Process {
    		Try {
    			Write-Verbose -Message "[Process] Test if: $ComputerName can be found in Active Directory"
    			$null = Get-ADComputer -Identity $ComputerName -ErrorAction Stop
    			Write-Verbose -Message "[Process] $ComputerName was found in Active Directory"
    			return $true
    		} #Try
    		Catch {
    			Write-Verbose "[Process] Computer: $ComputerName not found in Active Directory"
    			Return $false
    		} #Catch
    	} #Process
    	
    	End {
    		Write-Verbose -Message "[End    ] Ending:   $($MyInvocation.Mycommand)"
    	} #End
    	
    } #Function
    

    To Use it:

    Test-ADComputer -ComputerName Computer123 #-Verbose
    #Or
    If (Test-ADComputer -ComputerName Computer123) {'Do-SomeTing'}
    
  • #70822

    scott s
    Participant

    That wont work as it relies on Get-ADComputer. I asked for something that doesnt. these are new pc's and that requires an install to work. It is also available as part of the Windows 7 and Windows 8 Remote Server Administration Toolkits (RSATs). It is not a Windows feature.

    I need somethinbg out of the box

  • #70823

    Olaf Soyk
    Participant

    Implicit Remoting might be helpful in this case ....

  • #70912

    Aapeli Hietikko
    Participant

    As ugly as this might be, it still works. You should do all that fancy stuff for the function as in above.

    
    function get-ldapComputer ($computer) {
    
        $strFilter = "(&(objectCategory=computer)(Name=$computer))"
        
        $objDomain = New-Object System.DirectoryServices.DirectoryEntry
        
        $objSearcher = New-Object System.DirectoryServices.DirectorySearcher
        $objSearcher.SearchRoot = $objDomain
        $objSearcher.PageSize = 1000
        $objSearcher.Filter = $strFilter
        
        $objSearcher.PropertiesToLoad.Add('name') |out-null
        $result = $objSearcher.Findone()
    
        if ($result) {
            write-output "Found $($result.Properties.name)"
            }
        Else {
            write-output "DANG! There was no $computer"
            }
        }
    
  • #71084

    Juliën Zweverink
    Participant

    My bad,

    This Module should do the trick, it uses ADSI and not the AD Module.
    https://github.com/lazywinadmin/AdsiPS

  • #71090

    scott s
    Participant

    Thanks guys the adsi and code samples worked perfect.

    Solved

You must be logged in to reply to this topic.