close





  • What is Cmdlet?




    • a command-ine tool




      • PS C:\> get-process


    • Focus on a particular tast




      • PS C:\> Get-Command -Noun service




        • Get-Service,New-Service,Restart-Service.......


    • Consistent




      • Verb-Noun


      • Cmdlet -param1 <value> -param2 <value>


      • Get-help cmdletname
        Get-Service | Sort-object Name
        Get-Service | Group-object Name


    • Object => Cmdlet => Object    //以物件輸入及輸出




      • No more prayer-based parsing


      • Objects aren't static


      • Objects have methods and calling those to do stuff


    • Retrieve object of interest




      • get-eventlog system


    • Flow cintrol anf filtering




      • where-object {$_.... }


    • Infrastructure




      • get-alias


  • Useful info




    • Available command?




      • List all command




        • PS C:\> get-command


      • How many?




        • PS C:\> get-command | measure-object
          Count : 129
          Average :
          Sum :
          Maximum :
          Minimum :
          Property :


    • What parameters does a cmdlet take?




      • different input  => take different action




        • get-eventlog -logname system


        • get-eventlog -list


        • (get-command get-eventlog).definition




          • Get-EventLog [-LogName] <String> [-Newest <Int32>] [-Verbose] [-Debug] [-ErrorA
            ction <ActionPreference>] [-ErrorVariable <String>] [-OutVariable <String>] [-O
            utBuffer <Int32>]


          • Get-EventLog [-List] [-AsString] [-Verbose] [-Debug] [-ErrorAction <ActionPrefe
            rence>] [-ErrorVariable <String>] [-OutVariable <String>] [-OutBuffer <Int32>]


    • What object does a Cmdlet output?




      • get-eventlog -logname system | get-member
        TypeName: System.Diagnostics.EventLogEntry


    • Categories of cmdlets




      • Retrieve object


      • Take action


      • Filter and control flow


      • Output and format


      • Work with windows PowerShell providers


      • Windows Powershell(including reflection)


  • Cmdlet walkthrough!




    • Retrieve object




      • set of thing | what you want to do...




        • get-process => pipeline => next cmdlet


        • get-process, get-eventlog, get-service


      • Get-WMIObject




        • $BIOS = Get-WMIObject win32_bios
          $BIOS | get-member


        • Get-WMIObject win32_bios | format-list *             // list all property


        • Get-WMIObject win32_bios | format-list [a-z]*      //regular expression


        • Get-WMIObject win32_bios -computername JOSEPHPC  //Remote computer


        • (get-command Get-WMIObject).definition


        • Get-WMIObject -list | where-object {$_.Name -match "^Win32.*"}  //browsing the repository for win_32 classes


      • New-Object




        • $object = new-object -comobject "shell.Applicaiton"
          $object | get-member   //see the property , method ...


        • $object.cascadewindows()    //改變多視窗顯示模式


        • 使用.net




          • $DI = new-object System.IO.DirectoryInfo($PSHOME)   
            $DI.CreationTime




            • 2006年11月1日 下午 07:39:29


      • Get-Eventlog




        • get-eventlog -logname application -newest 5   //取最新的5筆log


    • Take action




      • get-service alerter, start-service alerter, stop-service alerter....


    • Filter and control flow




      • do this to provide concsistency


      • some of this functionality is provided intrinsically by PowerShell




        • Where-Object      //like SQL 過濾條件




          • get-service | measure-object
            //script block,pipe obj one by one


          • get-service | where-object {$true} | measure-object


          • $_ : like object variable; each value object




            • get-process | where-object {$_.workingset -ge 10000000}


          • Comparision Operators


          • get-process | where-object {$_.ProcessName -match 'p.*shell' }


        • ForEach-Object




          • 30000,56798,12432 | forEach-Object -process {$_/1024}  //每個數字除以1024




            • 29.296875
              55.466796875
              12.140625


        • Select-Object   //選擇欄位




          • get-wmiobject win32_processor | select-Object  Maxclockspeed


        • Tee-Object




          • Tee as in T


          • Y-Object might have been a better name


          • grabs a snapshot of objects working their way down the pipeline


          • stores snapshot in file or varialbe


          • $proc = get-process notepad  //儲存回傳物件於變數
            $proc                                      //顯示物件


          • get-process notepad | tee-object -variable proc   //儲存回傳物件於變數並且顯示於螢幕上


          • get-process notepad > noteout.txt   //儲存回傳物件於檔案


          • get-process notepad | tee-object -file noteout.txt //儲存回傳物件於檔案並且顯示於螢幕上


    • Output and format




      • Take the work out of creating basic spreadsheets and HTML pages


      • Override default formattting




        • group-object




          • get-eventlog application | group-object eventid  //欄位為count, eventid,group


          • get-process | group-object company    


        • sort-object




          • get-eventlog application | group-object eventid -descending | sort-object count


        • convertion-html




          • get-service | convertion-html | out-file "service.htm"
            invoke-item service.htm


        • export-csv




          • get-service | export-csv -notypeinformation service.csv
            invoke-item service.csv


    • Work with windows PowerShell providers




      • set-location HKCU 
        get-childitem


      • set-location "control panel"
        get-childitem


    • Windows Powershell(including reflection)




      • mamage aspects of PowerShell Environment


      • include reflection


      • Get-History //查看本session曾打過的指令


      • Get-Alias


      • four windows powershell cowfolk




        • get-command


        • get-help -full <cmd-name>


        • get-psdrive


        • get-psprovider


 





Cmdlets are not created equal

arrow
arrow
    全站熱搜

    ayowu 發表在 痞客邦 留言(0) 人氣()