目前还不太清楚您到底要得到什么,但我将尝试猜测,请参阅代码中的注释。我认为,最初的代码有几个错误,其中一个是严重的:
Set-Content
在错误的管道/循环中使用。这是正确的代码。
function Insert-Usings
{
trap {
Write-Host ("ERROR: " + $_) -ForegroundColor Red
return $false
}
(Get-ChildItem $base_dir -Include *.asmx,*.ascx,*.cs,*.aspx -Force -Recurse -ErrorAction:SilentlyContinue) | % {
$fileName = $_.FullName
(Get-Content $fileName) | % {
if ($_ -cmatch "using Company\.Shared;") {
# just replace
$_ -creplace "using Company\.Shared;", "using Company.Common;"
}
elseif ($_ -cmatch "using Company") {
# write the original line
$_
# and add this after
"using Company.Services.Contracts;"
}
else{
# write the original line
$_
}
} |
Set-Content $fileName
}
}
例如,它取代了:
xxx
using Company.Shared;
using Company;
ttt
xxx
using Company.Common;
using Company;
using Company.Services.Contracts;
ttt
注意:假设您不应该将此代码多次应用于源代码,此代码不是为此而设计的。