在PowerShell脚本访问被拒绝中保存文件

时间:2015-06-18 15:59:05

标签: powershell tfs tfsbuild powershell-v3.0

我有一个PowerShell脚本,旨在将Web配置转换修改为构建定义中的预构建事件。我已经让它在大多数情况下工作,但是当它保存更新的文件时,我将被拒绝访问。

有没有办法在没有打开窗口的情况下提供正确的访问权限,因为这是通过TFS构建代理完成的?

这是脚本:

param(
[string]$buildTarget="Dev",
[string]$projectName="SalesTools"
)

$VerbosePreference = "continue"

Write-Verbose "Params: buildTarget = '$($buildTarget)', projectName = '$($projectName)'"

# Make sure path to source code directory is available
if (-not $Env:TF_BUILD_SOURCESDIRECTORY)
{
    Write-Error ("TF_BUILD_SOURCESDIRECTORY environment variable is missing.")
    exit 1
}
elseif (-not (Test-Path $Env:TF_BUILD_SOURCESDIRECTORY))
{
    Write-Error "TF_BUILD_SOURCESDIRECTORY does not exist: $Env:TF_BUILD_SOURCESDIRECTORY"
    exit 1
}
Write-Verbose "TF_BUILD_SOURCESDIRECTORY: $Env:TF_BUILD_SOURCESDIRECTORY"

$webConfig = "$($Env:TF_BUILD_SOURCESDIRECTORY)\$($buildTarget)\SalesTools.Web\$($projectName)\web.$($buildTarget).config"
#$webConfig = "$($Env:TF_BUILD_SOURCESDIRECTORY)\$($buildTarget)\SalesTools.Web\ARCTools\web.$($buildTarget).config"

Write-Verbose "File Path: $($webConfig)"

$doc = (gc $webConfig) -as [xml]

$versionNumber = $doc.SelectSingleNode('//appSettings/add[@key="versionNumber"]/@value').'#text'

Write-Verbose "Current Version Number: $($versionNumber)"

if (($versionNumber)) 
{
    $versionInfo = $versionNumber.Split(".")

    $versionIteration = $versionInfo[1]
    $minorVersion = $versionInfo[2] -as [int]

    $minorVersion = $minorVersion + 1

    $currentIteration = Get-Iteration

    $newVersionInfo = ("v: 1.$($currentIteration).$($minorVersion)")
}
else
{
    Write-Error "Could not get version info from config."
    exit 1
}

$doc.SelectSingleNode('//appSettings/add[@key="versionNumber"]/@value').'#text' = $newVersionInfo

$doc.Save($webConfig)

1 个答案:

答案 0 :(得分:0)

在您阅读之前更新web.config,尝试更改"只读" web.config文件的属性。因为默认情况下,所有源文件都是"只读"。

在" $ doc = ....":

之前添加此行
  

attrib -R $ webConfig / S