Passing the contents of a csv file through to a cmdlet

I am new to PowerShell and I’m having trouble understanding what’s going on here.

I have a simple csv file whose contents look like this:
“name”
“DLT600-101”
“DLT600-106”
“DLT600-107”

These are computer names. I want to run Test-Connection against them.

I import the csv file and store it in an array:

[array]$A = Import-Csv "e:/computernames.csv"

If I display A$ I get:

name

DLT600-101
DLT600-106
DLT600-107

So far, so good.

Then I try to run the connection test against this array:

Test-Connection -ComputerName $A

What I get is a series of error messages that all begin like this:

Test-Connection : Testing connection to computer ‘@{name=DLT600-101}’ failed: A non-recoverable error occurred during a database lookup.

So my questions are 1) why does ‘@{name=DLT600-101}’ appear there, instead of ‘DLT600-101’, and 2) what am I doing wrong?.

Thanks!

That’s because the -ComputerName parameter expects one or more strings. You’ve given it an array of non-String objects.

Test-Connection -Computername ( Import-CSV whatever.csv | Select -Expand Name )

You could also:

$A = Import-CSV whatever.csv | Select -Expand Name
Test-Connection -ComputerName $A

That’s probably what you want. This “extracts” the values from the Name properties of the objects, leaving you with plain-old strings. This is one of the many fun and interesting techniques, using this exact example along with others, in “Learn PowerShell in a Month of Lunches,” and it’s an excellent example of how PowerShell is very object-oriented in how it works.

It’s so obvious when you see how it’s done. A bit like having a magic trick explained to you. Thanks very much!

I have another problem, which I suspect is of the same nature:

I want to rename a room calendar mailbox. I begin by displaying a grid view of all the existing room names:

Get-Mailbox -RecipientTypeDetails RoomMailbox | select name | sort name |
out-gridview -OutputMode Single -Title "Which room do you want to rename?" -OutVariable oldname

This fills the variable $oldname with the name of the room I want to rename.

I fill in a few other variables with the new name, the alias and the email address.

Then I try to run the Set-Mailbox command:

Set-Mailbox -identity $oldname -Name $newname -Alias $alias -DisplayName $newname -Emailaddresses $email

I get an error message:

Cannot process argument transformation on parameter ‘Identity’. Cannot convert value “” to type
“Microsoft.Exchange.Configuration.Tasks.MailboxIdParameter”. Error: “Parameter values of type
Microsoft.Exchange.Configuration.Tasks.MailboxIdParameter can’t be empty. Specify a value, and try again.
Parameter name: identity”
+ CategoryInfo : InvalidData: (:slight_smile: [Set-Mailbox], ParameterBindin…mationException
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,Set-Mailbox
+ PSComputerName : ps.outlook.com

If I fill in the identity with a hard-coded string everything works fine, including the other variables.

I have tried a hundred different ways, but I cannot get the trick to this!

As I may have mentioned, I am a noobie to PowerShell!

Thanks!

Hi Stewart,
I’m fairly new to some of the more intricate parts of PowerShell, but what appears to be happening is that the parameter

-outvariable
is not actually storing any output inside your variable. This might be because Out-Gridview is meant for displaying information after you have done all the processing, I am just guessing.

What I would try is

$oldname = Get-Mailbox -RecipientTypeDetails RoomMailbox | select name | sort name |
out-gridview -OutputMode Single -Title "Which room do you want to rename?"
Set-Mailbox -identity $oldname -Name $newname -Alias $alias -DisplayName $newname -Emailaddresses $email

Hi,

Iam new to the powershell world but I have observed that OGV is storing the values in hash table where set-mailbox identity parameter is asking for a array format.

You can set the value like below

Set-Mailbox -identity $oldname.name -Name $newname -Alias $alias -DisplayName $newname -Emailaddresses $email

Thanks everyone. That was helpful.