Code: Select all
<# :
@echo off
echo start ps
:: $cmdargs variable will contain the command line.
powershell $cmdargs="""%*""";$exp=$(Get-Content '%~f0') -replace """`n""",""";""" ;invoke-expression """$exp"""
exit /b 0
#>
Write-Host "Hello from PowerShell";
Write-Host "Hello from PowerShell"
(end of update.)
Does anybody else have tried something in this direction?
The main impediment are the extension restrictions for both - cmd.exe and powershell.
I haven't find a way to drive the powershell to run a file with extension different from .ps1 .psc1 .psd1 .psm1 . One way is to use a temporary file and additional calling:
Code: Select all
@echo off
rem "installing" a caller.A files with .psc1 extension will be able to execute batch code but is not perfect as the %0 argument is lost
if not exist "c:\caller.bat" (
echo @echo off
echo copy "%%~nx1" "%%temp%%\%%~nx1.bat" /Y ^>nul
echo "%%temp%%\%%~nx1.bat" %%*
) > c:\caller.bat
:: to restore use assoc .psc1=Microsoft.PowerShellConsole.1
assoc .psc1=batps
ftype batps=c:\caller "%1" %*
and a test .psc1 file:
Code: Select all
set dummy="
goto :pscomment
"
<#
:pscomment
echo echo from bat
PowerShell -NoProfile -ExecutionPolicy Bypass -file "%~dpfnx1"
exit /b 0
#>
echo "echo from powershell"
Probably it can be done with temporary powershell caller . Any idea how powershell manages its extensions? And ideas for a better way to hybrid them?