1

I've been using these lines of code:

$204computernames = Get-ADComputer -searchbase $sb -filter * | ?{$_.name -like "ptfg*-061*"} | select name
$onlineComputers = $204computernames |Where-Object { Test-Connection $_.name -Count 1 -Quiet }

to grab all of my computers on my network and put them into a variable so I can push all of my documents, updates, etc to them so that I dont have to go to each computer individually to get the files I want where I want. When I take the variable and put it into a line of code like this

Test-Connection $onlineComputers

I get errors like this:

Test-Connection : Testing connection to computer '@{name=PTFGW-0613618TN}' failed: A non-recoverable error occurred during a database lookup
At line:1 char:1
+ Test-Connection $onlineComputers

I'm assuming after extensive testing in different codes that there is a problem with the way my variable stores its values. Does anyone know how I can fix this issue?

5
  • What happens if you change Test-Connection $onlineComputers to Test-Connection $onlineComputers.Name? Commented Jul 15, 2020 at 15:15
  • that works in this context, do i have to integrate it into all of my other codes like that? Commented Jul 15, 2020 at 15:22
  • 1
    Your problem is that when you used | Select name it creates a custom object with a name property for each computer and not just a list of computer names. You need to pass only the names to Test-Connection - adding .name implicitly loops through all the objects pulling out the value of the name property. So, the answer to your question is that it depends on the type of information you are passing as to whether or not you need to do this (or something similar). You can always check the type of information by doing something like this: $onlineComputers.GetType() Commented Jul 15, 2020 at 15:29
  • thank you so much you gave me some inspiration and i realised that the '.name' was exactly what i needed on my other codes. Commented Jul 15, 2020 at 15:53
  • Most frequently asked powershell question. Or | foreach name. Commented Jul 15, 2020 at 17:03

1 Answer 1

1

As @boxdog already pointed out in the comments, with | select name you get objects with the single property Name. Therefore, you don't get a list of computer names, but a list of objects that have the computer name in the Name property. You can work with that and access each computer name like .Name.

But to solve your problem, you can replace | select name (which stands for | Select-Object -Property Name) by | Select-Object -ExpandProperty Name. That way, you filter out only the computer name and expand the result to just this property. After that, you really have just a list of computernames (an array of string objects).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.