【发布时间】: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