Steve
Steve

Reputation: 923

powershell cmdlet errors vary in script vs. console direst entry

Please understand this is a part of a much larger script. It is unreasonable for me to post the entire script to this question.

I have done a bunch of test and proven the root cause is the Copy-Item command ~ behaves differently run as a script vs. manually executing the cmdlet manually typing in the PS console.

#PS 5.1.14393.206 on Windows10 properly

$SiteName = "mysite"
$AppPoolTemplate = ".NET` v4.5"
$iisAppPoolTemplate = "IIS:\AppPools\$AppPoolTemplate"
$iisAppPool = "IIS:\AppPools\$SiteName"
$iisSites = "IIS:\Sites\$SiteName"

Write-Host "AppPoolTemplate //$AppPoolTemplate//"
Write-Host "iisAppPoolTemplate //$iisAppPoolTemplate//"
Write-Host "iisAppPool //$iisAppPool//"
Write-Host "iisSites //$iisSites//"

Stop-WebAppPool -Name $AppPoolTemplate

Copy-Item $iisAppPoolTemplate $iisAppPool                # fails
Copy-Item IIS:\AppPools\.NET` v4.5 IIS:\AppPools\mysite  # doesn't fail, but doesn't copy either

The first Copy-Item statement

Copy-Item $iisAppPoolTemplate $iisAppPool

causes the script to fail. Removing this line proves the problem starts with this line. The actual error line reported is whichever is the last line with properly "closed string literal". Erroneously reporting the literal is not closed. This error only occurs if I run the script. If I cut-and-paste the script content into PS console ... no error.

If run the second Copy-Item statement

Copy-Item IIS:\AppPools\.NET` v4.5 IIS:\AppPools\mysite

from a script it completes without error. But doesn't do what it is supposed to do. If I cut-and-paste the above command into a PS console .. no error AND does what it is supposed to do.

Here is the actual error. The error line #87 is the last line in the script with a string literal. Line #67 also has a string literal. If I remove line #87 the error becomes the same only now on line #67. The Copy-Item line sets up the problem which is only reported at the last closed string literal closest to the end of the script.

PS C:\scripts> .\_addSite.ps1
At C:\scripts\_addSite.ps1:87 char:51
+ $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
+                                                   ~~
The string is missing the terminator: ".
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString

Upvotes: 0

Views: 39

Answers (1)

Steve
Steve

Reputation: 923

After posting this question, I took the boiled down script I wrote for this question and found it wasn't creating the error. So I slowly cut-and-paste little sections from the broken script into this question's version. Then re-ran the question's script each time. And always without error.

What changed? I closed and re-opened the PS console between giving up on a broken script and starting on this question. And now neither the question's script nor the broken version have a problem.

Probable solution? There was something in the previous PS console environment that was the root cause and this was flushed out by closing the console. How lovely.

Thanks everyone for your remarks

Upvotes: 1

Related Questions