[英]Simple PowerShell script refuses to write file when running from command line but works fine in ISE
So I have a pretty simple PowerShell script used to update a version number during a build. 因此,我有一个非常简单的PowerShell脚本,用于在构建过程中更新版本号。
$InputFile = 'D:\BuildAgent\work\c9716e2651305a2e\web\WEB-INF\classes\version.properties'
$OutputFile = $InputFile
Write-Host $('Updating "' + $OutputFile + '" to reflect version "2.10.0.51"')
(Get-Content $InputFile) |
Foreach-Object {
$_ -replace 'version.release\s*:.*','version.release: 2.10.0.51'
} |
Out-File $OutputFile
It works completely fine from within ISE, but when I run it from the command line with 它在ISE中可以正常工作,但是当我从命令行运行时
powershell.exe -Command - < powershell2447437064467034590.ps1
powershell.exe-命令-<powershell2447437064467034590.ps1
or 要么
powershell.exe -NoProfile -NonInteractive -ExecutionPolicy ByPass -Command - < powershell2447437064467034590.ps1
powershell.exe -NoProfile -NonInteractive -ExecutionPolicy Bypass-命令-<powershell2447437064467034590.ps1
All this does though is blank out the file. 尽管所有这些都将文件清空。 How can this work fine from ISE but not from the command line?
如何通过ISE而不是通过命令行正常工作?
"Windows Powershell does not load commands from the current location by default. If you trust this command, instead type ".\\test.ps1" “默认情况下,Windows Powershell不会从当前位置加载命令。如果您信任此命令,请键入“。\\ test.ps1”
-Windows Powershell -Windows Powershell
powershell.exe .\powershell2447437064467034590.ps1
I tested with Powershell command prompt and classic command prompt 我用Powershell命令提示符和经典命令提示符进行了测试
For more information 欲获得更多信息
Run from an elevated script or command, Powershell 3.0 从增强的脚本或命令Powershell 3.0运行
Update-Help
Get-help about_Command_Precedence
I figured it out. 我想到了。 I used tabs in the file to pretty print and format the text.
我使用了文件中的标签来漂亮地打印和格式化文本。 Apparently that breaks it.
显然,这打破了它。 If I copy and past it into ISE, it silently converts all the tabs into spaces which is why it worked there.
如果我将其复制并粘贴到ISE中,它将以静默方式将所有选项卡转换为空格,这就是它在那里起作用的原因。 Pretty surprising that the tokenizer doesn't treat all whitespace the same.
令人惊讶的是,令牌生成器没有将所有空格都相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.