简体   繁体   English

远程执行Powershell脚本 - MSAccess未启动

[英]Executing Powershell Script Remotely - MSAccess not launching

I am trying to execute a Powershell script remotely that will launch an accdb file via MSAccess. 我试图远程执行一个Powershell脚本,它将通过MSAccess启动accdb文件。 I am able to get the Powershell script to execute successfully, but MSAccess is not launching since I know that the test.accdb file that I have is not getting updated. 我能够成功执行Powershell脚本,但是MSAccess没有启动,因为我知道我的test.accdb文件没有得到更新。 What am I missing in my code in order to be able to launch MSAccess? 为了能够启动MSAccess,我在代码中缺少什么? Or is it not possible? 还是不可能?

My code is running in a Windows 2012 R2 environment in IIS and is being executed by a service account that has Admin privileges to the machine. 我的代码在IIS中的Windows 2012 R2环境中运行,并由具有该计算机管理员权限的服务帐户执行。 If I run the code logged in as that service account, it works fine without issues. 如果我运行以该服务帐户登录的代码,它可以正常工作而不会出现问题。 If I execute it remotely, only part of the code is executed 如果我远程执行它,只执行部分代码

My code for the .NET app is as follows (running under service account). 我的.NET应用程序代码如下(在服务帐户下运行)。 I've changed some of the private information, but it doesn't effect the code. 我已经更改了一些私人信息,但它不会影响代码。

    internal static HttpStatusCode ExecuteRemoteCommand()
    {
        WSManConnectionInfo connectioninfo = new WSManConnectionInfo();
        connectioninfo.ComputerName = "testcomputer";
        using (Runspace runspace = RunspaceFactory.CreateRunspace(connectioninfo))
        {
            runspace.Open();
            using (PowerShell ps = PowerShell.Create())
            {
                ps.Runspace = runspace;
                var re = ps.AddScript(@"\\testcomputer\test\StartTest.ps1");
                var results = re.Invoke();
            }
        }

        return HttpStatusCode.Created;
    }

My Powershell code is as follows: 我的Powershell代码如下:

try {

    $msAccess = "C:\Program Files (x86)\Microsoft Office\Office14\msaccess.exe"
    $fileLocation =  "C:\test\DBT.accdb"
    Start-Process -FilePath $msAccess -ArgumentList $fileLocation -Verb RunAs -WindowStyle Hidden -WorkingDirectory "C:\test" 
    $today = Get-Date
    "SUCCESS: " + $today > "c:\test\TestExecutionSuccess.txt"

}
Catch
{
    $_.Exception.Message > "c:\test\TestExecutionError.txt"
}

In both cases , the one where I run the script locally and the one where I execute it remotely, the TestExecutionSuccess.txt file is created. 在这两种情况下 ,我在本地运行脚本和远程执行脚本的情况下都会创建TestExecutionSuccess.txt文件。

However, in ONLY the local test run is the DBT.accdb file updated. 但是,仅在本地测试运行时更新了DBT.accdb文件。

So lets talk about the problems 那么让我们谈谈这些问题

1 : You can run the code and open Excel when you run the code under your User account logged in. 1:当您运行登录的用户帐户下的代码时,您可以运行代码并打开Excel。

2 : You can run the code and open Excel when you run the code under your the service account logged in. 2:您可以在运行登录的服务帐户下的代码时运行代码并打开Excel。

3 : you can you the code But Excel will not load if you are not logged into the user. 3:你可以使用代码但是如果你没有登录用户,Excel将不会加载。

Why? 为什么?

This is about Interactive flag. 这是关于互动旗帜。 When you are logged into a user then you have can load up GUI's using COM. 当您登录用户时,您可以使用COM加载GUI。 If you are not logged into a user then you can not load a GUI. 如果您未登录用户,则无法加载GUI。

Here is a more indeepth explanation from Microsoft Interactive User 以下是Microsoft Interactive User提供的更为深刻的解释

With ArcSet's guidance, I think I was able to figure out what the issue was. 在ArcSet的指导下,我想我能够找出问题所在。 His answer led me down the path of investigating which version of powershell was running. 他的回答让我走上了调查powershell正在运行的版本的道路。 Turned out the C# code was running Powershell x64 instead of x86 (32-bit) version. 原来,C#代码运行的是Powershell x64而不是x86(32位)版本。 The office installation is 32-bit and hence why I couldn't run MSAccess. 办公室安装是32位,因此我无法运行MSAccess。

I couldn't figure out how to run x86 version of Powershell from the C# code, but what I did was put the launch of MSAccess into a batch file and then executed the batch file from my powershell code. 我无法弄清楚如何从C#代码运行x86版本的Powershell,但我所做的是将MSAccess的启动放入批处理文件,然后从我的powershell代码执行批处理文件。 Not super clean, but it worked. 不是超级干净,但它有效。 Access is now running properly. Access现在正常运行。

Here is the batch file code: 这是批处理文件代码:

cd "C:\Program Files (x86)\Microsoft Office\Office14"
MSACCESS.EXE C:\test\DBT.accdb

and the new version of the PowerShell script: 和PowerShell脚本的新版本:

try {

    $msAccess = "C:\Program Files (x86)\Microsoft Office\Office14\msaccess.exe"
    $fileLocation =  "C:\test\executeTest.bat"  
    cmd.exe /c $fileLocation
    $today = Get-Date
    "SUCCESS: " + $today > "c:\test\TestExecutionSuccess.txt"

}
Catch
{
    $_.Exception.Message > "c:\test\TestExecutionError.txt"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM