首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Powershell脚本独立运行,但在作为可执行文件运行时出现“方法调用失败”错误。

Powershell脚本独立运行,但在作为可执行文件运行时出现“方法调用失败”错误。
EN

Stack Overflow用户
提问于 2020-05-31 13:18:49
回答 1查看 266关注 0票数 0

下面是我的powershell脚本片段,用于获取所有已安装的应用程序:

代码语言:javascript
复制
$tPatchObject = @() #initialize array

#invoking the native powershell(32 bit/64 bit)
$tPatchObject =  &"$env:systemroot\sysnative\windowspowershell\v1.0\powershell.exe" -NonInteractive -NoProfile -Command {Get-Childitem "HKLM://SOFTWARE//Microsoft//Windows//CurrentVersion//Uninstall//*" | Get-ItemProperty | Sort-Object DisplayName -Unique;};

#appending applications from a different path to same array
$tPatchObject += &"$env:systemroot\sysnative\windowspowershell\v1.0\powershell.exe" -NonInteractive -NoProfile -Command {Get-Childitem "HKLM://SOFTWARE//Wow6432Node//Microsoft//Windows//CurrentVersion//Uninstall//*" | Get-ItemProperty | Sort-Object DisplayName -Unique}

在Windows 2008 R2系统上进行测试时,如果直接从powershell执行,它就会给出适当的输出。但是,在Golang中编译的可执行文件的一部分执行相同的脚本会产生错误。

方法调用失败,因为System.Management.Automation.PSObject不包含名为“op_a”的方法。

仅在Windows服务器2008系统上才会面临此问题。我的假设是由于调用&"powershell.exe"而出现的问题

EN

回答 1

Stack Overflow用户

发布于 2020-06-01 08:18:30

试试这个,LookPath

代码语言:javascript
复制
cmd := `Get-Childitem "HKLM://SOFTWARE//Microsoft//...`

ps, _ := exec.LookPath("powershell.exe")
args := append([]string{"-NoProfile", "-NonInteractive", cmd})

out, _ := exec.Command(ps, args...).Output()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62116849

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档