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脚本|查找前两个八位字节的匹配项_Powershell_Conditional Statements - Fatal编程技术网

Powershell脚本|查找前两个八位字节的匹配项

Powershell脚本|查找前两个八位字节的匹配项,powershell,conditional-statements,Powershell,Conditional Statements,我有一个脚本,可以从运行它的计算机上获取活动IP地址。如果前两个八位组包含该数字,它将输出“我在PH中” 因此,如果一个地址是10.4.20.52,我需要创建一个决定,如果IPAddress-包含“10.4”,则写“我在PH中”,否则写“我不在PH中” 我试着使用Like-iLike-and-contains,但它们似乎都不起作用 $NICIndex = Get-CimInstance -ClassName Win32_IP4RouteTable | Where-Object { $_.

我有一个脚本,可以从运行它的计算机上获取活动IP地址。如果前两个八位组包含该数字,它将输出“我在PH中”

因此,如果一个地址是10.4.20.52,我需要创建一个决定,如果IPAddress-包含“10.4”,则写“我在PH中”,否则写“我不在PH中”

我试着使用Like-iLike-and-contains,但它们似乎都不起作用

$NICIndex = Get-CimInstance -ClassName Win32_IP4RouteTable |
    Where-Object { $_.Destination -eq "0.0.0.0"-and $_.Mask -eq "0.0.0.0" } |
    Sort-Object Metric1 |
    Select-Object -First 1 |
    Select-Object -ExpandProperty InterfaceIndex
$AdapterConfig = Get-CimInstance -ClassName Win32_NetworkAdapter |
    Where-Object { $_.InterfaceIndex -eq $NICIndex } |
    Get-CimAssociatedInstance -ResultClassName Win32_NetworkAdapterConfiguration
$ipconfig = (Get-First $AdapterConfig.IPAddress);


$ipconfig


if ($ipconfig -contains '10.4.')
{
  Write-host "I'm in PH"  
}
else
{
  Write-host "I'm not in PH"
}

有什么想法吗?

我想我刚刚找到了答案,我用了-Match,它就成功了。没关系,我猜
-contains
用于查看数组是否包含元素。它与子字符串不匹配。@TheInfamousOne如果您找到了自己问题的答案,请将其作为答案发布,以便其他有相同问题的人可以清楚地看到答案。
if ($ipconfig -like '10.4.*')  # or if ($ipconfig -match '^10\.4\.')
{
  Write-host "I'm in PH"  
}
else
{
  Write-host "I'm not in PH"
}