[SOLVED] NetAdapterName data type mismatch

by andy.whitea at 2013-03-11 07:10:40

Hi guys,

I am creating an import script that uses forms to allow a user to select which network adapter they would like to use for their virtual switch.

The problem I am having is that using the variable that contains the selected network adapter name generates an error when I try and create the virtual switch. As you can see from the code, I have tried deleting any spaces that might be contained, and have tried converting the variable to a string (I know it should be a string anyway, I was just checking!).

The error is "A positional parameter can not be found". This is for the last line containing the New-VMSwitch cmdlet.

If anyone can help with my issue, I would be eternally grateful!

Regards
Andy



[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")

#Create new form
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Please choose a Network Adaptor"
$objForm.Size = New-Object System.Drawing.Size(400,200)
$objForm.StartPosition = "CenterScreen"

#Specify keypress actions
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($.KeyCode -eq "Enter")
{[string]$Global:extNet=$objListBox.SelectedItem;$objForm.Close()}})
$objForm.Add_KeyDown({if ($
.KeyCode -eq "Escape")
{$objForm.Close()}})

#Add Ok button
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(125,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({[string]$Global:extNet=$objListBox.SelectedItem;$objForm.Close()})
$objForm.Controls.Add($OKButton)

#Add Cancel button
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(200,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)

#Specify label properties
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20)
$objLabel.Size = New-Object System.Drawing.Size(380,20)
$objLabel.Text = "Please choose a Network Adaptor:"
$objForm.Controls.Add($objLabel)

#Specify ListBox properties
$objListBox = New-Object System.Windows.Forms.ListBox
$objListBox.Location = New-Object System.Drawing.Size(10,40)
$objListBox.Size = New-Object System.Drawing.Size(360,20)
$objListBox.Height = 80

#Discover and add network adaptors to list
$strComputer = "."
$objNetAdapt = Get-NetAdapter -Name *
foreach ($objNetAdaptItem in $objNetAdapt)
{
[void] $objListBox.Items.Add($objNetAdaptItem.Name)
}

#Add ListBox to form
$objForm.Controls.Add($objListBox)

#Bring for to top
$objForm.Topmost = $True

#Activate form
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()

$x = $extNet.toString()
$x -replace " ",""

#Create switch for demo network
$extVMS = New-VMSwitch "DEMOV2NETWORK" –NetAdapterName "$x" -AllowManagementOS $true -Notes "Demo Environment Switch""

by andy.whitea at 2013-03-11 07:12:48
Sorry guys; I forgot to add that the network adapter is created successfully when I change the variable name for "Ethernet" (which is the name of the adapter on the machine I am using).

Cheers
Andy
by andy.whitea at 2013-03-11 08:31:49
Solved the issue myself! It appears that there was a strange ascii character causing errors in my New-VMSwitch cmdlet