使用Powershell确认提示

我正在使用PowerShell来自动执行日常任务。 这实质上是一个macros,刷新链接到外部源的数据,然后发送一封电子邮件。 问题是Excel会在每次尝试刷新数据时自动提示input用户名/密码。 我做了一些挖掘,它不能通过设置DisplayAlert = False绕过,不能从Excel的设置更改。 我的凭证被保存了,所以我真正需要做的是让脚本按下“Enter”键。

我目前有以下设置:使用Windows计划程序,我首先启动一个Powershell脚本,它将打开Excel并运行macros来刷新数据。 当Excel提示input凭据时,macros将被卡住。 我有第二个任务,计划在第一个任务后2分钟运行,目的是按下“Enter”键来摆脱提示。

我已经尝试了几个脚本的第二个任务,但我似乎无法得到正确的。 我试过的是如下:

[void] [System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic") [void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms") $wshell = New-Object -ComObject wscript.shell; $wshell.AppActivate("Windows Security") [System.Windows.Forms.SendKeys]::SendWait("{ENTER}") 

 $excel = [Runtime.Interopservices.Marshal]::GetActiveObject('Excel.Application') [void] [System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic") [void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms") [System.Windows.Forms.SendKeys]::SendWait("{ENTER}") 

任何想法如何绕过/接受提示?

提前致谢。

我们使用的解决scheme与您已经尝试过的稍有不同。 也许是在细节…

 [void] [System.Reflection.Assembly]::LoadWithPartialName ("'Microsoft.VisualBasic") [Microsoft.VisualBasic.Interaction]::AppActivate("Windows Security") [void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms") [System.Windows.Forms.SendKeys]::SendWait("{ENTER}") 

请分享数据链接的更多细节,但是,我只是简单地重新创build数据链接编程,如果确实有一个原因,为什么通过和login可以保存,例如一个macros接受通行证和login

如果它是一个SQL连接,那么它看起来像这样:

 Sub Recreate(serverInstance as String, database as String, userId as String, password as String) sConn = "OLEDB;Provider=SQLOLEDB;Data Source=" & _ serverInstance & ";Initial Catalog=" & database & _ ";User ID=" & userId & ";Password=" & password & ";" Set qt = wks.QueryTables.Add(Connection:=sConn, _ Destination:=Target) With qt .CommandType = xlCmdSql .CommandText = sql .Name = sName .RefreshStyle = xlOverwriteCells .Refresh BackgroundQuery:=False 'Execute SQL End With End Sub