PowerShell 语法错误 [英] PowerShell Syntax Error

查看:39
本文介绍了PowerShell 语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个将创建 TFS 构建定义的 powershell 脚本.我使用下面的例子作为我的起点.

I am working on a powershell script that will create TFS build definitions. I have used below example as my starting point.

http://geekswithblogs.net/jakob/archive/2010/04/26/creating-a-build-definition-using-the-tfs-2010-api.aspx

我在 powershell 中完成了脚本,它在 TFS 中为我​​创建了一个构建定义文件.我陷入困境的一件事是创建流程信息,例如要构建的项目"和要构建的项目".下面给出了 C# 代码

I have the script done in powershell and it creates me a build definition file in TFS. One thing I am stuck in is creating Process information such as "Item to build" and "Projects to build". The C# code for this is given below

//Set process parameters 
varprocess = WorkflowHelpers.DeserializeProcessParameters(buildDefinition.ProcessParameters); 

//Set BuildSettings properties 
BuildSettings settings = newBuildSettings(); 
settings.ProjectsToBuild = newStringList("$/pathToProject/project.sln"); 
settings.PlatformConfigurations = newPlatformConfigurationList(); 
settings.PlatformConfigurations.Add(newPlatformConfiguration("Any CPU", "Debug")); 
process.Add("BuildSettings", settings); 

buildDefinition.ProcessParameters = WorkflowHelpers.SerializeProcessParameters(process);

以下是我为实现上述目的而编写的 powershell 代码.

Below is the powershell code I have written to achive above.

Write-Host"Set process parameters "$now
$process=[Microsoft.TeamFoundation.Build.Workflow.WorkflowHelpers]::DeserializeProcessParameters($def.ProcessParameters)
Write-Host"Set build settings properties "$now
$settings=new-object-`enter code here`TypeNameMicrosoft.TeamFoundation.Build.Workflow.Activities.BuildSettings
$sList=New-Object-TypeNameMicrosoft.TeamFoundation.Build.Workflow.Activities.StringList
$sList="$/pathToProject/project.sln"
$settings.ProjectsToBuild =$sList
$process.Add("BuildSettings", $sList)

但是上面的代码段并没有在我的构建定义文件中为我创建构建设置.我的问题是我在 powershell 中以正确的方式执行此操作吗?我觉得我没有错误地编写 powershell 代码,因为我是 powershell 的新手.任何指导和帮助将不胜感激

But the above segment of code does not create me the Build settings in my build definition file. Myquestion is am I doing this the correct way in powershell? I feel I am not writing the powershell code incorrectly as I am newbie to powershell. Any guidance and help would be appreciated

推荐答案

在 PowerShell 中调用带参数的构造函数应该像这样:

Calling a constructor with parameters should be done like this in PowerShell:

$ns = 'Microsoft.TeamFoundation.Build.Workflow.Activities'
$settings.ProjectsToBuild = new-object "$ns.StringList" '$/pathToProject/project.sln'

还要注意在 TF 服务器路径周围使用单引号.$ 是 PowerShell 中的特殊字符 - 告诉它后面是变量名还是子表达式,即使是在字符串中.除非该字符串是单引号.在这种情况下,PowerShell 不会解释字符串中的任何特殊字符.

Also note the use of single quotes around the TF server path. $ is s special character in PowerShell - tells it what follows is either a variable name or sub-expression even in a string. Unless that string is single quoted. In which case, PowerShell doesn't interpret any special characters within the string.

这篇关于PowerShell 语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆