所以我正在开发一个动态到静态IP的脚本
# Identify all adapters that recieved an address via DHCP
$adapters = Get-WmiObject Class Win32_NetworkAdapterConfiguration | Where-Object {($_.IPAddress) -and $_.DHCPEnabled -eq 'True' }
# Iterate through each adapter
foreach($adapter in $adapters) {
# Get current adapter and IP information
$adapIndex = $adapter.InterfaceIndex
$ipAddress = $adapter.IPAddress[0]
$subnetMask = $adapter.IPSubnet[0]
$defaultGateway = $adapter.DefaultIPGateway[0]
$prefix = (Get-NetIPAddress InterfaceIndex $adapIndex AddressFamily IPv4).PrefixLength
$dnsServers = $adapter.DNSServerSearchOrder
[ipaddress]$netAddr = ([ipaddress]$ipAddress).Address -band([ipaddress]$subnetMask).Address
# Identify the DHCP server
$dhcpServer = $adapter.DHCPServer
$dhcpName = ([System.Net.DNS]::GetHostEntry($dhcpServer)).HostName
# Add an exclusion to DHCP for the current IP address
Invoke-Command ComputerName $dhcpName ScriptBlock{
Add-DhcpServerv4ExclusionRange –ScopeId $args[0] -StartRange
$args[1] -EndRange $args[1]
} -ArgumentList $netAddr.IPAddressToString, $ipAddress
# Release the DHCP address lease
Remove-NetIPAddress InterfaceIndex $adapIndex -Confirm:$false
# Statically assign the IP and DNS information
New-NetIPAddress InterfaceIndex $adapIndex -AddressFamily IPv4 IPAddress $ipAddress PrefixLength $prefix DefaultGateway $defaultGateway
Set-DnsClientServerAddress InterfaceIndex $adapIndex ServerAddresses $dnsServers
}
我在最后一个块中得到了这个错误
At DynamicTOStatic.ps1:19 char:21
+ $args[1] -EndRange $args[1]
+ ~~~~~~~~~
Unexpected token '-EndRange' in expression or statement.
At DynamicTOStatic.ps1:19 char:31
+ $args[1] -EndRange $args[1]
+ ~~~~~
Unexpected token '$args' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
我尝试尽可能多地调试它,但我根本不明白修复它需要什么。任何类型的重新排列都会导致相同的错误。
答案 0 :(得分:1)
删除$args[1]
之前的换行符:
Add-DhcpServerv4ExclusionRange –ScopeId $args[0] -StartRange $args[1] -EndRange $args[1]
或者使用`
转义新行:
Add-DhcpServerv4ExclusionRange –ScopeId $args[0] -StartRange `
$args[1] -EndRange $args[1]