1

I referenced many threads on this and other forums, but not able to get this script to work in script form in ps or in bat or cmd file.

These commands work interactively within PowerShell

PowerShell Code:

#save creds
$creds=Get-Credential -Credential np\jcho

#create session
$session = New-CimSession –ComputerName "server-a.np.domainx.com", "server-b.np.domainx.com" -Credential $creds

#query the data needed.
Get-CimInstance -ClassName win32_operatingsystem -CimSession $session | select csname, lastbootuptime

Windows Batch/CMD file code:

powershell -noexit "& ""C:\Users\jcho\Downloads\remote_host_reboot_time.ps1"""
pause

Error Message:

C:\Users\jcho\Downloads>powershell -noexit "& ""C:\Users\jcho\Downloads\remote_host_reboot_time.ps1"""
At C:\Users\jcho\Downloads\remote_host_reboot_time.ps1:5 char:101
+ ... rver-a.np.domainx.com", "server-b.np.domainx.com" -Credential $creds
+                                                      ~~~~~~~~~~~~~~~~~~~~
The string is missing the terminator: ".
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString

OS and PS Versions:

Window 10
PS Version : 5.1.18362.1171

When I run those 3 PS commands interactively I get the following output, which is what I am expecting from scripted files

Good Output:

csname        lastbootuptime
------        --------------
server-a  12/13/2019 12:30:37 AM
server-b  12/12/2019 12:01:53 AM

I am surprise to see that the windows powershell has been made so convoluted to script/use.

2
  • Why not run the PS1 file via powershell -File C:\Users\jcho\Downloads\remote_host_reboot_time.ps1 I have a feeling the way you are executing it using double quotes and the & operator are causing your problems. Commented Dec 21, 2020 at 18:55
  • Nevermind, that's not causing your problem, I was able to re-created it on my machine. Commented Dec 21, 2020 at 19:02

1 Answer 1

1

It looks like the problem has to do with character encoding. This is always an annoying error to troubleshoot because things always "look" normal.

Here's how you can identify the problem for your particular script:

Open your script in notepad++ (or your favorite editor, but I'm using notepad++).

Then under the "Encoding" menu, select "ANSI". You'll see that in your case the hyphen on "ComputerName" is not a standard ASCII hyphen. This is likely because you copy-pasted your script from somewhere else.

For example, when I copy your script into a UTF-8 file in notepad++, then switch the encoding to ANSI, I see this:

New-CimSession –ComputerName

When I look up the hex code for –, which is E28093, I find it's this:

Unicode Character 'EN DASH' (U+2013) https://www.fileformat.info/info/unicode/char/2013/index.htm

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Chad - I knew something was wrong on those lines, but was not sure how do I do it, luckily I do use Notedpad++ so was able to fix it and its working
Nicely done. The dash and single/double quotes are a common cause from copy/paste
Long dashes, em dashes, and "smart" quotes are all common character corruption issues. It's often caused by Microsoft Word and other word processing software, but other kinds of overly clever programs can also be the source of these problems.
@BoogaRoo at least with smart quotes, you can _usually_visually identify them. The worst is cases like this where the "en dash" looks the same as a normal dash. As a database developer, this is the bane of my existence lol.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.