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 - Fatal编程技术网

Powershell 为什么-就像不工作,但-匹配工作?

Powershell 为什么-就像不工作,但-匹配工作?,powershell,Powershell,我从这里得到了这个脚本,它可以用于卸载应用程序,但只有在前两行中使用了-match而不是-like时,即使我使用了整个应用程序名 应用程序的名称包括版本,因此我想在脚本中通配符名称以支持“MyApp 2.4.1”等。谢谢 $uninstall32 = gci "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall" | foreach { gp $_.PSPath } | ? { $_ -like

我从这里得到了这个脚本,它可以用于卸载应用程序,但只有在前两行中使用了
-match
而不是
-like
时,即使我使用了整个应用程序名

应用程序的名称包括版本,因此我想在脚本中通配符名称以支持“MyApp 2.4.1”等。谢谢

$uninstall32 = gci "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall" | 
     foreach { gp $_.PSPath } | ? { $_ -like "MyApp*" } | select UninstallString

$uninstall64 = gci "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" | 
     foreach { gp $_.PSPath } | ? { $_ -like "MyApp*" } | select UninstallString

if ($uninstall64) {
   $uninstall64 = $uninstall64.UninstallString -Replace "msiexec.exe","" -Replace "/I","" -Replace "/X",""
   $uninstall64 = $uninstall64.Trim()

   Write "Uninstalling..."
   start-process "msiexec.exe" -arg "/X $uninstall64 /qb" -Wait
}

if ($uninstall32) {
   $uninstall32 = $uninstall32.UninstallString -Replace "msiexec.exe","" -Replace "/I","" -Replace "/X",""
   $uninstall32 = $uninstall32.Trim()

   Write "Uninstalling..."
   start-process "msiexec.exe" -arg "/X $uninstall32 /qb" -Wait
}

考虑以下字符串“MyApp 2.3.4”,同时我们将查看您提到的两个示例之间的重要区别:

  • ?{$\类似于“MyApp*”}
  • 匹配
    ?{$\匹配“MyApp*”}
-Like
正在查找以“MyApp”开头的字符串,后跟任何内容
-Match
正在查找后跟任何字符的文本“MyApp”。类似于
-Like的
将失败,因为前面有空格
-Match
将“MyApp*”视为一个正则表达式字符串,该字符串查找后跟任何字符的“MyApp”。在这种情况下,它不关心要匹配的空间。我怀疑如果您也更改了
-match
会失败?{$\匹配“^MyApp*”},插入符号表示字符串的开头

如果您想让
-like
在这种情况下工作,您应该将其更改为
?{$\类似“*MyApp*”}

重要


虽然我对你的比较不起作用的原因是正确的,但我首先要说明的是这个问题发生在你身上的原因

请考虑以下字符串“MyApp 2.3.4”,同时我们将查看您提到的两个示例之间的重要区别:

  • ?{$\类似于“MyApp*”}
  • 匹配
    ?{$\匹配“MyApp*”}
-Like
正在查找以“MyApp”开头的字符串,后跟任何内容
-Match
正在查找后跟任何字符的文本“MyApp”。类似于
-Like的
将失败,因为前面有空格
-Match
将“MyApp*”视为一个正则表达式字符串,该字符串查找后跟任何字符的“MyApp”。在这种情况下,它不关心要匹配的空间。我怀疑如果您也更改了
-match
会失败?{$\匹配“^MyApp*”},插入符号表示字符串的开头

如果您想让
-like
在这种情况下工作,您应该将其更改为
?{$\类似“*MyApp*”}

重要


虽然我对你的比较不起作用的原因是正确的,但我首先要说明的是这个问题发生在你身上的原因

Get ItemProperty
生成一个对象,而不是字符串。检查
DisplayName
属性而不是对象本身。您还应该展开卸载字符串,这样以后就不需要使用
$uninstall64.UninstallString

$uninstall64 = gci "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" | 
                 foreach { gp $_.PSPath } |
                 ? { $_.DisplayName -like "MyApp*" } |
                 select -Expand UninstallString
$uninstall64=gci“HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall”|
foreach{gp$\u0.PSPath}|
? {$\.DisplayName-类似于“MyApp*”}|

选择-展开卸载字符串
获取项目属性
生成一个对象,而不是字符串。检查
DisplayName
属性而不是对象本身。您还应该展开卸载字符串,这样以后就不需要使用
$uninstall64.UninstallString

$uninstall64 = gci "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" | 
                 foreach { gp $_.PSPath } |
                 ? { $_.DisplayName -like "MyApp*" } |
                 select -Expand UninstallString
$uninstall64=gci“HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall”|
foreach{gp$\u0.PSPath}|
? {$\.DisplayName-类似于“MyApp*”}|

如果
-match
有效但
-like
无效,则选择-Expand UninstallString
。唯一的区别是,如果实际字符串为“1MyAPP 2.3.4”或“MyAPP 2.3.4”
-match
会找到它,因为match只关心字符串是否存在,而like不会,因为它希望第一个字符是M,依此类推。不知道M前面是否有字符。如果您将子句更改为
?{$\u-match“^MyApp”}
它仍然有效吗?我想您是对的。我在name属性中看不到任何前导字符,但我在开头添加了一个
*
,效果很好。请随意提交答案。谢谢如果
-match
有效,但
-like
无效,唯一的区别是,如果实际字符串是“1MyAPP 2.3.4”或“MyAPP 2.3.4”
,则match
会找到它,因为match只关心字符串是否存在,而like不会,因为它希望第一个字符是M,依此类推。不知道M前面是否有字符。如果您将子句更改为
?{$\u-match“^MyApp”}
它仍然有效吗?我想您是对的。我在name属性中看不到任何前导字符,但我在开头添加了一个
*
,效果很好。请随意提交答案。谢谢废话。。。这就是他对
-like
-match
有问题的原因。正在比较表示注册表项的整个字符串。“MyAPP”出现在中间。我是对的,为什么它不起作用,但这解决了核心问题。我喜欢足球。。。这就是他对
-like
-match
有问题的原因。正在比较表示注册表项的整个字符串。“MyAPP”出现在中间。我是对的,为什么它不起作用,但这解决了核心问题。我爱死你了