Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.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 如果对数组使用-notlike不起作用_Powershell - Fatal编程技术网

Powershell 如果对数组使用-notlike不起作用

Powershell 如果对数组使用-notlike不起作用,powershell,Powershell,我现在太笨了,解决不了问题 我需要一个IF查询来搜索整个数组,然后确定是否满足条件。 在示例中,$array[0]已满足条件。。。威奇提前到了,因为$arry[1…]可能满足条件。 我目前正在使用foreach查询,如果在数组中找到13.2.*,则将变量$match设置为1。 但不知何故,这对我来说似乎很愚蠢,而且它必须更容易工作,对吗 $pattern值只是一个示例。 写主机当然只是简单地呈现它 这行不通: $Regpath="HKLM:\SOFTWARE\Microsoft\Win

我现在太笨了,解决不了问题

我需要一个IF查询来搜索整个数组,然后确定是否满足条件。
在示例中,$array[0]已满足条件。。。威奇提前到了,因为$arry[1…]可能满足条件。 我目前正在使用foreach查询,如果在数组中找到13.2.*,则将变量$match设置为1。
但不知何故,这对我来说似乎很愚蠢,而且它必须更容易工作,对吗

$pattern值只是一个示例。
写主机当然只是简单地呈现它

这行不通:

$Regpath="HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
$Pattern="*SQL*Server*"  
$array=Get-ChildItem -Path $Regpath | Get-ItemProperty | Where-Object { $_.Displayname -like $Pattern }

PS C:\> $array.displayversion  

13.0.3225.4 
13.2.5026.0

PS C:\> if ($array.displayversion -notlike "*13.2*") {write-host "Install msi File with Version 13.2.5026.0"}  
Install msi File with Version 13.2.5026.0
使用If查询的工作Foreach循环:

$match=0  
foreach ($Search in $array.displayversion) 
{   if ($Search -like "*13.2.*") { $match=1}} 
if ($match -ne "1") 
{ write-host "Install msi File with Version 13.2.5026.0" }

您可以在管道中扩展
Where对象
,以尝试查找您要查找的版本。如果
$array
变量在查询结束时不是
[System.Management.Automation.PSCustomObject]
对象,则未找到任何内容

$array = Get-ChildItem -Path $Regpath | Get-ItemProperty | Where-Object { (($_.Displayname -like $Pattern)-and ($_.DisplayVersion -eq "13.2.5026.0")) }

if ($array -isnot [System.Management.Automation.PSCustomObject]) { # If it was not able to meet the Where-Object criteria it will not create the corresponding object.
    #do something
}

您可以在管道中扩展
Where对象
,以尝试查找您要查找的版本。如果
$array
变量在查询结束时不是
[System.Management.Automation.PSCustomObject]
对象,则未找到任何内容

$array = Get-ChildItem -Path $Regpath | Get-ItemProperty | Where-Object { (($_.Displayname -like $Pattern)-and ($_.DisplayVersion -eq "13.2.5026.0")) }

if ($array -isnot [System.Management.Automation.PSCustomObject]) { # If it was not able to meet the Where-Object criteria it will not create the corresponding object.
    #do something
}

我认为这是你想要的行为:

if ( -not ($array.displayversion -like "*13.2*") ) {
  write-host "Install msi File with Version 13.2.5026.0"}  

我认为这是你想要的行为:

if ( -not ($array.displayversion -like "*13.2*") ) {
  write-host "Install msi File with Version 13.2.5026.0"}  

对于这个场景,我将使用
switch
语句。不管怎样,我相信问题在于,当你引用它时,它可能是把它当作一个字符串来读的。似乎没有引号也行,所以你能删除双引号和13之前的第一个星号吗?就为了测试目的,据我所知,它确实按照预期的方式工作:。也就是说,如果
$array.displayversion
项中的任何项与
“*13.2*”
不匹配,则条件将确定。如果
条件应如何工作?如果任何值匹配13.2*?@MathiasR.Jessen,则为True:如果没有值匹配13.2
if(!($array.displayversion-类似“*13.2*”),则为True{…
。另请参见,我会在这种情况下使用
switch
语句。无论如何,我相信问题在于,当你引用它时,它可能会以字符串的形式读取它。似乎在没有引号的情况下它可以工作,所以你可以删除双引号,以及13之前的第一个星号吗?仅用于测试目的。据我所知,它的工作方式与我的相同t应该工作:。这意味着,如果
$array.displayversion
项中的任何项与
“*13.2*”
不匹配,则条件会验证。如果
if
条件如何工作?如果任何值与13.2*?@MathiasR匹配,则为True。Jessen:如果没有值与13.2
if(!($array.displayversion-类似“*13.2*”)匹配,则为True){…
。另请参见您好,谢谢您的回答。本机似乎不存在任何可能性。但是-not是一个完美的解决方案。现在我必须记住这一点,以备将来使用!您好,谢谢您的回答。本机似乎不存在任何可能性。但是-not是一个完美的解决方案。现在我必须记住这一点,以备将来使用!当然,th也是一个解决方案。数组由函数提供给我,然后我必须扩展它。谢谢提示!当然,这也是一个解决方案。数组由函数提供给我,然后我必须扩展它。谢谢提示!