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.
powershell -File C:\Users\jcho\Downloads\remote_host_reboot_time.ps1I have a feeling the way you are executing it using double quotes and the&operator are causing your problems.