首先让我说我在编写脚本或对其进行故障排除方面一点也不擅长,因此我在这里:)
我正在尝试查找 Windows 域网络中使用静态 IP 地址的所有计算机,我在网上进行了一项小型研究,发现这个 PowerShell 脚本如下所示:
param (
[string]$LDAPFilter = '(name=*)',
[switch]$Verbose
)
Get-ADComputer -LDAPFilter $LDAPFilter |
% `
{
$name = $_.DNSHostName;
if ($Verbose.IsPresent)
{ Write-Host -ForegroundColor Yellow "Connecting to $name..." }
$ints = Get-WmiObject -ErrorAction SilentlyContinue -ComputerName $name `
-Query "select IPAddress, DefaultIPGateway from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE and DHCPEnabled=FALSE";
if ($ints -ne $null)
{
foreach ($int in $ints)
{
foreach ($addr in $int.IPAddress)
{
$ip = $null
if ($addr -match "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}")
{
$ip = $addr
$gw = $null
foreach ($gw_addr in $int.DefaultIPGateway)
{
if ($gw_addr -match "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}")
{
$gw = $gw_addr
break
}
}
if ($ip -ne $null)
{
$info = New-Object PSObject -Property `
@{
Name = $name
IP = $ip
Gateway = $gw
}
$info
if ($Verbose.IsPresent)
{ Write-Host -ForegroundColor Yellow $info }
}
}
}
}
}
} |
Select-Object Name, IP, Gateway
我试图在我的 Active Directory 服务器上运行它,问题是,脚本在运行时出错,如下所示:
Missing expression after unary operator '-'.
At C:\Users\Administrator.MyDomain\Desktop\ComputersWithStaticIP.ps1:24 char:8
+ - <<<< Query "select IPAddress, DefaultIPGateway from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE an
d DHCPEnabled=FALSE";
+ CategoryInfo : ParserError: (-:String) [], ParseException
+ FullyQualifiedErrorId : MissingExpressionAfterOperator
我追踪了从脚本中删除“-”的方法,然后得到了这个错误:
ForEach-Object : Cannot bind parameter 'Process'. Cannot convert the " " value
of type "System.String" to type "System.Management.Automation.ScriptBlock".
At C:\Users\Administrator.MyDomain\Desktop\ComputersWithStaticIP.ps1:18 char:2
+ % <<<< `
+ CategoryInfo : InvalidArgument: (:) [ForEach-Object], Parameter
BindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerSh
ell.Commands.ForEachObjectCommand
我究竟做错了什么?或者我应该从脚本中添加/删除什么以使其运行?
谢谢,