2013-05-02 5 views
0

면책 조항 : 적절한 시간 내에이를 수행하려면 ps에 대해 충분히 알지 못합니다. 그렇기 때문에 다른 사람에게 요청하고 있습니다. 내 더러운 일을해라.Powershell Script : (마스크를 통해) 파일을 프롬프트하고 명령 줄에서 해당 파일을 사용하십시오.

명령 줄을 열지 않고도 web.config 변환을 실행할 수 있기를 원합니다.

나는 폴더에있는 파일을 다음 있습니다 :

여기
web.config - actual web config 
web.qa.config - web config transformation for qa env 
web.production.config - web config transformation for production env 
transform.ps1 - powershell script I want to use to run transformation 

내가 원하는 무엇인가 : PS 파일 .*\.(?<env>.*?)\.config을 사용하여 현재 디렉토리를 열거하고 제가에 관심이에 대한 Web.config의를 생성하는 <env> 선택하게된다. 필자는 "qa", "production"의 두 가지 옵션을 제시 할 것이다.

  1. :

    I (사용자가) 환경을 선택하면 스크립트는 다음을 수행하여야한다 (의 그것 $ 변환로 저장됩니다 선택 환경이 ENV $ 및 해당 파일 이름으로 저장 "QA"라고하자)

을 : 백업 원래 web.config

  • 는 다음 명령을 실행 web.config.bak있다.

    echo applying $transformation... 
    [ctt][1].exe source:web.config transformation:$transformation destination:web.config preservewhitespaces verbose 
    echo done. 
    

    ctt .EXE 커맨드 라인에서의 Web.config 변환을 실행 XDT에 기초한 도구이다.

  • +0

    은, 당신을 위해 스크립트 작업을했다? (나는 아무것도 암시하지 않는다 ...);) –

    답변

    2

    좋아, 아주 간단 해 보이네, 내가 너를 위해 더러운 일을 할거야. ;)

    저장 transform.ps1으로 다음

    그래서
    $environments = @()f 
        gci | %{if ($_ -match '.*\.(?<env>.*?)\.config') {$environments += $matches.env}} 
        Write-Host "`nEnvironments:" 
        for ($i = 0; $i -lt $environments.Length; $i++) {Write-Host "[$($i + 1)] $($environments[$i])"} 
        Write-Host 
        do { 
         $selection = [int](Read-Host "Please select an environment") 
         if ($selection -gt 0 -and $selection -le $environments.Length) { 
          $continue = $true 
         } else { 
          Write-Host "Invalid selection. Please enter the number of the environment you would like to select from the list." 
         } 
        } until ($continue) 
        $transformation = "web.$($environments[$selection - 1]).config" 
        if (Test-Path .\web.config) { 
         cpi .\web.config .\web.config.bak 
        } else { 
         Write-Warning "web.config does not exist. No backup will be created." 
        } 
        if ($?) { 
         Write-Host "`nApplying $transformation..." 
         [ctt][1].exe source:web.config transformation:$transformation destination:web.config preservewhitespaces verbose 
         Write-Host "Done.`n" 
        } else { 
         Write-Error "Failed to create a backup of web.config. Transformation aborted." 
        }