Sam Lewis
Sam Lewis

Reputation: 45

Getting Results of Get-ADComputer with Invoke-Command

I am working on a script that shall run on client (desktop) Windows machines that do not have any RSAT tools installed. The script will poll various things about the machine, and one of those I would like to include is the description of the machine in Active Directory. The ultimate goal is to save these into SQL for a pretty table.

I am able to get everything but the AD Description. This is what I am trying now:

$Computer = (Get-WmiObject Win32_ComputerSystem | Select -Expand Name) | Out-String

$session = New-PSSession -ComputerName domainController
Invoke-Command -Session $session -ScriptBlock {
param(
   [Parameter(Position=0)]
   $computer
)

Import-Module ActiveDirectory

write-host "the value of the passed parameter is $computer"

Get-ADComputer -SearchBase 'DC=CONTOSO,DC=LOCAL' -Filter {Name -Like $computer} -Properties Description | Select -Expand Description

write-host "the name of this computer is $(Get-WmiObject Win32_ComputerSystem | Select -Expand Name)"

write-host $description

 } -ArgumentList $Computer

I know that the computer name is being passed, because the result of

write-host "the value of the passed parameter is $computer"

does indeed show the name of the computer I am passing. I know that the command is indeed running on the server because the result of

write-host "the name of this computer is $(Get-WmiObject Win32_ComputerSystem | Select -Expand Name)"

does indeed show the name of the server with ADUC installed. I know the command to pull the Description works on that server, because if I am connected to said server with RDP, I can run that command without issue.

I don't get any errors (and my $ErrorActionPreference is "Stop"), it just simply skips over the line of code like it's a comment.

Is there anything I am missing, or better yet a better way for me to pull the description of said computer?

Upvotes: 0

Views: 1095

Answers (1)

Bill_Stewart
Bill_Stewart

Reputation: 24525

You don't need to remote to request information from Active Directory. Here's a short example that doesn't even use the AD cmdlets:

$computerName = [Net.Dns]::GetHostName()

$searcher = [ADSISearcher] "(sAMAccountName=$computerName$)"
$searcher.PropertiesToLoad.AddRange(@("description"))
$searchResult = $searcher.FindOne()

"Computer description: {0}" -f $searchResult.Properties["description"][0]

Upvotes: 2

Related Questions