1
votes

I am running this script below and when i execute inside of powershell it runs and provides the exact link I am requesting. When I save in a ps1 file and call the file I get this error. Why would it run fine in powershell but not in the actual file?

My code:

$year = $date.year
$date = GET-DATE 
$datemonth=$date.month
$month = (Get-Culture).DateTimeFormat.GetMonthName($datemonth)
$url = 'https://health.mil/Reference-Center/Technical-Documents?query=DMIS'
$links=((Invoke-WebRequest –Uri $url).Links | Where-Object {($_.innerHTML - 
like "*DMIS ID Monthly*") -and ($_.innerHTML -notlike "*Change*") -and 
($_.innerHTML -like "*$month*")}).href
$links

The error:

At C:\Users\Documents\DMIS\web_scrape2.ps1:6 char:178 + ... HTML -notlike "Change") -and ($_.innerHTML -like "$month")}).href + ~~~~~~~~~ The string is missing the terminator: ". At C:\User\Documents\DMIS\web_scrape2.ps1:8 char:1 + Missing closing ')' in expression. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString

2
is this exactly what your ps1 file looks like? even the line breaksArcSet
@missy - your code shows "smart quotes" - the curved/curled quotes. PoSh has a known problem with those. replace them with the straight versions and things will likely work fine.Lee_Dailey
no there is no spaces in the code...stack overflow formatted it that way. Removed the " and replaced with single ' and different error.missy
paste new errorl. This runs fine for me in ps1 formArcSet
There still appears to be "smart" quotes in -like “*DMIS ID Monthly*”). Change these to a plain QUOTATION MARK.lit

2 Answers

2
votes

Based on the problem description and the original form or your question, it sounds like you have a character-encoding problem.

Your code contains non-ASCII-range Unicode quotation marks_ - (LEFT DOUBLE QUOTATION MARK, U+201c and RIGHT DOUBLE QUOTATION MARK, U+201d) and punctuation (EN DASH U+2013) - which PowerShell recognizes in principle as their ASCII counterparts, but only if it interprets the character encoding of the input file correctly.

Be sure to save your script as UTF-8 with BOM in order for Windows PowerShell to recognize these characters correctly.
(PowerShell Core would recognize them correctly if UTF-8-encoded even without a BOM).

0
votes

From the Unicode standard at https://www.unicode.org/charts/PDF/U0000.pdf

The code is using the "preferred characters in English for paired quotation marks."

0022 " QUOTATION MARK
• neutral (vertical), used as opening or closing
quotation mark
• preferred characters in English for paired
quotation marks are 201C “  & 201D ”

Update:

When I run this code, here are the results. Is this correct?

PS H:\src\tws> Get-Content .\t.ps1
$year = $date.year
$date = GET-DATE
$datemonth=$date.month
$month = (Get-Culture).DateTimeFormat.GetMonthName($datemonth)
$url = 'https://health.mil/Reference-Center/Technical-Documents?query=DMIS'

$links = ((Invoke-WebRequest –Uri $url).Links |
    Where-Object {
        ($_.innerHTML -like "*DMIS ID Monthly*") -and
        ($_.innerHTML -notlike "*Change*") -and
        ($_.innerHTML -like "*$month*")
    }).href

$links

PS H:\src\tws> .\t.ps1
/Reference-Center/Technical-Documents/2018/09/27/DMIS-ID-Monthly-October-2018