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

powershell ครั้งที่ 20 Options · View
paedotnet
Posted: Thursday, February 21, 2008 4:03:13 AM

Rank: มือเทพ
Groups: Member

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

การใช้งาน Variable Provider
variable provider สามารถทำให้เราเข้าถึง ตัวแปรที่ถูกกำหนดใน powershell ได้ เช่น $host
ในการกำหนด location ให้เป็น variable สามารถเขียนได้ดังนี้


Set-Location Variable:\


ผลลัพธ์โปรแกรมจะเปลี่ยน path ไปเป็น PS Variable:\>
เราสามารถดูรายละเอียดที่อยู่ใน location นี้ได้โดยใช้คำสั่งดังนี้


Get-ChildItem


เราสามารถใช้คำสั่ง get-variable เพื่อดูตัวแปรได้ดังนี้


Get-Variable


ถ้าต้องการสร้างตัวแปรใหม่ ก็เขียนได้ดังนี้


New-Variable myvariable1


โดย myvariable1 เป็นตัวแปรที่เราสร้างขึ้นมา
ถ้าต้องการดูตัวแปรที่เราสร้างขึ้นมาก็ใช้คำสั่ง


Get-Variable myvariable1


แต่ตัวแปรที่เราสร้างขึ้นนี้ยังไม่มีค่า Value ดังนั้นถ้าต้องการกำหนดค่า Value ให้กับตัวแปรนี้ก็เขียนได้ดังนี้


Set-Variable myvariable1 -Value Hello


แล้วใช้คำสั่งเพื่อเรียกดูค่าของตัวแปรดังนี้


Get-Variable myvariable1


ถ้าต้องการลบตัวแปรที่สร้างขึ้นเองออกก็ใช้คำสั่ง


Remove-Variable myvariable1

การสร้าง psdrive ขึ้นมาเอง
ปกติแล้ว psdrive ระบบจะเป็นคนสร้างเช่น function:\,env:\ ฯ ถ้าเราต้องการสร้างขึ้นมาเองก็เขียนได้ดังนี้


New-PSDrive -Name mypsdrive1 -PSProvider alias -Root .


โดย alias นี้เป็น provider ที่ต้องการในตัวอย่างจะใช้ alias เป็น provider ก็เลยเขียน เป็น alias เพื่อสร้าง drive สำหรับ alias
แล้วเปลียนเป็น drive ที่สร้างดังนี้


Set-Location mypsdrive1:\


แล้วลองดูรายละเอียดของ mypsdrive1 นี้ว่ามีอะไรบ้างก็ใช้คำสั่ง


 Get-ChildItem

ในการลบ psdrive ที่สร้างขึ้นก็ใช้คำสั่งดังนี้
1. ต้องออกจาก psdrive ที่สร้างขึ้นก่อนในกรณีที่ทำงานอยู่กับ drive นี้โดยใช้คำสั่ง 
   Set-Location c:\

2. แล้วใช้คำสั่ง
 
Remove-PSDrive mypsdrive1

3. หลังจากนั้นตรวจสอบดูว่า psdrive ที่ต้องการลบออกหรือยังโดยใช้คำสั่ง Get-PSDrive

 



[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