2010年9月21日 星期二

Powershell - some tip

  1. Let your computer can run powershell script : Set-ExecutionPolicy RemoteSigned
  2. 將使用者需要互動輸入的選項,自動帶入指令中 :
    因在build software時,會進入cygwin的環境中,並需輸入選項去build, 為將整個動作自動化
    1. 單一次選項時 :  sunny || .\login.bat ==> 執行login.bat時,會等待user輸入其名字再做login, 此時利用pipeline, powershell 會自動將 'sunny' input 
    2. 需多次輸入時 : 
      ps >@' [enter]
      >> input1 [enter]
      >> input2 [enter]
      >> '@  | command
    3. refer to : http://powershell.com/cs/blogs/tips/archive/2009/07/15/feeding-input-into-native-commands.aspx
  3. Execute powershell script (.ps1 file) as a command (可應用於task scheduler中) :
    powershell.exe -command "&{d:\ps\sample.ps1 arg1 arg2}"
  4. Print all System variable
    Get-ChildItem -Path Variable: `
           | Add-Member `
                  -Name Type `
                  -MemberType ScriptProperty `
                  -Value { $this.GetType().FullName } `
                  -PassThru `
           | Add-Member `
                  -Name DisplayValue `
                  -MemberType ScriptProperty `
                  -Value { (Get-Variable -Name $this.Name -Scope 1 -ValueOnly | Format-List -Property * | Out-String).Trim("`r`n") } `
                  -PassThru `
           | Sort-Object -Property Name `
           | Format-List -Property Name,Type,Description,Value,DisplayValue,Options `
           | Out-Host -Paging

沒有留言: