目前分類:PowerShell (163)

瀏覽方式: 標題列表 簡短摘要


執行指令
PS C:\> $a=(Get-WmiObject -Class Win32_OperatingSystem).LastBootUpTime
PS C:\> $b = New-TimeSpan $(Get-Date –month $a.substring(4,2) -day $a.substring(6,2) -year $a.substring(0,4) -hour $a.substring(8,2) -minute $a.substring(10,2) ) $(Get-Date)
PS C:\> $b

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


指令如下
Get-WmiObject -Class Win32_USBHub | where {$_.description -like
"*Mass Storage*"} | format-table DeviceID, Description

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


目前以PowerShell為tag的中文文章
幾乎都是我的文章^^""


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

  • get-childitem -Recurse -force -filter *.txt  <==>  dir /s *.txt 
    • -Recurse 遞迴
    • -force     列出隱藏檔
  • get-content a.txt  <==>  type a.txt
  • remove-item -Recurse *.txt  <==>  del /s *.txt
  • Write-Output "a is $a"  <==>  echo a is %a%

 

資料參考:Windows PowerShell In Action By Bruce Payette


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

資料夾有一堆檔案感覺很亂.....

若想要依據檔案的最後修改日期分類,例如依據年月
我們可使用以下的Script

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

  • DesktopEngineer.com提供三天的課程(1495美元)
    • Day 1 - Getting to Know PowerShell
      • What is PowerShell?
      • What are the Key New Items for Administrators?
      • Why You Should Care About the Key Differences
      • In what contexts can it run?
      • What new things are now in reach that may not have been before?
      • Smallest "Hello World" script in the world
      • Language Constructs and Symantecs (If, For)
      • What is pipelining, Object orientation, interactive environment
      • Text Editor comparison
      • Special Tools (psanalyzer)
      • Sample code, sample code, sample code
      • Input & Output
      • How to research building a script
    • Day 2 - Getting At The System
      • PowerShell Direct Access
      • Registry and Filesystem
      • WMI
      • ADSI
      • .NET (a little bit)
      • Interfacing with CMD.EXE and Other EXEs
      • PowerShell Shortcomings and Compensations
    • Day 3 - Porting Scripts And Best Practice
      • VBScript and NT Shell Scripts (.BAT/.CMD)
      • COM Object Access
      • Favoring Native PowerShell Functions
      • Porting String Functions
      • Replicating VBScript String Functions
      • Regular Expressions Primer
      • Simple Scripts versus Production Ready versus Scripting Framework
      • Framework Script (process arguments, tracing and logging, error processing)
  • 位於德國的AddOn提供三天的課程(1725歐元 )[以下內容是從德文翻譯成英文]
      • Power shell introduction
        • Term "Monad"
        • Characteristics of the power shell
        • Conditions
        • From HelloWorld to GUI
      • Power shell bases
        • Instruction types
        • Search sequence
        • CmdLets
        • Aliase
        • Classes and objects
        • Pipeline Processor
        • Important Cmdlets
    • Power shell LANGUAGE
      • Variable one
      • Operators
      • Array
      • Hash Tables
      • Inquiries
      • Loops
      • Functions
      • Filter
      • Error handling
      • Debugging
  • Scriptdateien
    • Files create
    • Comments
    • Call
    • Safety configuration
    • Profiles
  • System administration
    • Systems management with power shell
    • Use of .NET Classes
    • Windows management instrumentation (WMI)
    • Component Object Model (COM)
    • Selection assistance object model
    • Active directory service interface (ADSI)
    • Extensible Markup LANGUAGE (XML)
    • Exchange server 2007

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

    #使用get-content path顯示檔案內容
    PS C:\> get-content c:\a.txt

    #使用${path} 顯示檔案內容
    PS C:\> ${C:\a.txt}

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


    #產生內容為PowerShell的char陣列
    PS C:\> [char[]]$Array = "P","o","w","e","r","S","h","e","l","l"

    #預設將陣列轉為字串時是以空白隔開

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

    • 輸入:  "2+2 is (2+2)"
      結果:   2+2 is (2+2)
      • 一般字串
    • 輸入:  "2+2 is $(2+2)"
      結果:   2+2 is 4
      • 雙引號中會將$開頭的作解析,括號內解析為4
    • 輸入:  "2+2 is $2+2"
      結果:   2+2 is +2
      • 雙引號中會將$開頭的作解析,$2無此變數因此不顯示,+2視為一般字串;若是事先定義$2="a",則結果為2+2 is a+2
    • 輸入:  '2+2 is (2+2)'
      結果:   2+2 is (2+2)
      • 單引號所見及所得
    • 輸入:  '2+2 is $(2+2)'

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

    在PowerShell中可以使用上下鍵瀏覽先前執行過的指令
    以利重複執行相同或類似的指令

    除此之外也可按下F7功能鍵瀏覽先前執行過的指令

    畫面跳出一小方塊

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


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

    • 得知A的ASCII code
      • [byte][char]"A"
        • 65
    • 得知ASCII code為65是什麼
      • [char]65
        • A

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

    欲得知指令的執行時間可使用measure-command

    PS C:\Joseph> measure-command{for($i=0;$i -lt 1000000;$i++){$i} }

    Days : 0
    Hours : 0
    Minutes : 0

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

    • 欲獲得17進位字串
      • PS C:\Joseph> [Convert]::ToString(17,2)
        • 10001
    • 欲獲得17進位字串
      • PS C:\Joseph> [Convert]::ToString(17,8)
        • 21
    • 欲獲得17十六進位字串
      • 方法一
        • PS C:\Joseph> [Convert]::ToString(17,16)
          • 11
      • 方法二
        • PS C:\Joseph> "{0:X}" -f 17
          • 11

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

    重點整理自Let's do a deep drill into Select-String by Jeffrey Snover , Windows PowerShell/MMC Architect

    • $c="a"
      get-alias ($c+"*")   #參數有變數 則用()包住  ; 等於get-alias a*
    • Select-String accepts arrays of wildcards to specify Files
      • select-string  欲搜尋字串  欲搜尋之檔案路徑(可為wildcard)
        • select-string  drive *  #搜尋目前路徑下所有檔案中包含*drive*的字串(預設視為前後加*)
        • 被搜尋到的對象不能為資料夾???
        • select-string  drive  [a-m]* #搜尋名為a-m開頭的檔案
        • select-string  drive  drive g*,r* #搜尋名為g,r開頭的檔案
    • Select-String accepts arrays of regular expressions to specify STRINGS
      • 欲搜尋字串可為regular expression
        • " [a-r]dr" *
        • " [a-r]dr|mount"
        • " [a-n]dr"," [m-z]dr"
      • 若不想讓搜尋字串被視為regular expression 則要指定 -SimpleMatch
    • Select-String accepts –Include and –Exclude to tweak which files it operates on
      • 欲搜尋檔案可利用–Include 包含某檔案
      • 欲搜尋檔案可利用–Exclude 排除某檔案
      • These parameters take a wildcard expression or set of wildcard expressions and operate AFTER the filepath is resolved
        • -exclude *[mn]-al*   #完整檔案路徑中(不只是檔名)不包含*[mn]-al*
        • -exclude [mn]-al*    #完整檔案路徑中不包含[mn]-al*
    • You can pipe anything that produces FileInfo objects into Select-String
      • 欲搜尋之檔案可由pipeline餵近來
      • dir [g-r]* |select-string  drive 
      • ([System.IO.DirectoryInfo]"c:\sstest").GetFileSystemInfos() |  select-string  drive
    • You can pipe anything that produces MatchInfo objects into Select-String
      • select-string get *|select-string item
    • Select-String outputs MatchInfo objects not Strings
      • Microsoft.PowerShell.Commands.MatchInfo
      • select-string  "stop","new.*ve" *|Format-List *
      • select-string  "stop","new.*ve" *|group Pattern
    • Sometimes you just want the first match in a file
      • select-string item r* -list

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

    http://manning.com/payette/


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

    http://blogs.technet.com/canitpro/archive/2006/11/20/podcast-powershell-with-bruce-payette.aspx


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

    (1)
    可以使用get-date指令取得現在時間

    執行
    PS C:\Joseph> (get-date).ToString("HH:mm:ss")

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

    (1)

    執行

    PS C:\Joseph> if((ping 140.112.107.132 | Where-Object {$_ -like "*(0% loss)*"}) -eq $null){"The computer is dead"}else{"The computer is alive"}

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

    若想要開啟某檔案
    可執行
    Invoke-Item 完整檔案路徑

    系統將使用預設關聯的應用程式開啟該檔案

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