0

I have script step1.ps1 & it contains the code:

echo "Starting with Step 3: Configuring the OS..."
echo "$args[0] and $args[1]"
step3.ps1 $args[0] $args[1]

I execute this script

powershell.exe install.ps1 <ip-address> <hostname>

Now when step1.ps1 is executed it calls script step3.ps1 & it contains the code:

echo "checking step3"
echo $args[0] and $args[1]
ac -Encoding UTF8  "$($env:windir)\system32\Drivers\etc\hosts" $args[0] $args[1]
echo "HANA DB Host file entry maintained"

It gives me an error:

powershell.exe : Add-Content : A positional parameter cannot be found that accepts argument '<hostname>'.  
At line:1 char:1  
+ powershell.exe install.ps1 <ip-address> ...  
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
    + CategoryInfo          : NotSpecified: (Add-Content : A...  
 'vue2dvdbhs5'.:String) [], RemoteException  
    + FullyQualifiedErrorId : NativeCommandError


At step3.ps1:3 char:1  
+ Add-Content -Value $args[0] $args[1] -Path "$($env:windir)\system32\D ...  
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
    + CategoryInfo          : InvalidArgument: (:) [Add-Content],  
 ParameterBindingException  
    + FullyQualifiedErrorId :  
 PositionalParameterNotFound,Microsoft.PowerShell.Commands.AddContentCommand 
2
  • Why use the $args array when you can use named parameters? Commented Mar 26, 2018 at 14:09
  • @Bill_Stewart Probably a newer powershell user with batch background. Commented Mar 26, 2018 at 14:09

1 Answer 1

1

Your problem is with the Add-Content (ac) call. As a best-practice in scripts, I suggest you avoid aliases and always name your parameters:

     #Alternative: "$($args[0]) $($args[1])"
     #or           ('{0} {1}' -f $args)
Add-Content -Value ($args[0] + ' ' + $args[1]) -Path "$Env:SystemRoot\System32\Drivers\etc\hosts" -Encoding UTF8

The problem is the interpreter is trying to find a positionally-bound parameter that doesn't exist. It is trying to fill -Value with $args[0] and find another parameter for $args[1]. In this example, I group them with parens and add a space for the hosts file.


Based on your comment:

$IP = Test-Connection -ComputerName $Env:ComputerName -Count 1
Add-Content -Value "$($IP.Address) $($IP.IPV4Address)" -Path "$Env:SystemRoot\System32\Drivers\etc\hosts" -Encoding UTF8
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your patience. Yes, I am beginner in powershell script. Can you please also suggest how to add the system itslef ip-address & hosts. Below is the code I wrote based on the your reference but it did not work. $ipV4 = Test-Connection -ComputerName (hostname) -Count 1 Add-Content -Path "$Env:SystemRoot\System32\Drivers\etc\hosts" -Value "$($ipv4) $($hostname)" -Encoding UTF8
@hans I've updated my answer with your comment question
Its only updating the ipaddress but not the hostname along with it
@hans I made a mistake in which member I was accessing. I've updated the answer again to address this. It turns out this cmdlet is just an abstraction of the Win32_PingStatus wmi class

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.