1
votes

So I have a pretty simple PowerShell script used to update a version number during a build.

$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

powershell.exe -Command - < powershell2447437064467034590.ps1

or

powershell.exe -NoProfile -NonInteractive -ExecutionPolicy ByPass -Command - < powershell2447437064467034590.ps1

All this does though is blank out the file. How can this work fine from ISE but not from the command line?

2
Are you running it under the same user account? Why run the script with -Command - < powershell2447437064467034590.ps1 when you could do it with -File powershell2447437064467034590.ps1? It could be (but I don't think it is) a threading issue since the ISE is MTA (Multi-Threaded Apartment) and the console is STA (Single-Threaded Apartment) by default. you could try adding the -MTA switch.TheMadTechnician
Well -Command - < powershell2447437064467034590.ps1 is how the TeamCity build agent initiates the script. Don't have a whole lot of choice in that matter.Ultratrunks
I figured it out. I'm so furious with M$ right now. I used tabs in the file to pretty print and format the text. Apparently that breaks it. But if you copy and past it into ISE it silently converts all your tabs to spaces which is why it worked there. Unbelievable that the tokenizer doesn't eat up all whitespace. Such a trash scripting language. So glad I hardly have to work with it. 5 hours wasted.Ultratrunks
So you are claiming tab characters in your PowerShell source code prevented it from executing. Really? I guess I now have to remove the tabs in hundreds of my PowerShell scripts to make them work.Peter Hahndorf
Correct. Specifically in the For-Object loop, I had a tab at the begging of the line. I took it out. Works fine. Put it back in, it stops looping through the file.Ultratrunks

2 Answers

1
votes

"Windows Powershell does not load commands from the current location by default. If you trust this command, instead type ".\test.ps1"

-Windows Powershell

powershell.exe .\powershell2447437064467034590.ps1

I tested with Powershell command prompt and classic command prompt

For more information

Run from an elevated script or command, Powershell 3.0

Update-Help
Get-help about_Command_Precedence
1
votes

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. Pretty surprising that the tokenizer doesn't treat all whitespace the same.