【问题标题】:Confirm prompt using Powershell使用 Powershell 确认提示
【发布时间】:2015-05-12 21:15:44
【问题描述】:

我正在使用 powershell 来自动执行日常任务。它本质上是一个宏,用于刷新链接到外部源的数据,然后发送电子邮件。问题是 Excel 在尝试刷新数据时会自动提示输入用户名/密码。我做了一些挖掘,它不能通过设置 DisplayAlert = False 来绕过,也不能从 Excel 的设置中更改。我的凭据已保存,所以我真正需要做的是有一个脚本来按“Enter”。

我目前有以下设置:使用 Windows 调度程序,我首先启动一个 Powershell 脚本,该脚本将打开 Excel 并运行宏以刷新数据。当 Excel 提示输入凭据时,宏将被卡住。我有第二个任务,它计划在第一个任务后 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}")

知道如何绕过/接受提示吗?

提前致谢。

【问题讨论】:

  • 也许这个link 可以帮助你。它告诉您编辑.odc 文件以确保密码已保存...

标签: excel powershell


【解决方案1】:

我们使用的解决方案与您已经尝试过的略有不同。也许是在细节中......

[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}")

【讨论】:

    【解决方案2】:

    请分享数据链接以获取更多详细信息,但是,如果确实有原因可以保存通行证和登录名,我会简单地以编程方式重新创建数据链接,例如一个接受 pass 和 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
    

    【讨论】:

    • 数据链接来自第三方公司。 excel 文件是他们创建的,用于从受密码保护的网站中提取数据。
    • 这是什么数据连接?我们需要更多信息。右键单击数据集以获取更多信息。
    • 这是到他们网站的连接,受用户名/密码保护。数据连接字符串类似于这样:website.com/viewserver/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-15
    • 2017-07-26
    • 2019-09-26
    • 1970-01-01
    • 2019-06-28
    • 2016-12-15
    • 2015-08-28
    相关资源
    最近更新 更多