執行指令
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
目前分類:PowerShell (163)
- Dec 06 Wed 2006 08:25
PowerShell:利用WMI計算電腦開機多久
- Dec 06 Wed 2006 07:32
PowerShell:利用WMI偵測電腦是否有插入隨身硬碟
指令如下
Get-WmiObject -Class Win32_USBHub | where {$_.description -like
"*Mass Storage*"} | format-table DeviceID, Description
- Dec 03 Sun 2006 06:00
Technorati
- Dec 03 Sun 2006 03:26
PowerShell V.S. Cmd.exe
- 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
- Dec 02 Sat 2006 20:29
PowerShell:整理檔案
- Dec 02 Sat 2006 15:56
國外PowerShell教育訓練課程
- 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
- 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
- Dec 02 Sat 2006 08:12
PowerShell:顯示檔案
#使用get-content path顯示檔案內容
PS C:\> get-content c:\a.txt
#使用${path} 顯示檔案內容
PS C:\> ${C:\a.txt}
- Dec 02 Sat 2006 03:33
PowerShell:$OFS
#產生內容為PowerShell的char陣列
PS C:\> [char[]]$Array = "P","o","w","e","r","S","h","e","l","l"
#預設將陣列轉為字串時是以空白隔開
- Nov 30 Thu 2006 04:30
PowerShell:雙引號與單引號
- 輸入: "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)'
- Nov 27 Mon 2006 06:52
PowerShell:Command History
- Nov 26 Sun 2006 21:41
PowerShell:安裝
- 下載.NET Framework Version 2.0
Microsoft .NET Framework Version 2.0 Redistributable Package (x86)
http://www.microsoft.com/downloads/details.aspx?FamilyID=0856eacb-4362-4b0d-8edd-aab15c5e04f5&DisplayLang=en - 安裝.NET Framework Version 2.0
- 下載適合自己作業系統的Windows PowerShell 1.0
http://www.microsoft.com/technet/scriptcenter/topics/msh/download.mspx - 安裝Windows PowerShell 1.0
- Nov 26 Sun 2006 03:00
PowerShell:ASCII
- 得知A的ASCII code
- [byte][char]"A"
- 65
- 得知ASCII code為65是什麼
- [char]65
- A
- Nov 26 Sun 2006 02:51
PowerShell:Measure-command
欲得知指令的執行時間可使用measure-command
PS C:\Joseph> measure-command{for($i=0;$i -lt 1000000;$i++){$i} }
Days : 0
Hours : 0
Minutes : 0
- Nov 26 Sun 2006 02:36
PowerShell:二進位,八進位以及十六進位
- 欲獲得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
- Nov 25 Sat 2006 08:34
PowerShell:Select-String
重點整理自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
- Nov 23 Thu 2006 05:37
Windows PowerShell in Action - EARLY ACCESS EDITION
- Nov 23 Thu 2006 05:30
[Podcast] PowerShell with Bruce Payette
- Nov 22 Wed 2006 07:55
PowerShell:現在時間
(1)
可以使用get-date指令取得現在時間
執行
PS C:\Joseph> (get-date).ToString("HH:mm:ss")
- Nov 22 Wed 2006 07:32
PowerShell, ping and ipconfig
(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"}
- Nov 19 Sun 2006 23:46
PowerShell:Invoke-Item
若想要開啟某檔案
可執行
Invoke-Item 完整檔案路徑
系統將使用預設關聯的應用程式開啟該檔案