[英]Passing variables to powershell from C# : Output file location variables
我最近開始使用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
返回一個SharePoint GUID。
題:
我只是希望傳遞將在其中寫入GUID的目錄/文件名。 那怎么辦?
我已經在MSDN論壇上發布了: http : //goo.gl/5p0oz,但是由於看似死角的線程,我繼續在stackoverflow上進行搜索。 這篇文章是通過MSDN響應找到的信息的累積收集。
您可以像這樣嘗試使用Set-Content cmdlet
。 您需要將$ myFile作為字符串傳遞,而Set-Content將為您完成其余工作-
在腳本(即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 $_ )
}
如何運行:打開Powershell提示並鍵入
.\\MyScript.ps1 "someparam1" "D:\\Output.txt"
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.