Windows PowerShell คืออะไร Part1

0
9471

เริ่มต้นกับ Windows PowerShell คืออะไร Part1

สำหรับผู้ดูแลระบบที่สนใจอยากเริ่มต้นมาลองใช้ PowerShell ควรรู้ข้อมูลเบื้องต้นดังนี้ครับ

เบื้องต้นเกี่ยวกับ PowerShell

  • Windows PowerShell เริ่มพัฒนาและเปิดใช้ Version 1.0 ตั้งแต่ พฤศจิกายน 2006 ปัจจุบัน ณ วันที่บทความนี้เขียนคือปี 2014 เป็น Version 4.0 ที่มาพร้อมกับ Windows 8.1 และ Windows Server 2012r2
  • Windows PowerShell คือ Command Shell รูปแบบใหม่จาก Microsoft ที่รวม Scripting Language และ .NET Framework มาให้ด้วยในตัว
  • Windows PowerShell มีความสามารถเข้าถึง COM และ WMI บนระบบปฏิบัติการ Windows ได้โดยตรง และมีการรวมทำให้ผู้ดูแลระบบสามารถใช้คำสั่งที่ Advance ได้มากกว่า DOS CMD ในรูปแบบเดิม
  • รูปแบบคำสั่งสำเร็จรูปที่มากับ PowerShell นั้นเรียกว่า cmdlets (อ่านออกเสียงว่า Command-Lets)
  • Windows PowerShell Version 2.0 มีการเริ่มใช้งานจริงจังโดยที่แถมมากับ Windows 7 และ Windows Server 2008 R2 ส่วนระบบปฏิบัติการที่ Version ต่ำกว่านี้สามารถใช้ได้โดยแยก Download ต่างหาก ซึ่งรองรับ Windows รุ่นต่ำสุดดังนี้
    • Windows XP with Service Pack 3
    • Windows Server 2003 with Service Pack 2
    • Windows Vista with Service Pack 1

Windows PowerShell สามารถ Execute ได้ด้วย 4 วิธีดังนี้

1. ใช้บน cmdlets ที่อยู่บน interface ของ PowerShell เอง

2. ใช้เป็น Script โดยทำการ Save File เป็น .PS1

3. ใช้จาก Functions

4. ใช้จาก Executable Program

หมายเหตุ: PowerShell สามารถใช้ทาง remote ได้ โดยมีรวบรวมวิธีเปิด remote ไว้แล้วที่นี่ http://www.mvpskill.com/kb/powershell-tip-remote-management-using-windows-powershell.html

ประโยชน์(ในมุมของผมนะครับ)

  • สามารถใช้งานผ่าน remote ได้ดีกว่า DOS รวมถึงสามารถเรียกคำสั่งจาก dos มาทำงานได้เช่นเคย
  • เป็น Full Scripting language ที่ดีกว่า DOS และง่ายกว่า vbscript
  • คนที่ถนัด Unix Based อยู่แล้วพอมาใช้ Shell แบบนี้จะ get เร็วกว่า Windows Administrator ที่เคยใช้แต่ GUI

ลองเริ่มใช้งานกันดีกว่าครับ การติดตั้ง PowerShell

Windows PowerShell 3.0 มาพร้อมกับ Windows 8 Client และ Windows Server 2012 ส่วน

Windows PowerShell 4.0 มาพร้อมกับ Windows 8.1 Client และ Windows Server 2012R2

หากคุณใช้ PowerShell ที่ Version ต่ำกว่านี้สามารถแยก Download Version ใหม่มาติดตั้งได้ครับ (จำเป็นต้องมี .NET Framework ด้วยนะครับ)

หากไม่แน่ใจว่าเครื่องที่เรารับดูแลต่อจากผู้ดูแลระบบท่านอื่นใช้ PowerShell Version อะไรอยู่ ใช้คำสั่งนี้ได้ครับ

คุณใช้ PowerShell Version อะไรอยู่ ? คำสั่งที่เอาไว้ตรวจเช็คดังนี้

$PSversiontable

เริ่มต้นกับ Windows PowerShell คืออะไร Part1

หรือใช้ get-host

เริ่มต้นกับ Windows PowerShell คืออะไร Part1

Tip1: ตั้งค่าความปลอดภัยให้ PowerShell รัน Script .PS1 ได้

โดย Default แล้วการจะ run PowerShell Script ที่เป็น .ps1 นั้นสามารถตั้งค่าความปลอดภัยได้ว่าจะให้ทำงานในระดับไหน (ให้ Script ทำงานได้ หรือไม่ได้) คำสั่งตรวจเช็คว่าเครื่องนั้น Run Script .PS1 ได้หรือเปล่าใช้คำสั่งนี้ครับ

Get-ExecutionPolicy

จากรูปด้านล่างได้คำตอบมาว่า “Restricted” หมายความว่าเครื่องนี้ไม่ยอมให้ .ps1 ทำงานได้ครับ

เริ่มต้นกับ Windows PowerShell คืออะไร Part1

เวลาเอา PowerShell Script .ps1 ไปรันก็จะมี Error แดง ๆ แบบนี้ครับ

เริ่มต้นกับ Windows PowerShell คืออะไร Part1

หากต้องการปรับเปลี่ยนระดับความปลอดภัยให้ใช้คำสั่งนี้ครับ

Set-executionpolicy remotesigned > หมายถึงรัน Script จาก Remote Channel ได้

Set-executionpolicy unrestricted > หมายถึงรัน Script ได้ผ่านตลอดครับ

ในตัวอย่างนี้ผมใช้เป็น unrestricted ก่อนนะครับเพื่อพามาลองใช้งาน PowerShell กันง่าย ๆ หน่อย

เริ่มต้นกับ Windows PowerShell คืออะไร Part1

เริ่มต้นลองดูก่อนเลยครับด้วย Script นี้เอาไป Save เป็น File .PS1 เป็น Script ที่ผมเอามาจากหนังสือ Windows PowerShell 3.0 Step by Step ที่โหลดได้จากที่นี่ครับ

http://gallery.technet.microsoft.com/scriptcenter/Windows-PowerShell-30-Step-46825ea6#content

ตัวอย่าง (นำ Code ข้างล่างนี้ไป Save เป็นไฟล์ .ps1)

Param([string[]]$computer = @($env:computername, “LocalHost”))

foreach ($c in $computer)

{

$o = Get-WmiObject win32_operatingsystem -cn $c

switch ($o.version)

{

{$o.version -gt 6.2} {“$c is Windows 8 or greater”; break}

{$o.version -gt 6.1}

{

If($o.ServicePackMajorVersion -gt 0){$sp = $true}

If(Get-WmiObject Win32_Product -cn $c |

where { $_.name -match ‘.NET Framework 4’}) {$net = $true }

If($sp -AND $net) { “$c meets the requirements for PowerShell 3” ; break}

ElseIF (!$sp) {“$c needs a service pack”; break}

ELSEIF (!$net) {“$c needs a .NET Framework upgrade”} ; break}

{$o.version -lt 6.1} {“$c does not meet standards for PowerShell 3.0”; break}

Default {“Unable to tell if $c meets the standards for PowerShell 3.0”}

}

}

ผมตั้งชื่อ Script นี้ว่า temp.ps1

จึง run ด้วยคำสั่งนี้ครับ

เริ่มต้นกับ Windows PowerShell คืออะไร Part1

จากรูปที่ run script นี้สำเร็จ จะบอกว่าเครื่องผม Windows Server 2008R2 หากจะใช้ PowerShell 3.0 ต้อง Upgrade .NET Framework ก่อนนะครับ

Tip2: PowerShell สามารถส่งต่อ Output ของคำสั่งได้

ยกตัวอย่างเช่น สมัย DOS ถ้าจะสั่งคำสั่ง 3-4 คำสั่งพร้อมกัน จะต้อง RUN ในรูปแบบนี้

ipconfig /all >C:\tshoot.txt

route print >>C:\tshoot.txt

hostname >>C:\tshoot.txt

net statistics workstation >>C:\tshoot.txt

แต่ถ้าเป็น PowerShell จะสั่งด้วยบรรทัดเดียวได้เลยแบบนี้

ipconfig /all >tshoot.txt; route print >>tshoot.txt; netdiag /q >>tshoot.txt; net statistics workstation >>tshoot.txt

การให้ PowerShell ส่ง Output ไปยัง File แต่ไม่ต้องการให้เห็นค่า Banner ของ PowerShell ให้ใช้คำสั่ง

PowerShell –nologo

คำสั่งที่ใช้ประกอบเป็นประจำ

-whatif

หากต่อท้ายคำสั่งด้วย –whatif จะเป็นการแสดงผลว่าคำสั่งนี้จะทำอะไร แต่ไม่ได้ทำจริง ๆ

เริ่มต้นกับ Windows PowerShell คืออะไร Part1

-confirm

หากต่อท้ายด้วย –Confirm ระบบจะ Prompt ถามก่อนให้แน่ใจว่าต้องการ run คำสั่งนี้จริง ๆ หรือไม่ ?

เริ่มต้นกับ Windows PowerShell คืออะไร Part1

Tip3: คำสั่งขอความช่วยเหลือ get-help แบบ Online

ที่สำคัญคือ ใช้คำสั่ง get-help ต่อท้ายด้วยคำสั่งที่อยากดูคู่มือการใช้งาน เช่น

Get-help get-eventlog

เริ่มต้นกับ Windows PowerShell คืออะไร Part1

Get-help get-eventlog –examples

หรือหากต้องการไปดูอย่างรวดเร็วให้ใช้

Get-help get-eventlog –online

เริ่มต้นกับ Windows PowerShell คืออะไร Part1

Tip4: กลับมาเข้าใจ Cmdlets และ DOS กันอีกหน่อย

คำสั่งแบบ Alis

ดูจากรูปด้านล่างครับ

ถ้าเราจำ DOS ได้ คำสั่ง Dir คือดู list ข้อมูลใน Data นั้น ๆ

แต่ถ้าจะสั่งแบบ PowerShell ใช้เป็น Get-ChildItem ก็ได้

ดังนั้น Dir = Get-ChildItem

เริ่มต้นกับ Windows PowerShell คืออะไร Part1

ตัวอย่างอีกคำสั่งหนึ่งครับ

ใน DOS คำสั่ง CD = Change Directory

แต่ใน PowerShell สามารถใช้คำสั่ง Set-Location ได้ด้วย

TIP5: เรื่องการจัด Format ของ Output

คำสั่งต่อท้ายเวลาต้องการดู output สวย ๆของ PowerShell command คือการใช้ PIPE และ Format-List ครับ

ยกตัวอย่างเช่น

คำสั่ง Get-ChildItem c:\ จะแสดงผลดังนี้

เริ่มต้นกับ Windows PowerShell คืออะไร Part1

ถ้าเราอยากให้ output สวย ๆ กว่านี้มีตัวเลือกให้ลองดูครับ

Get-ChildItem c:\ | format-list ผลลัพธ์จะแสดงค่าที่เยอะและเป็นระเบียบกว่า

เริ่มต้นกับ Windows PowerShell คืออะไร Part1

อีกทั้งยังเลือก list output ที่ต้องการจาก Format-list ได้ด้วยครับตัวอย่างเช่นคำสั่ง

Get-ChildItem c:\ | Format-List -Property name,LastAccessTime

เริ่มต้นกับ Windows PowerShell คืออะไร Part1

อีกอันที่เจ๋งดีครับคือการสร้าง output แบบ Grid View มาดูก่อน

Get-EventLog -LogName security -Newest 10 | Out-GridView

ตัวอย่างผลลัพธ์แบบ GridView ครับ สามารถพิมพ์ Filter ได้เลยตามรูปด้านล่างครับ ง่

เริ่มต้นกับ Windows PowerShell คืออะไร Part1

อ้างอิง

Windows PowerShell

http://en.wikipedia.org/wiki/Windows_PowerShell

Scripting with Windows PowerShell (KB)

http://technet.microsoft.com/en-us/library/bb978526.aspx

Windows PowerShell Scripting (VDO)

http://technet.microsoft.com/en-us/scriptcenter/dd742419.aspx

Remote PowerShell

http://winintro.ru/windowspowershell2corehelp.en/html/f23b65e2-c608-485d-95f5-a8c20e00f1fc.htm

image