Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 我希望附加windows主机文件以获取当前动态ip并将其映射到主机名_Powershell - Fatal编程技术网

Powershell 我希望附加windows主机文件以获取当前动态ip并将其映射到主机名

Powershell 我希望附加windows主机文件以获取当前动态ip并将其映射到主机名,powershell,Powershell,我希望附加windows主机文件获取当前动态ip,并将其映射到主机名,而不考虑当前ip地址。 我正在犯错误 ============================================================= 添加内容:找不到接受参数“hostname1”的位置参数。 在C:\Users\Opps\Desktop\power\New Text Document.ps1:6 char:3 +{ac-编码UTF8-值“$($env:windir)\system32\Driver

我希望附加windows主机文件获取当前动态ip,并将其映射到主机名,而不考虑当前ip地址。 我正在犯错误

=============================================================
添加内容:找不到接受参数“hostname1”的位置参数。 在C:\Users\Opps\Desktop\power\New Text Document.ps1:6 char:3 +{ac-编码UTF8-值“$($env:windir)\system32\Drivers\etc\hosts。。。 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +CategoryInfo:InvalidArgument:(:)[Add Content],参数BindingException +FullyQualifiedErrorId:PositionParameterNotFound,Microsoft.PowerShell.Commands.AddContentCommand
================================================================================

脚本:

$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) }

添加内容
需要一个字符串作为值,因此要更改将值封装在引号中所需的类型。要在引号中访问对象属性,例如
$ip.ipaddress[0]
,为了使文本不被逐字处理,我们必须在括号中加上前面的美元符号
“$(…)“
正式称为子表达式运算符(请参阅)。为确保不复制条目,我们使用
if
语句对条目进行快速检查,如果
if
语句的两个条件都满足,则只继续执行
添加内容

$ip = get-WmiObject Win32_NetworkAdapterConfiguration|Where {$_.Ipaddress.length -gt 1} 
$ip.ipaddress[0]
$hst = $env:COMPUTERNAME
$hostfile = Get-Content "$($env:windir)\system32\Drivers\etc\hosts"
Set-ExecutionPolicy -ExecutionPolicy Unrestricted 
if ($hostfile -notcontains "127.0.0.2 hostname1" -and 
    (-not($hostfile -like "$($ip.ipaddress[0]) $hst"))) {
    Add-Content -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}
并将
if
语句下移一行谢谢你,它现在正在工作,这似乎是在向主机文件添加多个重复或新的ip。我们是否可以在每次运行此脚本时追加同一行而不是追加新行。主机文件需要每行包含一行,因此我假设你希望在复制之前进行检查一个条目。我将添加它作为答案,以便格式保持不变。建议:可能希望添加一个检查条目是否存在但未注释。@Rohindhart-感谢建议-脚本已检查条目是否存在,条件是
if
语句
(-not($hostfile-like)”$($ip.ipaddress[0])$hst”))
还是我误解了你?这没什么大不了的,但是有一个条目可能存在,但在行首用#注释。因为该条件只检查条目是否存在,如果存在则不添加,但如果有注释,则脚本什么也不做。正如我所说,这是一种微小的可能性。没有big deal.我明白了Rohin的意思-我可以通过添加星号并更改稍微类似于
(-not($hostfile-like“*$($ip.ipaddress[0])$hst”)){
的条件,通过通配符搜索很容易地解决这个问题,但是O.P.一直在移动球门柱,所以我现在就挂火