【问题标题】:Set-ItemProperty can't find path when using CreateProcess and Powershell Start-Process in VBA在 VBA 中使用 CreateProcess 和 Powershell Start-Process 时,Set-ItemProperty 找不到路径
【发布时间】:2020-08-10 15:49:40
【问题描述】:

我需要使用 CreateProcess 和 Powershell 从 VBA 脚本中更改 reg 键。下面你会找到我的 VBA 代码。

当我直接在 Powershell 中运行引号内的代码或使用 Windows 运行时,注册表项会按应有的方式设置。当我使用这个 VBA 代码时,我得到一个路径错误。

路径绝对正确。在同一个 VBA 脚本中,我使用相同的命令设置了一些其他 reg 键,没有错误。我不知道错误可能是什么。我在两个新的 Windows 10 虚拟机上测试了脚本,结果相同。

提前感谢您的帮助。

Dim pInfo As PROCESS_INFORMATION
Dim sInfo As STARTUPINFO
Dim sNull As String
Dim lSuccess As Long
Dim lRetValue As Long

lSuccess = CreateProcess(sNull, "powershell start-process -filepath powershell.exe -Argumentlist 'Set-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Windows` Defender` Security` Center\Notifications -Name DisableNotifications -Value 1' -verb RunAs -windowstyle hidden -Wait", ByVal 0&, ByVal 0&, 1&, CREATE_NO_WINDOW, ByVal 0&, sNull, sInfo, pInfo)
Set-ItemProperty : Der Pfad "HKLM:\SOFTWARE\Microsoft\Windows Defender Security Center\Notifications" kann nicht
gefunden werden, da er nicht vorhanden ist.
In C:\Users\public\psc.ps1:9 Zeichen:1
+ Set-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Windows` Defender` Se ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (HKLM:\SOFTWARE\...r\Notifications:String) [Set-ItemProperty], ItemNotFo
   undException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetItemPropertyCommand


Note---------------------------------------
(The german stuff at the beginning means the following)
The path "HKLM:\SOFTWARE\Microsoft\Windows Defender Security Center\Notifications" cannot be found, because it does not exist"

编辑1:

令人惊讶的是,当我在那里运行我刚刚发现的脚本时,我的开发虚拟机上正在设置 reg 密钥。所以我会假设 Windows 或安装的软件有问题。我已经测试了我能想到的所有差异,但没有运气

【问题讨论】:

  • 这看起来真的是一个报价问题。我建议你尝试这样的事情:''HKLM:\SOFTWARE\Microsoft\Windows Defender Security Center\Notifications'' Mening 你在路径周围添加 2,3 或 4 个单引号并删除路径中的 `。

标签: vba powershell registry


【解决方案1】:

我认为您的报价有问题。我建议你在路径周围加上 2 个单引号并删除路径中的 `:

例如。

$mystring = 'Get-ItemProperty -Path ''HKLM:\SOFTWARE\Microsoft\Windows Defender Security Center\Notifications'''
$mystring

这将导致输出:

Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows Defender Security Center\Notifications'

所以你的代码应该是这样的:

Dim pInfo As PROCESS_INFORMATION
Dim sInfo As STARTUPINFO
Dim sNull As String
Dim lSuccess As Long
Dim lRetValue As Long

lSuccess = CreateProcess(sNull, "powershell start-process -filepath powershell.exe -Argumentlist 'Set-ItemProperty -Path ''HKLM:\SOFTWARE\Microsoft\Windows Defender Security Center\Notifications'' -Name DisableNotifications -Value 1' -verb RunAs -windowstyle hidden -Wait", ByVal 0&, ByVal 0&, 1&, CREATE_NO_WINDOW, ByVal 0&, sNull, sInfo, pInfo)

【讨论】:

  • 感谢您的回答,但遗憾的是,引号并不是错。我一开始也是这么想的。所以,同样的错误。令人惊讶的是,当我在那里运行我刚刚发现的脚本时,我的开发虚拟机上设置了 reg 密钥。所以我会假设 Windows 或安装的软件有问题。我已经测试了我能想到的所有差异,但没有运气。
【解决方案2】:

EDIT1:所以我终于找到了这个问题的真正解决方案和原因。如旧“解决方案”中所述,32 位和 64 位版本的 Office 之间存在问题。我尝试使用 reg add 设置注册表值,发现它向 WOW6432 写入了 32 位应用程序的兼容层。如果您知道它的存在,这一点非常明显,因为 Office 通常是一个 32 位应用程序。我假设找不到路径,因为 powershell 在 32 位 Office 的上下文中运行并尝试访问注册表的 64 位部分。 reg add 命令设置 WOW6432 中的值,这将不起作用,因为 Windows Defender 安全中心不会读取那里的值。但是有一个 /reg:64 标志,它强制 reg add 写入注册表的 64 位部分。并且一切正常。

【讨论】:

    猜你喜欢
    • 2017-06-01
    • 1970-01-01
    • 2018-06-15
    • 1970-01-01
    • 2020-08-17
    • 2019-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多