PowerShell调用-命令使用stdout和stderr检索从VBA交换管理外壳,但不弹出凭据 [英] powershell invoke-command exchange mgmt shell from vba with stdout and stderr retrieval and no credential popup

查看:18
本文介绍了PowerShell调用-命令使用stdout和stderr检索从VBA交换管理外壳,但不弹出凭据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下要求:

以域管理员用户身份登录到管理客户端计算机 我希望使用调用在Exchange服务器上执行一些更改 通过PowerShell从VBA(EXCEL 2013)到Exchange服务器(2013)。 客户端计算机运行Windows 10(1809)和PowerShell v5.1.17763.1

在VBA EXCEL表单中按下按钮后,我想执行一项琐碎的操作 任务,如获取特定邮箱用户的所有信息,阅读 使用WSH.Shell从stdout/stderr返回结果,稍后会有更多信息。

执行下面的命令会做它应该做的事情,但有以下两个缺点:

1)尽管已作为$cred via-ArgumentList传递给脚本块,但仍会再次请求凭据

2)PowerShell窗口在处理后不自动关闭,它需要 由用户主动关闭

最后,检索到的stdout/stderr得到了我想要的东西(顺便问一下,是否可以将PowerShell对象检索到VBA集合中?)

在命令行上工作("一行程序"),但必须通过弹出窗口提供凭据:

powershell -Command { $Username = 'MYDOMAINAdministrator'; $Password = 'foobar'; $pass = ConvertTo-SecureString -AsPlainText $Password -Force; $Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass; Invoke-Command -ComputerName Exchange -ArgumentList $Cred -ScriptBlock { $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://exchange.somewhere.com/PowerShell/ -Authentication Kerberos -Credential $Cred; Import-PSSession $Session; Get-Mailbox MYUSER; Remove-PSSession $Session } }
通过WSH.Shell Exec从VBA工作,但必须通过弹出窗口提供凭据,并必须主动关闭PowerShell控制台窗口 (看到我在PowerShell脚本中避免了双引号("),还没有想出如何正确转义它们(""不起作用)):

powershell -Command "& { $Username = 'MYDOMAINAdministrator'; $Password = 'foobar'; $pass = ConvertTo-SecureString -AsPlainText $Password -Force; $Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass; Invoke-Command -ComputerName Exchange -ArgumentList $Cred -ScriptBlock { $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://exchange.somewhere.com/PowerShell/ -Authentication Kerberos -Credential $Cred; Import-PSSession $Session; Get-Mailbox MYUSER; Remove-PSSession $Session } }"
所以基本上它是在写 PowerShell-命令"&;{...}" 在VBA中通过‘wsh外壳exec’调用时,而不是 PowerShell-命令{...} 在命令行上,这似乎是正确检索stdin/stdout所必需的,如果有建议为什么是这样,或者是否也有替代样式来编写它,将很高兴。

如何摆脱要求凭据的PowerShell弹出窗口的任何建议 以及如何摆脱PowerShell窗口不会自动消失?

谢谢, 杰夫

附注: 供您参考的是执行PowerShell调用的VBA方法(End Function和#如果代码块中的内容被破坏,您将找出它):

Public Function execPSCommand(ByVal psCmd As String, Optional ByVal label As String = "Debug") As String()
Dim oShell, oExec

Dim retval(2) As String
retval(0) = ""
retval(1) = ""

Set oShell = CreateObject("WScript.Shell")
Set oExec = oShell.Exec(psCmd)

oExec.stdin.Close ' close standard input before reading output

Const WshRunning = 0

Do While oExec.Status = WshRunning
    Sleep 100 ' Sleep a tenth of a second
Loop

Dim stdout As String
Dim stderr As String

stdout = oExec.stdout.ReadAll
stderr = oExec.stderr.ReadAll

retval(0) = stdout
retval(1) = stderr

execPSCommand = retval()

结束函数

' Sleep (fractions of a second, milliseconds to be precise) for VBA
‘#如果VBA7,则 ‘将PtrSafe子休眠Lib声明为"kernel32"(ByVal dw毫秒为长) ‘#其他 ‘声明子休眠Lib"kernel32"(ByVal以毫秒为长) ‘#End If

推荐答案

我认为您没有正确地将$CredD参数传递给脚本块。如果要使用局部变量,则脚本块应以param($cred)开头。为什么不在脚本块中定义$cred呢?您还可以使用USING MODIFIER将局部变量推送到远程命令(如$USING:CRED,请参阅更多详细信息https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_remote_variables?view=powershell-6)

关于末尾退出PowerShell,我想您只需在命令末尾键入"Exit"或"Stop-Process$Pid"即可。

这篇关于PowerShell调用-命令使用stdout和stderr检索从VBA交换管理外壳,但不弹出凭据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆