首页 > 解决方案 > 使用 test-path 的结果执行 powershell 命令

问题描述

如果系统上不存在文件/文件夹,则希望执行将卸载应用程序并清除一些注册表项的 powershell 命令。

目前正在看这样的东西:

if(!(test-path “c:\Windows\mdm” )
    {
    $productdetails=(Get-WmiObject -Query “Select IdentifyingNumber from win32_product where name = ‘Tesapp’“).identifyingnumber
    $proc=(Start-Process -FilePath “msiexec.exe” -ArgumentList “/x $productdetails /qn /l*v c:\temp\Uninstall-testapp.log” -Wait -PassThru).ExitCode
if (!($proc -eq 0 -or $proc -eq 3010))
 {
    exit(1)
    }
$RegKey=“HKLM:\SOFTWARE\Microsoft\TestAppManagement\S-0-0-00-0000000000-0000000000-000000000-000\MSI\{0}” -f $productdetails
if(test-path $RegKey)
    {
   $op= Get-Item $RegKey | Out-File “c:\windows\temp\TestApp_Reg_backup.reg” -Force
   $op= Remove-Item $RegKey -Force
    }
    }

有趣的是,如果我运行测试路径,它将返回预期的 false,或者如果单独运行应用程序卸载/注册清除,它将按预期工作 - 但是我不能一起运行它们。

我假设我正在犯某种语法/基本逻辑错误?

标签: powershell

解决方案


推荐阅读