I am looking to append windows host file get current dynamic ip and map it to a host name irrespective of current ip address. i am getting below error
===============================================
Add-Content : A positional parameter cannot be found that accepts argument 'hostname1'. At C:\Users\Opps\Desktop\power\New Text Document.ps1:6 char:3 + {ac -Encoding UTF8 -value "$($env:windir)\system32\Drivers\etc\hosts ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Add-Content], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.AddContentCommand
================================================================================
Script :
$ip=get-WmiObject Win32_NetworkAdapterConfiguration|Where {$_.Ipaddress.length -gt 1}
$ip.ipaddress[0]
$hst = $env:COMPUTERNAME
Set-ExecutionPolicy -ExecutionPolicy Unrestricted If ((Get-Content "$($env:windir)\system32\Drivers\etc\hosts" ) -notcontains "127.0.0.2 hostname1")
{ac -Encoding UTF8 "$($env:windir)\system32\Drivers\etc\hosts" ($ip.ipaddress[0]) ($hst) }
{ac -Encoding UTF8 "$($env:windir)\system32\Drivers\etc\hosts" "$($ip.ipaddress[0]) $hst" }
and move theif
statement down a line – Itchydon