• 0.Object
    • object has..
      • Unique name
      • Properties
      • Methods
    • Like command tool
      • unique name
      • Let you do things
      • group together related functionality
      • Can be used from command prompt
    • Like output strings
      • Contain results
      • Can be parsed for particular bits of info
      • Can be used from command prompt
    • Type of object
      • COM obj
      • WMI obj
      • .NET framework obj
      • Windows powerShell obj
  • 1.Using Object
    • COM
      • $obj = new-object -comobject <PROGID>
      • $obj = new-object -com <PROGID>
        • PROGID look like "WScript.Network" and they stored in the registry HKEY_CLASSES_ROOT
      • Accessing Properties
        • $obj = new-object -com WScript.Network
        • $obj.Name
      • Setting Properties
        • $ExcelObj = new-object -com Excel.Application
        • $ExcelObj.Visible = $true
        • $WorkBookObj =  $ExcelObj.Workbooks.add()
        • $WorkSheetObj  = $WorkBookObj.Worksheets.Item(1)
        • $WorkSheetObj.Cells.Item(1.1) = "Feel the Power"
        • $ExcelObj.Quit
      • Calling Methods
        • $WorkBookObj =  $ExcelObj.Workbooks.add()
    • WMI
      • $obj = get-wmiobject -class <WMI Class>
      • $obj = get-wmiobject <WMI Class>
      • $obj = get-wmiobject -query <WQL>
      • Accessing Properties
        • $obj = get-wmiobject win32_process
        • $obj.WorkingSetSize
        • $obj | foreach-obkect {$_.ProcessName +":"+$_.WorkingSetSize}
      • Modifying Properties
        • $wmi_settings = get-wmiobject win32_wmisetting
        • $wmi_settings.ASPScriptDefaultNamespace = "root\default"
      • Calling methods
        • $obj = get-wmiobject win32_process -filter 'Name = "notepad.exe"'
        • $obj.GetOwner()
    • .NET framework
      • $obj = new-object -typeName <Fully-Qualified Name>
      • $obj = new-object <Fully-Qualified Name>
      • $obj = [ADSI]"LDAP://localhist:389/dc=NA,dc=fabrikam,dc=com"
        • $di = new-object System.IO.DiretoryInfo($PSHOME)
          • $PSHOME : home dir for PowerShell
      • Accessing Properties
        • $di = new-object System.IO.DiretoryInfo($PSHOME)
        • $di.CreationTime
      • Calling methods
      • UI
        • [System.Refleciotn.Assembly]::LoadWithPartialName(" System.windows.Forms")
        • $form = new-object System.Wondows.Forms.form
  • 2.Exploring Object
    • get-member
    • OLEView
    • Go to MSDN library
    • $objshell = get-object -com "Shell.Application" | get-member
    • $objFilder = $objshell.BrowseForFolder(0,"Pick Me!!",0)  //跳出瀏覽資料夾視窗
    • System.IO.DirectoryInfo  //namespace
    • $di = get-member -membertype Property
  • 3.Displaying Object
    • Many ways to "display" an object
    • Windows PowerShell provides a default way
    • XML config files let you change that default
    • 修改types.ps1xml檔以修改預設顯示欄位
      • $PSHOME\types.ps1xml
      • ... | format -list *
      • set-executionpolicy remotesigned
      • update-typedata
  • 4.Saving and Restoring Object
    • Export-Clixml saves a representation of the object to a file you can revive later
      • new-object -com "WScript.Network" | export-clixml "mothball.xml"
    • Import-Clixml restores the object from a file
      • $obj = import-clixml "mothball.xml"
  • 5.Copying and Modifying Object
    • Select-Object
      • Creates a new object by copying a portion of an exising one 
    • Add-Member
      • Augments an object by adding to an existing one
    • Add property using select-object
      • get-process | select-object ProcessName,@{Name="Start Day";Expression = {$_.startTime.DayofWeek}}
      • 上述物件型態:System.management.automation.PSCustomObject
    • Add property using add-mamber
      • $a = "thanks for watching windows powershell week"
      • $a = add-member -inputobject $a -membertype scriptmethod -name words -value {$this.splite()} -passthru
  • 6.Comparing Object
    • compare-object
      • $process = get-process
      • notepad
      • $processes_now = get-process
      • compare-object -ref $process -dif $process_now
      • $process_now | where-object {$_.Name -eq 'notepad'} | select-object Name,HasExited  //true

 

  • , $procs | get-member  //object[ ]

 

  • Reflection uses Get-Member
  • Simple,consistent,and use dot notation
創作者介紹

PowerShell@HungYuWu

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