[英]Passing variables to powershell from C# : Output file location variables
I have recently began using C# to invoke powershell scripts, with some success :) 我最近开始使用C#调用Powershell脚本,并取得了一些成功:)
$spWeb = Get-SPWeb -Identity http://127.0.0.1
$listTemplate = [Microsoft.SharePoint.SPListTemplateType]::DocumentLibrary
$spWeb.Lists.Add($args, "", $listTemplate) > $myDirectory
$spWeb.Lists.Add
returns a SharePoint GUID. $spWeb.Lists.Add
返回一个SharePoint GUID。
Question: 题:
I simply wish to pass in the directory/filename in which the GUID will be written. 我只是希望传递将在其中写入GUID的目录/文件名。 How could that be done? 那怎么办?
I have posted on the MSDN forums here: http://goo.gl/5p0oz but have continued my search on stackoverflow due to a seemingly dead end thread. 我已经在MSDN论坛上发布了: http : //goo.gl/5p0oz,但是由于看似死角的线程,我继续在stackoverflow上进行搜索。 This post is a cumulative gathering of the information found through MSDN responses. 这篇文章是通过MSDN响应找到的信息的累积收集。
You can try using the Set-Content cmdlet
instead, like this. 您可以像这样尝试使用Set-Content cmdlet
。 You need to pass the $myFile as a string and Set-Content will do the rest for you - 您需要将$ myFile作为字符串传递,而Set-Content将为您完成其余工作-
Inside your script ie MyScript.ps1
here, you will have this piece of code - 在脚本(即MyScript.ps1
,您将拥有以下代码-
param([string]$parameter, [string]$myFile)
try
{
$spWeb = Get-SPWeb -Identity http://127.0.0.1
$listTemplate = [Microsoft.SharePoint.SPListTemplateType]::DocumentLibrary
$guid = $spWeb.Lists.Add($parameter, "", $listTemplate)
$guid | Set-Content $myFile
}
catch
{
throw ("Failed. The error was: '{0}'." -f $_ )
}
How to Run: Open Powershell Prompt and type this 如何运行:打开Powershell提示并键入
.\\MyScript.ps1 "someparam1" "D:\\Output.txt"
C# Bit - C#位-
private PowerShell _ps;
_ps = PowerShell.Create();
var parameter = new List<string>
{
parameter,
myFile
};
var sr = new StreamReader(@"C:\MyScript.ps1");
_ps.AddScript(sr.ReadToEnd()).AddParameters(parameter);
_ps.Invoke();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.