首页 > 解决方案 > 无法在powershell中执行字符串

问题描述

somescript.ps1 将返回一个字符串,它是一个命令

$PATHS = @("HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall", "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall") $SOFTWARE = "Microsoft Azure Information Protection" ForEach ($path in $PATHS) { $installed = Get-ChildItem -Path $path | ForEach { Get-ItemProperty $_.PSPath } | Where-Object { $_.DisplayName -match $SOFTWARE } | Select-Object -Property DisplayName,DisplayVersion,UninstallString ForEach ($app in $installed) { Write-Output "$($app.UninstallString)"} }

现在,我有一个命令,它是从上述查询返回的字符串,如下所示:

PS D:\FolderName> .\somescript.ps1
"C:\ProgramData\Package Cache\{21b41fEE-93c0-498f-a284-659d275b4076}\AzInfoProtection.exe"  /uninstall
MsiExec.exe /I{7FA8B359-E9D7-4037-8DE3-A28F2603D743}

我需要执行上面返回的字符串,这是 PowerShell 中的一个命令

"C:\ProgramData\Package Cache{21b41fEE-93c0-498f-a284-659d275b4076}\AzInfoProtection.exe" /卸载 MsiExec.exe /I{7FA8B359-E9D7-4037-8DE3-A28F2603D743}

我收到如下错误

PS C:\ProgramData\Package Cache> C:\ProgramData\Package Cache{21b41fEE-93c0-498f-a284-659d275b4076}\AzInfoProtection.exe /uninstall MsiExec.exe /I{7FA8B359-E9D7-4037-8DE3-A28F2603D743}

C:\ProgramData\Package : The term 'C:\ProgramData\Package' is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ C:\ProgramData\Package Cache\{21b41fEE-93c0-498f-a284-659d275b4076}\A ...
+ ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\ProgramData\Package:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

如何修复在 PowerShell 中执行此命令的命令?

标签: powershellwindows-installeruninstallationpowershell-4.0

解决方案


推荐阅读