Reputation: 41
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
Upvotes: 0
Views: 123
Reputation: 19634
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
Upvotes: 1