Get-AdComputer not working in PSAD script

Evening all,

I am writing a PSAD script to detect the remote computers OU and that will then install the software and configuration for that site.

My problem is is that It is running the script perfectly on my (admin) maachine but not on a pilot users machine.
I have checked the C:\Windows\Logs\Software log files and getting the below.

[Post-Installation] :: Error Record:

Message : The term ‘get-adcomputer’ is not recognized as the name of a
cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that
the path is correct and try again.
InnerException :

I have added Import-Module ActiveDirectory at the top but it still doesnt work. does anyone have any thoughts to why? Below is the script.

<code> Import-Module ActiveDirectory
$cname = $env:Computername
        $distname = Get-Adcomputer $cname | Select Name -ExpandProperty DistinguishedName</code>

From the code above I then set a variable for the Distinguished name, then I have IF statements to run through 20 OU variables and then If that computer matches one of the 20 it’ll have the specific configuration.

Do you have the RSAT tools installed on the client device(s) so that when the Import-Module ActiveDirectory cmdlet is called, it can locate the required contents?

Hello there,

RSAT is not installed on the client machines. I could install it as a test. Is there another command I could use that doesnt require RSAT? Dont think its good practice install admin tools for standard uses.

Hi Joe, the Get-AD* cmdlets are not available unless the RSAT tools are installed.

Since you want to query AD from multiple client PC’s, RSAT needs to be installed on those systems in order to have access to the Get-ADComputer cmdlet.

Your statement “Dont think its good practice install admin tools for standard uses.” is correct and I agree, but you are attempting to perform an admin related task from a client PC therefore requiring the RSAT tools and associated PowerShell cmdlets.

Perhaps you need to redesign the solution in order to make this work?
What deployment method are you using?
Think about checking if clients meet certain criteria and then targeting them with a software deployment. This approach is essentially how deploying software to a collection of computers works in SCCM.

Good morning!

Yes you are right. I can always uninstall RSAT at the end of the script if needed anyway.

I cant think of another way to obtain the Distinguished name of a machine and then query it at a later stage so will include it in this script and the forum know the outcome.

I am using SCCM 2012 to deploy the PSAD script. I could have 20 different deployments for each targeted site but the 20 sites will increase and SCCM will get messy. Each ‘site’ has a specific configuration and a certificate to go with it so my script queries the computer and depending what OU they are in they get said config.

This is now my next challenge.

Add-WindowsFeature : The target of the specified cmdlet cannot be a Windows client-based operating system.

Hello all,

I have found that you will need to install RSAT on the client machine in order for the “Get-AdComputer” cmdlet to work. Below is what I used to obtain the information needed using WMI.

<code> $cname = $env:Computername
        $OU = ([adsisearcher]&quot;(&amp;(name=$env:computername)(objectClass=computer))&quot;).findall().path
        $OU = $OU -Replace &quot;LDAP://&quot;
        $distname = $OU</code>

using ([adsisearcher]"(&(name=$env:computername)(objectClass=computer))").findall().path outputs the command with LDAP:// at the beginning so I used POSH to replace the “LDAP://” and set that as a variable.

VBScript can get this info. Note: I’m not the original author of this script but I did modify it years ago to suit my purposes.

'Use VBScript to query Domain for the computer DN
'Usage -
’ 1. you must include the computer name on the commandline
’ example: cscript.exe //nologo GetDN.vbs computername
’ output is the DN found or an error if it isn’t found or an
’ error if the computer name is not provided on the commandline
’ output can be redirected to a file using CMD redirection if needed

Option Explicit

Dim objRootDSE, strDNSDomain, objTrans, strNetBIOSDomain, objArgs
Dim objFSO, strComputer, strComputerDN, RC

Const ForReading = 1

’ Constants for the NameTranslate object.
Const ADS_NAME_INITTYPE_GC = 3
Const ADS_NAME_TYPE_NT4 = 3
Const ADS_NAME_TYPE_1779 = 1

'Check for command line arg for the computername
Set objArgs = Wscript.Arguments
If objArgs.Count <> 1 then
wscript.echo “Missing Argument”
wscript.quit 1
Else
strComputer = objArgs(0)
End if

’ Determine DNS name of domain from RootDSE.
Set objRootDSE = GetObject(“LDAP://RootDSE”)
strDNSDomain = objRootDSE.Get(“defaultNamingContext”)

’ Use the NameTranslate object to find the NetBIOS domain name from the DNS domain
Set objTrans = CreateObject(“NameTranslate”)
objTrans.Init ADS_NAME_INITTYPE_GC, “”
objTrans.Set ADS_NAME_TYPE_1779, strDNSDomain
strNetBIOSDomain = objTrans.Get(ADS_NAME_TYPE_NT4)
’ Remove trailing backslash.
strNetBIOSDomain = Left(strNetBIOSDomain, Len(strNetBIOSDomain) - 1)

’ Use Set method to specify NT format name.
’ sAMAccountName of computer is NetBIOS name with “$” appended.
’ Trap error if computer not found.
On Error Resume Next
objTrans.Set ADS_NAME_TYPE_NT4, strNetBIOSDomain & “” & strComputer & “$”
If (Err.Number <> 0) Then
Wscript.Echo “Computer not found”
RC = 2
On Error GoTo 0
Else
On Error GoTo 0
’ Retrieve Distinguished Name.
strComputerDN = objTrans.Get(ADS_NAME_TYPE_1779)
Wscript.Echo strComputerDN
RC = 0
End If

wscript.quit RC