Author Posts

May 15, 2017 at 7:40 pm

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

May 16, 2017 at 8:25 pm

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'}

May 16, 2017 at 10:33 pm

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

May 16, 2017 at 10:48 pm

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

May 17, 2017 at 2:36 pm

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"
        }
    }

May 19, 2017 at 1:19 pm

My bad,

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

May 19, 2017 at 1:54 pm

Thanks guys the adsi and code samples worked perfect.

Solved