Empty string error but variable contains string

I am trying to set the Network Address of a USB C docking station attached to a remote system to match that of the systems primary NIC. When I run my script I get an error message stating “Cannot bind argument to parameter ‘Name’ because it is an empty string.” When I perform Write-Host $DockName is returns the string value I am looking for but will not complete the assigned task. What am I missing?

#Begin script by identifying remote system to check
Write-Host "What is the system name you wish to check?" -ForegroundColor Cyan
''
$SysName = Read-Host -Prompt "Enter system name"
''
#Check to see if system is active on the network
If ((Test-Connection $SysName -Quiet -Count 1 -ErrorAction SilentlyContinue) -eq $true) {
Write-Host "$SysName is online. Please press Enter to continue." -ForegroundColor Green
''
Pause
''
''
} Else {
	Write-Host "'$SysName' is not responding to pings. Please verify that it is connected to the network and powered on!" -ForegroundColor Red
	''
	Pause
	''
	''
}
#Now check for all physical network adapters and place them in an array
Write-Host "Retrieving Ethernet adapters from '$SysName'..." -ForegroundColor Yellow
''
''
$Adapter = @(Invoke-Command -ComputerName $SysName -ScriptBlock {Get-NetAdapter -Physical | Select-Object Name, InterfaceDescription, MacAddress})
    for ($i = 0; $i -lt $Adapter.Count; $i++){
    Write-Host  "($i) $($Adapter[$i].Name), $($Adapter[$i].InterfaceDescription), $($Adapter[$i].MacAddress)" -ForegroundColor Green
    }
''
''
''
#Select the systems primary network adapter from the array 
Do {
	Write-Host "What is the number of the main network adapter?" -ForegroundColor Green
	''
	$Select = Read-Host -Prompt "Enter number selection"
	''
	If ($Select -match "^\d+$") {
		$Select = [int]$Select
	}
	If (($Select -ge $Adapter.Count) -or ($Select -lt 0) -or ($Select -notmatch "^\d+$")) {
	Write-Host "You did not enter a proper number selection" -ForegroundColor Red
   	Write-Host "Please select a number between '0' and '$($Adapter.Count - 1)'" -ForegroundColor Red
    	''
    	Pause
    	''
    	''
    	''
	}
} Until (($Select -lt $Adapter.Count) -and ($Select -ge 0) -and ($Select -match "^\d+$"))
#Store MAC address in $PrimaryMac then modify MAC address to remove '_' and store in $PMac
$PrimaryMac = $Adapter.MacAddress[$Select]
$PMac = $PrimaryMac -replace '-'
''
''
Write-Host "'$PMac' has been selected."
''
''
Pause
''
''
''
#Select the systems docking station network adapter from the array
Do {
	Write-Host "What is the number of the docking station network adapter?" -ForegroundColor Green
	''
	$Select = Read-Host -Prompt "Enter number selection"
	''
	If ($Select -match "^\d+$") {
		$Select = [int]$Select
	}
	If (($Select -ge $Adapter.Count) -or ($Select -lt 0) -or ($Select -notmatch "^\d+$")) {
		Write-Host "You did not enter a proper number selection" -ForegroundColor Red
        Write-Host "Please select a number between '0' and '$($Adapter.Count - 1)'" -ForegroundColor Red
        ''
        Pause
        ''
        ''
        ''
    }
} Until (($Select -lt $Adapter.Count) -and ($Select -ge 0) -and ($Select -match "^\d+$"))
#Store docking station name in $DockName
$DockName = $Adapter.Name[$Select]
#Pass variables $DockName and $PMac to remote system to modify MAC address of docking station to that of system primary network adapter
Invoke-Command -ComputerName $SysName -ScriptBlock {Set-NetAdapter -Name "$DockName" -MacAddress "$PMac"}

about Remote Variables - PowerShell | Microsoft Learn

Rob,

Thank you for that hint, the $Using scope modifier was the trick here. I still received an error message but was able to overcome it using the Set-NetAdapterAdvancedProperty cmdlet.

1 Like