vendredi 22 avril 2011

Inclure du code PS dans un fichier Batch

salut,


;@echo off & Setlocal EnableExtensions
;echo CMD %ERRORLEVEL%
;:<>
;Findstr -rbv ; %0 | powershell -c - 
;goto:sCode ######################################
 
"PSH " | Write-Host -for yellow -no
$_=$OFS;$OFS=''
$char=87,65,76,73,68,50,77,73,64,71,77,65,73,76,46,67,79,77
([string][char[]]($char));$OFS=$_
exit 5
 
;:sCode ######################################
;:<>
;echo CMD %ERRORLEVEL%
;pause & goto :eof




voici une autre approche plus robuste:


@echo off
::::::::::::::::::::::::::::::::::::
For /f "delims=:" %%a In ('
     findstr /Bn "@PS" %0
 ') do Set /ALine=%%a
 more +%Line% %0  | powershell -c -
::::::::::::::::::::::::::::::::::::
:: ici suite du code Batch

dir
pause & exit /b 

@PS ::::::::::::::::::::::::::::::
Function Get-Test {
  param()
  Begin{
    Write-Warning "Debut du traitement" 
  }
  Process{
    write-verbose "ici n'importe quel traitement"
    gps power*,cmd*  
    <# 
      un Bloc de commentaires 
    #>
  }
  End{
   Write-warning "Fin du traitement"
  }
}
Get-Test
exit


1 commentaire:

Daten|teiler a dit…

Hello Walid,

thank you very much for this posting. It is very helpful for me and I think I will use it. ;-)

But there is one mistake in your code: Between "Set /A" and "Line=%%a" is a blank missing. Maybe you can fix this?

Thanks again and best wishes.

Christian