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


Trier selon les Types d'objets

cette petite fonction, à pour rôle de trier selon 3 types d'objets: [String] [Version] et [int]


Function Sort-ObjectType {
<#
 .Synopsis
   Sort Object by Types
 .Description
   Sort Object by Types
 .Parameter Type
   Specifies the type of object
     - ToString:   [System.String]
     - ToInteger:  [System.Int32]
     - ToVersion:  [System.Version]
     - ToIPAddress [System.Net.IPAddress]
 .Parameter CaseSensistive
 .Parameter Descending
 .Parameter Unique    
 .Example
  [1]PS> $str += "10","2","5"
  [2]PS> $str | Sort-Object # 10 2 5
  [3]PS> $str | Sort-ObjectType -Type ToInteger # 2 5 10
 .Example
  [1]PS> '1.0.1.10','1.0.1.100','1.0.1.9' | Sort-Object -unique
  [2]PS> # 1.0.1.10 > 1.0.1.100 > 1.0.1.9
  [3]PS> '1.0.1.10','1.0.1.100','1.0.1.9' | Sort-ObjectType -Type ToVersion
  [4]PS> # 1.0.1.9 > 1.0.1.10 > 1.0.1.100
 .Example
  [1]PS> $ips="3.25.20.100","198.10.3.1","1.2.0.3","10.2.0.6"
  [2]PS> $ips | Sort-ObjectType -Type ToIPAddress 
 .Outputs
  System.String
#>
#Requires -version 2.0
 Param(
  [ValidateSet('ToString','ToInteger','ToVersion','ToIPAddress')]
  $Type,
  [Switch]$CaseSensitive,
  [SWitch]$Descending,
  [Switch]$Unique
  )
 $input | Sort-Object -Property {
    $ty=$_
    Switch($Type) {
     'ToString'  {[String]$ty}
     'ToInteger' {[Int]$ty}
     'ToVersion' {[Version]$ty}
     'ToIPAddress' { 
         [byte[]]$ty.split('.')|ForEach{[string]::Format("{0:000}",$_)}
         }
     default {$ty}
    }
  } -case:$CaseSensitive -desc:$Descending -uni:$Unique
}

jeudi 14 avril 2011

colorer vos invites de commandes

salut,

malheureusement, dans le shell windows, on n'a pas de commandes native qui permet de colorer nos chaines de caractères, pour pallier à cette lacune on peux télécharger des commandes externes, ou bien pour les puristes on peux bricoler un tuc comme ceci:


@echo off
if "%~2"=="" (
   cls
   echo.
   echo   .SYNTAXE:
   echo. 
   echo        %~n0 BackgroundColor+ForegroundColor chaine
   echo.
   echo  ------------ EXEMPLE 1 --------------
   echo.  
   echo        CMD E:\^> %~n0 0c "hello world"
   echo.
   echo  ------------ EXEMPLE 2 --------------
   echo.
   echo        CMD E:\^> type script.cmd
   echo        @echo off
   echo        call cecho ce "Debut"
   echo        echo processus
   echo        call cecho ec "Fin"
   exit /b 3
)
mkdir $temp || (exit /b 2)
pushd $temp || (rmdir $temp & exit /b 1)
for /f "delims=" %%. in ('
    "%ComsPec% /k prompt $h$h <&1"
 ') do >rtn echo %%.
Call:bfc %1 %2
popd
rmdir /s /q $temp
exit /b 0

:bfc
>%2 (set/P=+) <&1
findstr /a:%1 + %2 con
type rtn
goto :eof

supprimer les dossiers vides

salut,

voici un petit filtre en PS pour supprimer les dossiers vides


Filter Remove-EmptyFolder
{
  try{
   [IO.DirectoryInfo]$Folder=$_
   if($Folder.GetFiles().count -eq 0) {$Folder.Delete()}
  }
  catch{}
}

PS> gci -Recurse | Remove-EmptyFolder