'Calling powershell function in batch script

Hoping someone can point me in the right direction. Have a working remote PC info scanning tool that collects computer name, serial number and model. Been trying to get the monitor info added for so time and found this Powershell script and have been trying to get intergraded without success.

Powershell function;

      $Monitors = Get-WmiObject WmiMonitorID -Namespace root\wmi
 $LogFile = ".\MonInfo.csv"
    
 function Decode {
     If ($args[0] -is [System.Array]) {
         [System.Text.Encoding]::ASCII.GetString($args[0])
     }
     Else {
         "Not Found"
     }
 }
    
    
 ForEach ($Monitor in $Monitors) {  
     $Manufacturer = Decode $Monitor.ManufacturerName -notmatch 0
     $Name = Decode $Monitor.UserFriendlyName -notmatch 0
     $Serial = Decode $Monitor.SerialNumberID -notmatch 0
    
     echo  $Manufacturer, $Name, $Serial" >> $LogFile
 }

Here's the network scan batch script I been using. (Basic info scan via ping and get info from remoter systems via WMI)

    @echo off
cls
color 5f


setlocal EnableDelayedExpansion

net session >nul 2>&1
if %errorlevel% neq 0 set errormsg=This program must be run as Administrator& goto ERRORDISP



set "ip="
for /f "tokens=2 delims=:" %%a in ('ipconfig ^| findstr /c:"IPv4 Address"') do set ip=%%a
if not defined ip (set errormsg=No IP address detected - check network cable& goto ERRORDISP)
set ip=%ip: =%
for /f "tokens=1,2,3,4 delims=." %%a in ("%ip%") do set oct1=%%a& set oct2=%%b& set oct3=%%c& set oct4=%%d
set subnet=%oct1%.%oct2%.%oct3%

set scan=0
set found=0
set foundsv=0
set ipstart=51
set ipend=170

:SCAN
set /a totalip=ipend-ipstart+1

set "_d=%date%"
set "_t=%time%"
set "log=scanLog%-d%-%_t%.csv"

echo IP,Name,Serial,Model > %log%

echo.
for /l %%a in (%ipstart%,1,%ipend%) do (
 set "ip=%subnet%.%%a"
 set /a scan=scan+1
 set /a pct=scan*100/totalip

 echo  Scanning !ip!...
 call :BAR !pct! 40 progbar 
 title Simple Scanner ^| !totalip!/!scan!/!found!/!foundsv! ^| [!progbar!] !pct!%%%

 ping -n 1 -w 200 !ip! | find "TTL" >nul
 if !errorlevel! equ 0 (
  set "output="
  set /a found=found+1

  call :GETWMI !ip! "bios get serialnumber" serial
  call :GETWMI !ip! "computersystem get model" model
  call :GETWMI !ip! "computersystem get name" name

    
    REM *** DO "SV WORKSTATION" THINGS HERE

    set "output=!ip!,!name!,!serial!,!model! !MonSN!"
   ) else (

    REM *** DO "NON-SV WORKSTATION" THINGS HERE

    set "output=!ip!,!name!,!serial!,!model! !MonSN!"
  )
  echo !output! >> %log%
 ) else ( 

    REM *** DO "WORKSTATION NOT DETECTED" THINGS HERE

 )
)
:END

cls
color 2f
echo.
echo  SCAN COMPLETE
echo  ______________________________________________________________________
echo.
echo           
echo        Range: %subnet%.%ipstart% - %ipend%
echo      Scanned: %scan%
echo  Found total: %found%
echo     Found SV: %foundsv%
echo  ______________________________________________________________________
echo.
echo  Opening log file %log%...
echo.

start /max %log%

echo 
echo  Press any key to exit...
pause>nul
exit

:GETWMI
set "_s="
set _r=%2
set _r=%_r:"=%
(for /f "tokens=2 delims==" %%b in ('wmic /failfast:on /node:%1 %_r% /value') do set _s=%%b) 2>nul
if not defined _s set "_s=ERROR"
set "%~3=%_s%"
goto :eof


:BAR
if not defined xbar call :initBAR %2
for /l %%b in (1,1,%2) do (
 set /a bars=%2*%1/100
 set /a spcs=%2-bars
 set "obar="
 for %%c in (!bars!) do set "obar=!obar!!xbar:~0,%%c!"
 for %%c in (!spcs!) do set "obar=!obar!!xspc:~0,%%c!"
 set %3=!obar!
)
goto :eof

:initBAR
set "xbar=" & for /l %%b in (1,1,%1) do set "xbar=!xbar!l"
set "xspc=" & for /l %%b in (1,1,%1) do set "xspc=!xspc! "
goto :eof

:ERRORDISP
cls
color cf
echo.
echo  ^>^>^> ERROR ^<^<^<
echo.
echo  %errormsg%
echo 
echo  Press any key to exit...
pause>nul
exit

I have also tried calling for the .ps1 yet the variables are always empty and the corp network requires Powershell scripts to have sign cert to run by them selves.



Solution 1:[1]

Long time ago I wrote the script to get monitor serial number from registry. It takes only first monitor value. But you can change this script or convert to Powershell. Anyway you can see script logic: get EDID-number, then parse it.

@for /f %%i in ('@wmic path win32_desktopmonitor get pnpdeviceid ^|@find "DISPLAY"') do @set val="HKLM\SYSTEM\CurrentControlSet\Enum\%%i\Device Parameters"


@reg query %val% /v EDID>NUL
@if %errorlevel% GTR 0 @echo BAD EDID&EXIT
@for /f "skip=2 tokens=1,2,3*" %%a in ('@reg query %val% /v EDID') do @set edid=%%c

@set /A Y=%edid:~34,1%*16+%edid:~35,1%+1990
@echo.Manufactured: %Y%

@set id=%edid:000000FC00=#%
@for /f "tokens=1,2* delims=#" %%a in ("%id%") do @set id=%%b
@set id=%id:~0,22%

@setlocal enableextensions enabledelayedexpansion
@echo off
@for /L %%i in (0,2,20) do (
@set p=!id:~%%i,2!
@if !p!==0A @goto nxt
@set m=!m!0x!p!
)
@echo on
:nxt
@forfiles /p %windir%\system32 /m shell32.dll /c "cmd /c @echo.Model       : !m!"
@endlocal

@set edid=%edid:000000FF00=#%
@for /f "tokens=1,2* delims=#" %%a in ("%edid%") do @set id=%%b
@set id=%id:~0,20%

@setlocal enableextensions enabledelayedexpansion
@for /L %%i in (0,2,18) do @set sn=!sn!0x!id:~%%i,2!
@forfiles /p %windir%\system32 /m shell32.dll /c "cmd /c @echo.S.N.        : !sn!"
@endlocal

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Daemon-5