gpt4 book ai didi

powershell - 术语 '-replace'无法识别为cmdlet,函数,脚本文件或可运行程序的名称

转载 作者:行者123 更新时间:2023-12-03 01:13:08 25 4
gpt4 key购买 nike

我正在编辑Powershell脚本,但突然开始出现奇怪的错误:
-replace : The term '-replace' is not recognized as the name of a cmdlet, function, script file, or operable program

-Get-TfsItemHistory : The term 'Get-TfsItemHistory' is not recognized as the name of a cmdlet, function, script file, or operable program
我会理解第二个错误,但是-replace?真奇怪。

Get-TfsItemHistory在引用的文件VersionsCache.ps1中使用

Script.ps1:

   #For all projects, that has been changed since last build updates [AssemblyVersion], [AssemblyFileVersion], and [AssemblyInformationalVersion],  attributes in all assemblyinfo.cs files.
$rootDir = "C:\Projects\UI\Evis\Dev"
$version = "2.0.0"
$buildType = "beta"
$currentChangeset = 1789
$versionsCacheFile ="C:\Projects\UI\Evis\versionsCache.txt"

. "$rootDir\VersionsCache.ps1"

$projects = Get-ChildItem "$rootDir\*.csproj" -Recurse;



function UpdateAssemblyInfo{
Param ([string]$dir, [string]$assemblyVersion, [string]$assemblyInformationalVersion)

$assemblyVersionPattern = 'AssemblyVersion\("[0-9]+(\.([0-9]+|\*)){1,3}"\)'
$assemblyFileVersionPattern = 'AssemblyFileVersion\("[0-9]+(\.([0-9]+|\*)){1,3}"\)'
$assemblyInformationalVersionPattern = 'AssemblyInformationalVersion\(.*\)'

$newVersion = 'AssemblyVersion("' + $assemblyVersion + '")';
$newFileVersion = 'AssemblyFileVersion("' + $assemblyVersion + '")';
$newInformationalVersion = 'AssemblyInformationalVersion("' + $assemblyInformationalVersion + '")';

$assemblyInfoFile = Get-ChildItem $dir -Filter assemblyinfo.cs -Recurse -Depth 1
$assemblyInfoContent = (Get-Content $assemblyInfoFile.FullName)
-replace $assemblyVersionPattern, $newVersion
-replace $assemblyFileVersionPattern, $newVersion


if ($assemblyInfoContent -match $assemblyInformationalVersionPattern)
{
$assemblyInfoContent = $assemblyInfoContent -replace $assemblyInformationalVersionPattern, $newInformationalVersion
}
else{
$assemblyInfoContent = $assemblyInfoContent + "`r`n[assembly: $newInformationalVersion]"
}


$assemblyInfoContent| Out-File $assemblyInfoFile.FullName
}


$publishedChangesetsCache = Load-Versions $versionsCacheFile

foreach ($project in $projects)
{
$projectDir = $project.Directory.FullName;

$assemblyInfo = Get-ChildItem $projectDir -Filter assemblyinfo.cs -Recurse -Depth 1 | select -First 1

Write-Host $projectDir -NoNewLine

$publishedChangesets = GetpublishedChangesets -dir $projectDir -publishedChangesetsCache $publishedChangesetsCache
$latestChangesetId = GetLatestChangeset $dir
Write-Host "changeset: $latestChangesetId"

if (IsChangedSinceLastBuild $latestChangesetId $publishedChangesets){
$publishedChangesets.Insert(0,$currentChangeset)
}
$changesetToPublish = $publishedChangesets[0]

$informationalVersion = "$version.$changesetToPublish"
if ($buildType -ne ""){
$informationalVersion = "$version-$buildType$changesetToPublish"
}
UpdateAssemblyInfo $projectDir -assemblyVersion "$version.$changesetToPublish" -assemblyInformationalVersion $informationalVersion
}



Save-Versions $publishedChangesetsCache -file versionsCacheFile

我想我有一些语法错误,因为是昨天,但是我不知道在哪里。

最佳答案

如果要将-replace语句放在新行中,则需要添加反引号:

$assemblyInfoContent = (Get-Content $assemblyInfoFile.FullName) `
-replace $assemblyVersionPattern, $newVersion `
-replace $assemblyFileVersionPattern, $newVersion

或者只写成一行...

关于powershell - 术语 '-replace'无法识别为cmdlet,函数,脚本文件或可运行程序的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35219891/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com