简体   繁体   English

从命令行运行时,简单的PowerShell脚本拒绝写入文件,但在ISE中可以正常工作

[英]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.

相关问题 Powershell:脚本不是从命令行运行,而是从ISE运行? - Powershell: Script not running from command line but runs from ISE? 我的 PowerShell 脚本仅在从 ISE 运行时有效 - My PowerShell script only works when running from ISE PowerShell 命令中的脚本错误,但在 ISE 中有效 - PowerShell Script Error in command but works in ISE powershell 脚本在 ISE 中工作正常,但在 powershell 命令提示符下失败? - powershell script working fine in ISE but fails at powershell command prompt? 从命令行运行PowerShell ISE? - Run PowerShell ISE from the command line? 从命令行参数到Powershell_ISE - Parameters into Powershell_ISE from Command Line 从.bat脚本运行Powershell命令不起作用,但是当我直接在命令行中键入它时它可以工作 - Running Powershell command from .bat script doesn't work, however it works when I type it directly in Command Line PowerShell脚本仅可从PowerShell控制台运行,而不能从ISE运行 - PowerShell Script only works from PowerShell Console and not from ISE PowerShell:从 Visual Studio Code 运行脚本有效,从 PS 命令行运行失败 - PowerShell: Running Script from Visual Studio Code Works, Running from PS Command Line Fails Powershell脚本可在ISE中使用,但不能在控制台中使用 - Powershell script works in ISE but not in console
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM