简体   繁体   English

Add-PSSnapin'Microsoft.SharePoint.PowerShell'在ISE中运行,但不是从.Net Process.Start命令运行

[英]Add-PSSnapin 'Microsoft.SharePoint.PowerShell' runs in ISE but not from .Net Process.Start command

I have an automation issue that I've tracked down to the following situation: 我遇到了一个自动化问题,可追溯到以下情况:

The PS script is being loaded via process.start in a C# program. PS脚本通过C#程序中的process.start加载。 If I run the script in the ISE, it runs fine. 如果我在ISE中运行脚本,则可以正常运行。 When running via process.start, it fails on the command: 通过process.start运行时,该命令失败:

Add-PSSnapin 'Microsoft.SharePoint.PowerShell'

With the error: 与错误:

Add-PSSnapin : No snap-ins have been registered for Windows PowerShell version 4.

The command I'm using for process.start in .net is: 我在.net中用于process.start的命令是:

var Arguments @"-version 4.0 -ExecutionPolicy Bypass -noexit -file """ + filePath + @""" -XmlString """ + args + @""" -Verb RunAs"
var FileName = "powershell.exe"

var process = Process.Start(FileName, Arguments);

filePath is the path to the powershell script that is being executed. filePath是正在执行的Powershell脚本的路径。

This runs and attempts to run the script, but fails on the command shown above inside the script. 这将运行并尝试运行脚本,但是在脚本内部上面显示的命令上失败。 The user account is the admin service account for the machine. 用户帐户是计算机的管理员服务帐户。 The issue seems to be in how the .Net program is starting PowerShell, as I can run this script and these commands via the ISE and console if I do it manually (under the same service account). 问题似乎在于.Net程序如何启动PowerShell,因为如果我手动(在同一服务帐户下)可以通过ISE和控制台运行此脚本和这些命令。 I just can't seem to find some combination of tricks to get the .Net program to be able to execute the PowerShell script under the correct profile, I guess. 我想,我似乎似乎找不到技巧的组合来使.Net程序能够在正确的配置文件下执行PowerShell脚本。

Any thoughts? 有什么想法吗?

PetSerAl's request was spot-on... when running the PowerShell script via the .Net program, it was running under x86, but when running it via ISE, it was running in x64. PetSerAl的请求是紧急的……通过.Net程序运行PowerShell脚本时,它在x86下运行,但是通过ISE运行时,它在x64中运行。 I re-compiled the .Net program against x64 and everything now works. 我针对x64重新编译了.Net程序,现在一切正常。

Thanks for the advice! 谢谢你的建议!

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

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