.NET Logo
Welcome Guest Search | Active Topics | Members | Log In | Register

powershell คร้ังที่ 9 Options · View
paedotnet
Posted: Sunday, December 23, 2007 7:51:46 PM

Rank: มือเทพ
Groups: Member

Joined: 12/6/2007
Posts: 344
Location: bkk

การดู Drive

คำสั่ง

Get-PSDrive

โดย Get-PsDrive จะมีพารามิเตอร์สี่ตัวคือ  -name,-psprovider,-literalname ,-scope

ถ้าต้องการดู Registry ก็ใช้

Get-PsDrive -psprovider Registry
 
การดูข้อมูลของไฟล์
เปิด notepad แล้วพิมพ์อะไรก็ได้ในตัวอย่างพิมพ์
1234567890
abcdefghijklmnopqrstuvwxyz
แล้ว save ในโฟว์เดอร์ของ powershell (ดูจากคำสั่ง pwd )
ใช้ชื่อว่า testps.txt
จากนั้นพิมพ์คำสั่งใน powershell ดังนี้
 
 
Get-Content -path testps1.txt

ผลลัพธ์จะได้ข้อความทีอยู่ใน notepad

ถ้าต้องการแสดงข้อมูลแบบกำหนดได้ว่าจะแสดงกี่บรรทัดใช้ parameter คือ
-totalcount จำนวนบรรทัด เช่น
ไปใน ไฟล์ testps1.txt เพิ่มข้อมูลเข้าไปหลายๆบรรทัด แล้ว ใช้คำสั่ง

Get-Content -path testps1.txt -totalcount 3

ผลลัพธ์จะแสดงข้อมูล สามบรรทัดออกมา


ถ้าต้องการนำข้อมูลที่พิมพ์ใน powershell ออกไปในไฟล์ .txt ก็ใช้ เครื่องหมาย >
ตัวอย่าง
 
 
"Hello How are you?"  > testps2.txt

ผลลัพธ์ลองไปเปิดไฟล์ testps2.txt จะมีข้อมูล Hello How are you? เกิดขึ้นมา

การเปิดไฟล์
รูปแบบ .\file_name.extension
เช่น
ถ้าต้องการเปิดไฟล์ที่ชื่อ testps2.txt ที่อยู่ในfolder หลัก (ดูจาก pwd) ก็ใช้คำสั่ง
 
 
.\testps2.txt
 
หลังจากใช้คำสั่งนี้จะมีไฟล์นี้เปิดขึ้นมา


การใช้งาน Read-Host

Read-Host ใช้รับค่าเข้ามา

ตัวอย่าง

$username = Read-Host "Please Enter username"
$password = Read-Host "Please Enter password"
Write-Host "username is $username and password is $password"
 
ในตัวอย่างนี้ password ถ้าใส่จะเห็นเป็นตัวอักษรที่ใส่เข้าไปเลยถ้าต้องการใส่ให้เห็นเป็น * ก็ใช้ พารามิเตอร์ดังนี้ -assecurestring
เช่น
 
$password = Read-Host "please Enter password" -assecurestring

 

การแสดงสีให้กับตัวอักษร
จะใช้ พารามิเตอร์คือ -foregroundcolor สำหรับใส่สีให้กับตัวอักษร และ  -backgroundcolor สำหรับใส่สีพื่นหลัง
ตัวอย่าง
 
 
Write-Host "Hello" -foregroundcolor Blue
 
ผลลัพธ์ตัวอักษรคำว่า Hello จะมีสีน้ำเงิน
 
Write-Host "Hello" -backgroundcolor Red
 
ผลลัพธ์ตัวอักษร Hello จะมีพื่นหลังสีแดง
ถ้าต้องการใส่สีทั้งพื่นหลังและตัวอักษรก็ใช้ดังนี้
 
 
Write-Host "Hello" -foregroundcolor Blue  -backgroundcolor Red
 

การทำงานเกี่ยวกับตัวเลข

ใน powershell เราสามารถใข้ operator เพื่อกระทำกับตัวเลขได้เลยเช่น +,-,*,/,% ตัวอย่าง
 
 
(2+4)/6
 
ผลลัพธ์จะได้ 1
หรือ
 
 
$a = 10
$b = 20
$c = $a + $b
$c
 
ผลลัพธ์จะได้ 30

การใช้งาน Set-Variable,new-Variable,Get-Variable,Remove-Variable,Clear-Variable

Set-Variable ใช้กำหนดตัวแปรพร้อมกับค่าเริ่มต้นของตัวแปร โดยจะใช้ พารามิเตอร์ -name สำหรับกำหนดชื่อ ตัวแปร , -value สำหรับกำหนดค่าตัวแปร
Get-Variable ตามด้วยชื่อตัวแปร ที่กำหนดใน -name เพื่อแสดงค่าตัวแปร
ตัวอย่าง
 
 
Set-Variable -name i -value 50
Get-Variable i
 
 
ผลลัพธ์แสดงดังรูป

คำอธิบาย ตอนแรกกำหนดตัวแปรชื่อว่า i มีค่าเป็น50 ในการเรียกดูตัวแปรที่สร้างไปใช้ Get-Variable i ซึ่งจะแสดงชื่อและvalue ของ
ตัวแปร i
เราสามารถแสดงค่าของตัวแปรได้โดยใช้ $i แทน Get-Variable

การใช้ New-Variable
ใช้สร้างตัวแปรใหม่ขึ้นมาคล้ายๆกับ Set-Variable

ตัวอย่างการใช้
 
 
New-Variable -name j
Set-Variable -name j -value 11
$j
 
 
ผลลัพธ์จะแสดง 11
คำอธิบายในตัวอย่างนี้สร้างตัวแปรใหม่ชื่อ j โดยใช้ New-Variable -name j
แล้วกำหนดค่าให้กับตัวแปร j โดยใช้ Set-Variable แล้วแสดงค่าตัวแปร j โดยใช้
 
$j
 
 
การใช้ Clear-Variable
คำสั่งนี้ใช้ลบข้อมูลหรือค่าของตัวแปรเช่น
จากตัวอย่างที่แล้ว $j จะแสดงค่า 11 ออกมาแต่ถ้าใช้คำสั่ง
 
Clear-Variable j
 
แล้วใช้คำสั่ง
 
$j
 
ผลลัพธ์จะไม่แสดงอะไรเนื่องจากว่าค่าของ ตัวแปร j ถูกลบแล้ว
การใช้ Remove-Variable
คำสั่งนี้ใช้ลบ ตัวแปรออกเช่น
ต้องการลบตัวแปร j ก็ใช้คำสั่งดังนี้
 
 
Remove-Variable j
 
หรือ
 
Remove-Variable -name j

 


 


paedotnet attached the following image(s):
picps3.jpg



[With great power comes great responsibility]
Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

YAFVision Theme Created by Jaben Cargman (Tiny Gecko)
Powered by Yet Another Forum.net version 1.9.1.8 (NET v2.0) - 3/29/2008
Copyright © 2003-2008 Yet Another Forum.net. All rights reserved.


Sponsored by