如何在不使用UserName和密码的情况下在C#中执行Powershell脚本?
Powershell脚本:
#Script:This Script generate the Package (Zip file) dynamically for unittest
#DirectoryName is Out Directory of Unittest execution
#location is Out\Resource Directory
#locationdetail is Array of files Directory for generate the Package
#FileName is name of Package generated through this script
#option is use for switch case implementation,currently not implemented
param(
[string]$DirectoryName="."
, [string]$location=""
, [string]$FileName="Test.zip"
, [int] $option
)
[System.Reflection.Assembly]::LoadFrom($DirectoryName+"\\"+"Ionic.Zip.dll");
$locationdetail = "Package\\Resource", "Package\\ResourceBad", "Package\\ResourceEmptymetada","Package\\ResourceEmptydatafile","Package\\EmptyTest"
$directoryToZip=""
foreach ($element in $locationdetail)
{
If($element -eq "Package\\Resource")
{
$directoryToZip= $location+"\\"+"Package\\Resource"
$FileName ="Package (Good Data).zip"
}
If($element -eq "Package\\ResourceBad")
{
$directoryToZip= $location+"\\"+"Package\\ResourceBad"
$FileName ="Package (Bad Data).zip"
}
If($element -eq "Package\\ResourceEmptymetada")
{
$directoryToZip= $location+"\\"+"Package\\ResourceEmptymetada"
$FileName ="Package (Bad with ManifestEmpty).zip"
}
If($element -eq "Package\\ResourceEmptydatafile")
{
$directoryToZip= $location+"\\"+"Package\\ResourceEmptydatafile"
$FileName ="Package (Bad with Datafile Empty).zip"
}
If($element -eq "Package\\EmptyTest")
{
$directoryToZip= $location+"\\"+"Package\\EmptyTest"
$FileName ="EmptyTest.zip"
}
$zipfile = new-object Ionic.Zip.ZipFile
$e= $zipfile.AddDirectory($directoryToZip)
$zipfile.Save( $location+"\\"+$FileName)
$zipfile.Dispose()
$directoryToZip=""
}
和我的C#代码
private static void StartPowerShell(string args,string TempScript)
{
string powerShellPath ="C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe";
string commandLine = @" -Sta -Command ""& {{ try {{ {0} }} catch {{throw}} }} """.InvariantCultureFormat(args);
var info = new ProcessStartInfo();
info.FileName = powerShellPath;
info.Arguments = commandLine;
info.WorkingDirectory = Environment.CurrentDirectory;
info.UseShellExecute = false;
info.CreateNoWindow = false;
info.RedirectStandardError = true;
info.RedirectStandardOutput = true;
//Pass the Username and Password here in ProcessStartInfo
info.UserName = "Saroop";
string pw = "saroop";
System.Security.SecureString password = new System.Security.SecureString();
foreach (var item in pw)
{
password.AppendChar(item);
}
info.Password = password;
info.RedirectStandardError = true;
info.RedirectStandardOutput = true;
Process p = new Process();
p.StartInfo = info;
bool flag = p.Start();
string error = p.StandardError.ReadToEnd();
p.WaitForExit();
}
请指导我。我尝试使用runas命令以用户(管理员)身份启动我的Powershell.exe,但它也要求输入用户名和密码。 我尝试使用System.Management.Automation。 dll但它不再存在于.Net4.0中 参考链接: 的 http://msdn.microsoft.com/en-us/library/system.management.automation(V = vs.85)的.aspx
请帮帮我。
答案 0 :(得分:1)
你在评论中写的内容是有效的,但是我想给你另一个方向来运行C#代码中的PowerShell脚本。
您可以使用System.Management.Automation.Runspaces
命名空间,它可以显示非常简单,更优雅的方式来运行powershell命令并从C#中检索结果(并使用返回的对象而不仅仅是输出文本流)。试着阅读它。
答案 1 :(得分:1)
我找到了我的查询答案。 执行POWERSHELL脚本必须执行政策命令 SYSTEM32 C:\ WINDOWS \ SYSTEM32 \ WindowsPowerShell \ V1.0 \ powershell.exe 如果你的系统是64位,那么它也执行相同的政策为64BIT POWERSHELL.EXE太。 C:\的Windows \ Syswow64资料\ WindowsPowerShell \ V1.0 \ POWERSHELL.EXE
/// COMMAND : `Set-ExecutionPolicy Unrestricted`