Powershell 批处理模板

common.ps1

cls; 
#Global is public
#Script is internal
#Script is Script
#Local is current stack level
#Numbered scopes are from 0..N where each step is up to stack level (and 0 is Local)


$Script:PSScriptRoot =Split-Path -Parent $MyInvocation.MyCommand.Definition
$Script:strDate = Get-Date -Format "yyyyMMdd"
#logging path and method
$Script:logfile = "$PSScriptRoot\log$($strDate).txt"
$Script:log = {$input|Tee-Object -FilePath $logfile -Append}
#commen
$Script:rem = {$Script:i = '';1..75|%{$i += '-'};$i } 
#start
&$rem|&$log
try{ 
<span style="white-space:pre">	</span>#your ps code
<span style="white-space:pre">	</span>$psversiontable|&$log 
<span style="white-space:pre">	</span>$Script:pss = ps
<span style="white-space:pre">	</span>$Script:i = 0
<span style="white-space:pre">	</span>foreach ($ps in $pss)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>Write-Progress -Activity "on processing..." -status "$ps...$($i.tostring() + '/' + $pss.count.tostring())" -percentcomplete ($i++/($pss.count)*100)
<span style="white-space:pre">		</span>sleep 1
<span style="white-space:pre">	</span>}
}
catch {
<span style="white-space:pre">	</span>"At $(Get-Date -Format “yy-MM-dd HH:mm:ss”) error occured:$_.Exception"|&$log
}finally{
<span style="white-space:pre">	</span>&$rem|&$log
<span style="white-space:pre">	</span>"Finish deploying at $(Get-Date -Format “yy-MM-dd HH:mm:ss”)"|&$log 
}
#notepad "$logfile"

start.bat

@echo off  
@cd %cd%
@set vers=2.0
:Entrance  
cls   
goto Start  
:Start  
title APOMS SQL Deployment Guide   
echo Please select an option from the list with the corresponding key  
echo ---------------------------------------------------------------- 
echo [1] Test  
echo [2] Exit 
echo ----------------------------------------------------------------  
CHOICE /c 12 /M "Choice->" 
if errorlevel 2 GOTO exit 
if errorlevel 1 GOTO GenA  
rem Generate Append
:GenA
cls  
echo starting powershell,Generate Append
powershell -file %cd%\common.ps1 -nologo -version %vers% -Mta
echo finished
rem timeout 2  
pause
goto :Entrance 

rem Generate Merge 
:GenM
cls   
echo starting powershell,Generate Merge 
rem powershell -file %cd%\module\GenerateMerge.ps1 -nologo -version %vers% -Mta
echo finished
pause
goto :Entrance

rem Depoly
:Depo 
cls  
echo Caching sqlcmd files to local.
powershell -file %cd%\Mail\Send-AvEmail.ps1 -nologo -version %vers% -Mta
echo finished
pause
goto :Entrance

rem clear
:clea
cls  
echo clearing CachedSQLS
rem powershell -file %cd%\module\ClearCachedSQLS.ps1 -nologo -version %vers% -Mta
echo finished
pause  
goto :Entrance


rem Set-ExecutionPolicy -ExecutionPolicy remotesigned
:setP
cls  
echo Set-ExecutionPolicy -ExecutionPolicy remotesigned
powershell -file %cd%\module\Set-ExecutionPolicy.ps1 -nologo -version %vers% -Mta
echo finished
pause  
goto :Entrance


rem Config 
:Conf 
cls   
echo configuring powershell inittial parameters:
echo edit and save to contiue.
rem echo editing  %cd%\module\config
rem notepad %cd%\module\config
echo editing  %cd%\module\config_sys
notepad "%cd%\module\config_sys"
echo testing config_sys
powershell -file %cd%\module\TestConfig.ps1 -nologo -version %vers% -Mta
pause
goto :Entrance

rem Help
:Help
cls  
@type %cd%\module\help.txt
pause  
goto :Entrance

rem SqlCMD
:SqlC
cls  
powershell -file %cd%\module\GenerateSqlcmdFiles-force.ps1 -nologo -version %vers% -Mta
pause  
goto :Entrance


rem default
:N  
cls  
echo Invalid Selection! Try again  
@type %cd%\module\help.txt
pause  
goto :Entrance 

rem exit
:Exit 
exit


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。