大佬教程收集整理的这篇文章主要介绍了使用 C# 运行 Windows Powershell 文件,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
12 小时后,我还在为这个头发拔毛!
我有这个 @H_836_5@myScript.ps1 文件:
import-module 'C:\Program files\windowsPowerSHell\Modules\sqlserver'
Invoke-sqlcmd -query "SELECT * FROM MyDatabase.dbo.tb_myTable" `
-ConnectionString "password=blah;Persist Security Info=True;User ID=foo;Initial Catalog=MyDatabase;Data source=MyServer" | `
Export-Csv -Path "C\dataextract.csv" -NoTypeinformation
在我们的服务器(64 位 windows Server 2016 标准)上,我可以右键单击 ps1
文件并选择在 PowersHell 中运行,它按预期工作 - 提取数据并将其保存到 csv
文件.
然后我尝试通过以下 C#
处理这个 powersHell 文件:
String aPath = @"C\somefolder\";
String aname = "myScript.ps1";
aProcess = new Process();
String sTinfofilename = null;
String sTinfoArgs = null;
//sTinfofilename = "powersHell.exe"; //<<<ATTEMPT 1
//sTinfofilename = @"C:\windows\System32\windowsPowerSHell\v1.0\powersHell.exe"; //<<<ATTEMPT 2
sTinfofilename = @"C:\windows\SysWOW64\windowsPowerSHell\v1.0\powersHell.exe";
sTinfoArgs = String.Format(" -ExecutionPolicy unreStricted -file \"{0}{1}\"",aPath,aName);
this.aProcess.StarTinfo.filename = sTinfofilename;
this.aProcess.StarTinfo.Arguments = sTinfoArgs;
this.aProcess.StarTinfo.UseSHellExecute = false;
this.aProcess.Start();
aProcessname = this.aProcess.processname;
this.aProcess.WaitForExit();
this.aProcess.dispose();
我收到此错误消息:
Invoke-sqlcmd:无法加载文件或程序集 'Microsoft.sqlServer.batchParser,版本=15.100.0.0,文化=中性, PublicKeyToken=89845dcd8080cc91' 或其依赖项之一。这 系统找不到指定的文件。
我在 ps1
文件中尝试了两种替代方法:
两者都工作正常,但每种方法都更加冗长。
我如何帮助这个程序找到它需要的东西来找到它需要的程序集?这些程序集肯定在机器上,因为运行 ps 脚本证明了这一点,但出于某种原因,在 c#
中它不知道它们?
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
以上是大佬教程为你收集整理的使用 C# 运行 Windows Powershell 文件全部内容,希望文章能够帮你解决使用 C# 运行 Windows Powershell 文件所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。