Get-adcomputer

Hi,

I’m new to this forum and beginner to Powershell too and not sure if it’s right forum to get some help on my query.

I believe i will not be a beginner like now if i keep reading and practicing from this space. :wink:

Not sure what’s wrong here but it returns nothing. :frowning:

 

$computers=Get-Content "C:\test\Input\Computers.txt"
$computers|ForEach-Object -Process {Get-ADComputer -filter 'Name -eq "$_"' -Properties *|select -Property Name,OperatingSystem}

Ranjithkumar

You switched the quotation marks. Try this:

$computers = Get-Content "C:\test\Input\Computers.txt"
$computers | 
ForEach-Object -Process { 
    Get-ADComputer -filter "Name -eq '$_'" -Properties Operatingsystem 
}

You might consider another loop type …

$computerList = Get-Content "C:\test\Input\Computers.txt"
foreach ($ComputerName in $computerList) {
    Get-ADComputer -Identity $ComputerName -Properties Operatingsystem
}

As the attribute “Name” already is part of the default subset Get-ADcomputer returns you don’t have to specify it explicitly for -Properties. :wink:

It’s awesome. Thanks for pointing this out. It worked.

And you was very fast too. :slight_smile:

Okay. Here I’ve come with another one very fast.

While I’m entering many computer names i.e. 80 Computers, It returns error as

 

Get-ADComputer : Cannot find an object with identity: '<Computername> ' under: '<DomainName>'.
I have tried keying in -searchbase, -searchscope and -server too.
$computerList = Get-Content "C:\test\Input\Computers.txt"
foreach ($ComputerName in $computerList) {
Get-ADComputer -Identity $ComputerName -Properties Operatingsystem -searchbase "<domainname>" -SearchScope Subtree -Server <DCName> |
Select -Property Name,OperatingSystem
}

Sorry. I think. I have got this. There were extra spaces on each computer names. That was the issue.

I’d suspect that you have computer names in your list that are not in your AD - maybe typos. Can you check this?

Does it work if you run the Get-ADComputer command using one of the names from the list?
Get-ADComputer -filter ‘Name -eq “ServerA”’ -Properties *|select -Property Name,OperatingSystem -verbose

What happens if you pick a name from the list by doing this:
$computers[0]|ForEach-Object -Process {Get-ADComputer -filter 'Name -eq "$_"' -Properties *|select -Property Name,OperatingSystem -verbose}

Hah hah, I guess I should have refreshed after having the tab open for a bit…

Here’s the next stop. To get the output as csv file. It generates header names for each computer result.
i.e. like below.

Name OperatingSystem ---- --------------- Computername Windows 10 Enterprise

Name OperatingSystem


Computername Windows 10 Enterprise

Name OperatingSystem


ComputernameWindows 10 Enterprise

Name OperatingSystem


Computername Windows 7 Enterprise

Name OperatingSystem


Computername Windows 10 Enterprise


Updated script

$computerList = Get-Content "C:\test\Input\Computers.txt"
foreach ($ComputerName in $computerList) {
Try{
$output=Get-ADComputer -Identity $ComputerName -Properties Operatingsystem|
Select -Property Name,OperatingSystem|Out-File -FilePath C:\test\Output\Result.csv -Append

}
catch{
Write-Output "$_.name"|Out-File -FilePath C:\test\Output\Result.csv -Append
}
}

Use Export-Csv -Path C:\test\Output\Result.csv -Notype rather than Out-File -FilePath C:\test\Output\Result.csv -Append.

Export-Csv will set the first row as the header row. The remaining rows will be data. Export-Csv -Delimiter 'character' allows for a different delimiter (character) than the default comma.

$computerList = Get-Content "C:\test\Input\Computers.txt"
foreach ($ComputerName in $computerList) {
    Try {
        Get-ADComputer -Identity $ComputerName -Properties Operatingsystem -ErrorAction Stop |
        Select-Object -Property Name, OperatingSystem | Export-Csv -Path C:\test\Output\Result.csv -Append

    }
    catch {
        $ComputerName | Out-File -FilePath C:\test\Output\UnAvailable.txt -Append
    }
}

You may make a step back and keep learning the basics of Powershell a little further. :wink:

Yes. You’re right. There were spaces at end of each computer names while i copied them from an excel.

[quote quote=247822]Does it work if you run the Get-ADComputer command using one of the names from the list?

Get-ADComputer -filter ‘Name -eq “ServerA”‘ -Properties *|select -Property Name,OperatingSystem -verbose

What happens if you pick a name from the list by doing this:

$computers[0]|ForEach-Object -Process {Get-ADComputer -filter ‘Name -eq “$_”’ -Properties *|select -Property Name,OperatingSystem -verbose}

Hah hah, I guess I should have refreshed after having the tab open for a bit…

[/quote]
NP. Thanks for your helping mind. :slight_smile:

[quote quote=247837]Use Export-Csv -Path C:\test\Output\Result.csv -Notype rather than Out-File -FilePath C:\test\Output\Result.csv -Append.

Export-Csv will set the first row as the header row. The remaining rows will be data. Export-Csv -Delimiter ‘character’ allows for a different delimiter (character) than the default comma.

[/quote]
Thanks much. I’ll use the same as suggested.

[quote quote=247843]$computerList = Get-Content “C:\test\Input\Computers.txt”
foreach ($ComputerName in $computerList) {
Try {
Get-ADComputer -Identity $ComputerName -Properties Operatingsystem -ErrorAction Stop |
Select-Object -Property Name, OperatingSystem | Export-Csv -Path C:\test\Output\Result.csv -Append

}
catch {
    $ComputerName | Out-File -FilePath C:\test\Output\UnAvailable.txt -Append
}

} You may make a step back and keep learning the basics of Powershell a little further. :wink:

[/quote]
Thanks @Olaf.

Actually I keep on watching different lectures from Youtube(Don Jones) & LinkedLearning(Jason Helmick, Matt Hester, Adam Bertram). Due to lack of practice i end up in different or wrong commands rather than the appropriate one. But Thanks for you kind advise, I’ll continue to do that. :slight_smile:

If you could share any PowerShell learning path to move from beginner, that would be really great…!! Thanks in advance. :slight_smile: Have a good weekend.

 

[quote quote=247873]Actually I keep on watching different lectures from Youtube(Don Jones) & LinkedLearning(Jason Helmick, Matt Hester, Adam Bertram). Due to lack of practice i end up in different or wrong commands rather than the appropriate one. But Thanks for you kind advise, I’ll continue to do that. :slightly_smiling_face:

If you could share any PowerShell learning path to move from beginner, that would be really great…!! Thanks in advance. :slightly_smiling_face: Have a good weekend.[/quote]

I think you’re doing just fine. What I’d like to recommend though is to search for a solution first before you ask in forum like this. Most questions a beginner has have been aswered already in the past. :wink:

If you have to use a source like this and you cannot count on the quality of the source data you could try to use the .trim() method to remove spaces (or other unwnated charachters) from strings you need to use. Example:

$ComputerList = 
@(
    'Computer_01'
    ' Computer_02 '
    'Computer_03 '
    ' Computer_04'
)
foreach ($ComputerName in $ComputerList) {
    "Output without leading or trailing spaces: '$($ComputerName.trim())'"
}

Thanks @olaf