Convert C# logic to powershell for TFS

旧街凉风 提交于 2019-12-21 19:55:56

问题


I have a C# program which build me a TFS build definition. I want to do the same code in a powershell script. So far, I have been able code the script which will create me a new build definition in TFS. However, I have trouble setting Process section of the build definition. I need to convert the below code in C# to powershell and all attemps I have made did not work.

//Set process parameters 
var process = WorkflowHelpers.DeserializeProcessParameters(buildDefinition.ProcessParameters);
//Set BuildSettings properties 
BuildSettings settings = new BuildSettings();
settings.ProjectsToBuild = new StringList("$/Templates/Main/Service/application1"); 
settings.PlatformConfigurations = new PlatformConfigurationList(); 
settings.PlatformConfigurations.Add(new PlatformConfiguration("Any CPU", "Debug"));
process.Add("BuildSettings", settings);

buildDefinition.ProcessParameters = WorkflowHelpers.SerializeProcessParameters(process); 

First I loaded the assemblies I need to work with TFS. When I want to replicate the same C# code as,

var process = WorkflowHelpers.DeserializeProcessParameters(buildDefinition.ProcessParameters); 

I did following in PowerShell

$process = New-Object Microsoft.TeamFoundation.Build.Workflow.WorkflowHelpers. 

Above gave me an error saying "Constructor not found. Cannot find an appropriate constructor for type Microsoft.TeamFoundation.Build.Workflow.WorkflowHelpers"

I checked and there are no constructors for that. My question is what I am I doing wrong in writing the PowerShell script to achieve the same functionality as c# code. I am sure it's syntax error that I am doing and not aware of the correct way of doing it in PowerShell.


回答1:


It would appear from your code snippet (and confirmed via MSDN) that the DeserializeProcessParameters is a static method on the WorkflowHelpers class. You would need to invoke it with the following syntax in PowerShell:

$process = [Microsoft.TeamFoundation.Build.Workflow.WorkflowHelpers]::DeserializeProcessParameters($buildDefinition.ProcessParameters)

It looks like the buildDefinition variable is declared earlier - so I just stuck a $ character on it to make it a legit PowerShell variable. Same thing with the process variable. I hope this helps!



来源:https://stackoverflow.com/questions/18389222/convert-c-sharp-logic-to-powershell-for-tfs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!