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

PowerShell ครั้งที่ 13 Options · View
paedotnet
Posted: Saturday, January 05, 2008 10:08:06 PM

Rank: มือเทพ
Groups: Member

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

ตัวอย่างภาษา VBScript ที่ใช้เขียน โปรแกรม Powershell
 

การสร้างอาร์เรย์
รูปแบบ
$array_name  =  member1,member2,member3,...
ตัวอย่าง
 

$a  =  1,2,3,4,5,6,7
$a
 

ผลลัพธ์จะแสดง
1
2
3
4
5
6
7

การใช้งาน พารามิเตอร์ -and
-and ใช้ในการตรวจสอบเงื่อนไข โดยถ้า condition1  -and  condition2 จะเป็นจริงเมื่อมีรูปแบบเดียวคือ condition1 , condition2 จะต้องเป็นจริงเท่านั้น
ตัวอย่าง


$a = 10
$b = 20
if( ($a -gt 15) -and ($b -gt 20) ){
Write-Host "true"
}
else{
Write-Host "false"
}

การใช้ Abs เพื่อแสดงค่าที่เป็นจำนวนบวก
ตัวอย่าง


$a = [Math]::Abs(-100)
$a


ผลลัพธ์จะได้ 100 (ค่าบวก)

การใช้คำสั่ง sin

$sin  = [Math]::Sin(60)
$sin


ผลลัพธ์ -0.30481

การแปลงชนิดข้อมูล
ตัวอย่างต้องการแปลง double เป็น int ก็เขียนดังนี้


$a = [int]45.666
$a


ผลลัพธ์จะได้ 46 เนื่องจากจะปัดค่าทศนิยมขึ้นถ้าตัวเลขหลังทศนิยมนั้นมากกว่าหรือเท่ากับ 5

การแปลงรหัส ASCII ให้เป็นตัวอักษร
ตัวอย่าง


$chr  = [Char]65
$chr


ผลลัพธ์จะได้ A

การกำหนดค่าคงที่ Constant

Set-Variable  -Name  aa  -Value  50  -Option Constant


หลังจากที่สร้างตัวแปร aa ที่มีค่าเท่ากับ 50 แล้วถ้าเราเปลียนค่าใหม่ตามคำสั่งด้านล่างนี้จะเกิด error
# $aa = 10 เกิด errorเพราะไม่สามารถเปลียนค่าที่กำหนดเป็น Constant ได้

การจัดการเกี่ยวกับ Date


$d = Get-Date -format d
$d


ผลลัพธ์จะแสดงวันเดือนปี
หรือ


$d = Get-Date -format M
$d
 

ก็ได้
การแสดงวันเดือนปีต่างๆ

$d = (Get-Date).Day
$d = (Get-Date).Month
$d =(Get-Date).Year
$d =(Get-Date).Minute
$d = (Get-Date).TimeOfDay
การรันคำสั่ง ใช้ Invoke-Expression
คำสั่งนี้เป็นการรันคำสั่งที่มีข้อมูลเป็น string
ตัวอย่าง

$i = "Get-Service"  # คำสั่งที่อยู่ในรูปของ string
Invoke-Expression $i
 
ผลลัพธ์จะแสดง Service ออกมา

การ Filter โดยใช้ Where-Object เพื่อแสดงผลลัพธ์ตามที่ต้องการ

$a = "one","two","three","four"
$result = $a | Where-Object {$_ -match "tw*"}
$result
 
 
ผลลัพธ์จะได้
two
three

การตัดเลขทศนิยมออกโดยใช้  truncate
ตัวอย่าง


$i = 1000.9999
$j = [Math]::Truncate($i)
$j


ผลลัพธ์จะได้ 1000


การ จัดรูปแบบ


$i =  500
$j  = "{0:C}" -f $i
$j

การใช้งานเมธอด tolongdatestring(),tolongtimestring()


$d = $(Get-Date).ToLongDateString()
$d
$d =$(Get-Date).ToShortDateString()
$d

การแสดง version


$h = (Get-Host).Version
$h

 



[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