วิธีเขียนโมดูลสคริปต์ PowerShell
โมดูลสคริปต์คือสคริปต์ PowerShell ที่ถูกต้องซึ่งบันทึกไว้ในส่วนขยาย .psm1 ส่วนขยายนี้อนุญาตให้กลไก PowerShell ใช้กฎและ cmdlets ของโมดูลในไฟล์ของคุณ ความสามารถเหล่านี้ส่วนใหญ่มีไว้เพื่อช่วยให้คุณติดตั้งโค้ดของคุณบนระบบอื่น รวมถึงจัดการการกำหนดขอบเขต คุณยังสามารถใช้ไฟล์รายการโมดูล ซึ่งอธิบายการติดตั้งและวิธีแก้ปัญหาที่ซับซ้อนมากขึ้น
การเขียนโมดูลสคริปต์ PowerShell
หากต้องการสร้างโมดูลสคริปต์ ให้บันทึกสคริปต์ PowerShell ที่ถูกต้องลงในไฟล์ .psm1 สคริปต์และไดเร็กทอรีที่เก็บไว้ต้องใช้ชื่อเดียวกัน ตัวอย่างเช่น สคริปต์ชื่อ MyPsScript.psm1 จะถูกจัดเก็บไว้ในไดเรกทอรีชื่อ MyPsScript
ไดเรกทอรีของโมดูลจะต้องอยู่ในเส้นทางที่ระบุใน $env:PSModulePath ไดเรกทอรีของโมดูลสามารถประกอบด้วยทรัพยากรใดๆ ที่จำเป็นในการเรียกใช้สคริปต์ และไฟล์รายการโมดูลที่อธิบายให้ PowerShell ทราบว่าโมดูลของคุณทำงานอย่างไร
สร้างโมดูล PowerShell พื้นฐาน
ขั้นตอนต่อไปนี้อธิบายวิธีการสร้างโมดูล PowerShell
บันทึกสคริปต์ PowerShell ด้วยส่วนขยาย
.psm1ใช้ชื่อเดียวกันสำหรับสคริปต์และไดเร็กทอรีที่บันทึกสคริปต์การบันทึกสคริปต์ด้วยส่วนขยาย
.psm1หมายความว่าคุณสามารถใช้ cmdlets ของโมดูลได้ เช่น Import-Module cmdlet ของโมดูลมีอยู่เพื่อให้คุณสามารถนำเข้าและส่งออกโค้ดของคุณไปยังระบบของผู้ใช้รายอื่นได้ โซลูชันสำรองคือการโหลดโค้ดของคุณบนระบบอื่นแล้วจึงดอทซอร์สลงในหน่วยความจำที่ใช้งานอยู่ ซึ่งไม่ใช่โซลูชันที่ปรับขนาดได้ สำหรับข้อมูลเพิ่มเติม โปรดดูที่ การทำความเข้าใจโมดูล Windows PowerShell ตามค่าเริ่มต้น เมื่อผู้ใช้นำเข้าไฟล์.psm1คุณจะสามารถเข้าถึงฟังก์ชันทั้งหมดในสคริปต์ของคุณได้ แต่ตัวแปรไม่สามารถเข้าถึงได้ตัวอย่างสคริปต์ PowerShell ชื่อ
Show-Calendarมีอยู่ที่ส่วนท้ายของบทความนี้function Show-Calendar { param( [DateTime] $start = [DateTime]::Today, [DateTime] $end = $start, $firstDayOfWeek, [int[]] $highlightDay, [string[]] $highlightDate = [DateTime]::Today.ToString('yyyy-MM-dd') ) #actual code for the function goes here see the end of the topic for the complete code sample }หากต้องการควบคุมการเข้าถึงฟังก์ชันหรือตัวแปรบางอย่างของผู้ใช้ ให้เรียก Export-ModuleMember ที่ส่วนท้ายของสคริปต์
โค้ดตัวอย่างที่ด้านล่างของบทความมีเพียงฟังก์ชันเดียวเท่านั้น ซึ่งโดยค่าเริ่มต้นจะถูกเปิดเผย อย่างไรก็ตาม ขอแนะนำให้คุณระบุฟังก์ชันที่คุณต้องการเปิดเผยอย่างชัดเจน ดังที่อธิบายไว้ในโค้ดต่อไปนี้:
function Show-Calendar { } Export-ModuleMember -Function Show-Calendarคุณสามารถจำกัดสิ่งที่นำเข้าได้โดยใช้รายการโมดูล สำหรับข้อมูลเพิ่มเติม โปรดดูที่การนำเข้าโมดูล PowerShell และวิธีเขียนรายการโมดูล PowerShell
หากคุณมีโมดูลที่โมดูลของคุณเองจำเป็นต้องโหลด คุณสามารถใช้
Import-Moduleที่ด้านบนของโมดูลImport-Modulecmdlet จะนำเข้าโมดูลเป้าหมายไปยังระบบ และสามารถนำมาใช้ในภายหลังในขั้นตอนการติดตั้งโมดูลของคุณเอง รหัสตัวอย่างที่ด้านล่างของบทความนี้ไม่ได้ใช้โมดูลการนำเข้าใดๆ แต่ถ้าเป็นเช่นนั้น รายการเหล่านั้นจะแสดงอยู่ที่ด้านบนของไฟล์ ดังที่แสดงในโค้ดต่อไปนี้:Import-Module GenericModuleหากต้องการอธิบายโมดูลของคุณต่อระบบวิธีใช้ PowerShell คุณสามารถใช้ความคิดเห็นวิธีใช้มาตรฐานภายในไฟล์ หรือสร้างไฟล์วิธีใช้เพิ่มเติมได้
ตัวอย่างโค้ดที่ด้านล่างของบทความนี้มีข้อมูลวิธีใช้ในความคิดเห็น คุณยังสามารถเขียนไฟล์ XML แบบขยายที่มีเนื้อหาวิธีใช้เพิ่มเติมได้ สำหรับข้อมูลเพิ่มเติม ดูวิธีใช้การเขียนสำหรับโมดูล Windows PowerShell
หากคุณมีโมดูล ไฟล์ XML หรือเนื้อหาอื่นเพิ่มเติมที่คุณต้องการจัดแพ็คเกจพร้อมกับโมดูลของคุณ คุณสามารถใช้รายการโมดูลได้
รายการโมดูลคือไฟล์ที่ประกอบด้วยชื่อของโมดูลอื่นๆ โครงร่างไดเร็กทอรี หมายเลขเวอร์ชัน ข้อมูลผู้เขียน และข้อมูลอื่นๆ PowerShell ใช้ไฟล์รายการโมดูลเพื่อจัดระเบียบและปรับใช้โซลูชันของคุณ สำหรับข้อมูลเพิ่มเติม โปรดดูวิธีการเขียนรายการโมดูล PowerShell
หากต้องการติดตั้งและรันโมดูลของคุณ ให้บันทึกโมดูลลงในพาธ PowerShell ที่เหมาะสม และใช้
Import-Moduleเส้นทางที่คุณสามารถติดตั้งโมดูลของคุณจะอยู่ในตัวแปรร่วม
$env:PSModulePathตัวอย่างเช่น พาธทั่วไปในการบันทึกโมดูลบนระบบจะเป็น%SystemRoot%/users//Documents/PowerShell/Modules/ อย่าลืมสร้างไดเร็กทอรีสำหรับโมดูลของคุณที่ใช้ชื่อเดียวกันกับโมดูลสคริปต์ แม้ว่าจะเป็นเพียงไฟล์ .psm1ไฟล์เดียวก็ตาม หากคุณไม่ได้บันทึกโมดูลของคุณไว้ในพาธเหล่านี้ คุณจะต้องระบุตำแหน่งของโมดูลในคำสั่งImport-Moduleมิฉะนั้น PowerShell จะไม่สามารถค้นหาโมดูลได้บันทึก
เริ่มต้นด้วย PowerShell 3.0 หากคุณได้วางโมดูลของคุณไว้ในเส้นทางโมดูล PowerShell อันใดอันหนึ่ง คุณไม่จำเป็นต้องนำเข้าโมดูลอย่างชัดเจน โมดูลของคุณจะถูกโหลดโดยอัตโนมัติเมื่อผู้ใช้เรียกใช้ฟังก์ชันของคุณ สำหรับข้อมูลเพิ่มเติมเกี่ยวกับเส้นทางของโมดูล โปรดดูที่การนำเข้าโมดูล PowerShell และ about_PSModulePath
หากต้องการลบโมดูลออกจากบริการที่ใช้งานอยู่ในเซสชัน PowerShell ปัจจุบัน ให้ใช้ Remove-Module
บันทึก
Remove-Moduleจะลบโมดูลออกจากเซสชัน PowerShell ปัจจุบัน แต่จะไม่ถอนการติดตั้งโมดูลหรือลบไฟล์ของโมดูล
ตัวอย่างโค้ดแสดงปฏิทิน
ตัวอย่างต่อไปนี้คือโมดูลสคริปต์ที่มีฟังก์ชันเดียวชื่อ Show-Calendar ฟังก์ชันนี้แสดงภาพปฏิทิน ตัวอย่างประกอบด้วยสตริงวิธีใช้ PowerShell สำหรับเรื่องย่อ คำอธิบาย ค่าพารามิเตอร์ และโค้ด เมื่อนำเข้าโมดูล คำสั่ง Export-ModuleMember ช่วยให้มั่นใจได้ว่าฟังก์ชัน Show-Calendar จะถูกส่งออกเป็นสมาชิกโมดูล
<#
.Synopsis
Displays a visual representation of a calendar.
.Description
Displays a visual representation of a calendar. This function supports multiple months
and lets you highlight specific date ranges or days.
.Parameter Start
The first month to display.
.Parameter End
The last month to display.
.Parameter FirstDayOfWeek
The day of the month on which the week begins.
.Parameter HighlightDay
Specific days (numbered) to highlight. Used for date ranges like (25..31).
Date ranges are specified by the Windows PowerShell range syntax. These dates are
enclosed in square brackets.
.Parameter HighlightDate
Specific days (named) to highlight. These dates are surrounded by asterisks.
.Example
# Show a default display of this month.
Show-Calendar
.Example
# Display a date range.
Show-Calendar -Start "March, 2010" -End "May, 2010"
.Example
# Highlight a range of days.
Show-Calendar -HighlightDay (1..10 + 22) -HighlightDate "2008-12-25"
#>
function Show-Calendar {
param(
[DateTime] $start = [DateTime]::Today,
[DateTime] $end = $start,
$firstDayOfWeek,
[int[]] $highlightDay,
[string[]] $highlightDate = [DateTime]::Today.ToString('yyyy-MM-dd')
)
## Determine the first day of the start and end months.
$start = New-Object DateTime $start.Year,$start.Month,1
$end = New-Object DateTime $end.Year,$end.Month,1
## Convert the highlighted dates into real dates.
[DateTime[]] $highlightDate = [DateTime[]] $highlightDate
## Retrieve the DateTimeFormat information so that the
## calendar can be manipulated.
$dateTimeFormat = (Get-Culture).DateTimeFormat
if($firstDayOfWeek)
{
$dateTimeFormat.FirstDayOfWeek = $firstDayOfWeek
}
$currentDay = $start
## Process the requested months.
while($start -le $end)
{
## Return to an earlier point in the function if the first day of the month
## is in the middle of the week.
while($currentDay.DayOfWeek -ne $dateTimeFormat.FirstDayOfWeek)
{
$currentDay = $currentDay.AddDays(-1)
}
## Prepare to store information about this date range.
$currentWeek = New-Object PsObject
$dayNames = @()
$weeks = @()
## Continue processing dates until the function reaches the end of the month.
## The function continues until the week is completed with
## days from the next month.
while(($currentDay -lt $start.AddMonths(1)) -or
($currentDay.DayOfWeek -ne $dateTimeFormat.FirstDayOfWeek))
{
## Determine the day names to use to label the columns.
$dayName = "{0:ddd}" -f $currentDay
if($dayNames -notcontains $dayName)
{
$dayNames += $dayName
}
## Pad the day number for display, highlighting if necessary.
$displayDay = " {0,2} " -f $currentDay.Day
## Determine whether to highlight a specific date.
if($highlightDate)
{
$compareDate = New-Object DateTime $currentDay.Year,
$currentDay.Month,$currentDay.Day
if($highlightDate -contains $compareDate)
{
$displayDay = "*" + ("{0,2}" -f $currentDay.Day) + "*"
}
}
## Otherwise, highlight as part of a date range.
if($highlightDay -and ($highlightDay[0] -eq $currentDay.Day))
{
$displayDay = "[" + ("{0,2}" -f $currentDay.Day) + "]"
$null,$highlightDay = $highlightDay
}
## Add the day of the week and the day of the month as note properties.
$currentWeek | Add-Member NoteProperty $dayName $displayDay
## Move to the next day of the month.
$currentDay = $currentDay.AddDays(1)
## If the function reaches the next week, store the current week
## in the week list and continue.
if($currentDay.DayOfWeek -eq $dateTimeFormat.FirstDayOfWeek)
{
$weeks += $currentWeek
$currentWeek = New-Object PsObject
}
}
## Format the weeks as a table.
$calendar = $weeks | Format-Table $dayNames -AutoSize | Out-String
## Add a centered header.
$width = ($calendar.Split("`n") | Measure-Object -Maximum Length).Maximum
$header = "{0:MMMM yyyy}" -f $start
$padding = " " * (($width - $header.Length) / 2)
$displayCalendar = " `n" + $padding + $header + "`n " + $calendar
$displayCalendar.TrimEnd()
## Move to the next month.
$start = $start.AddMonths(1)
}
}
Export-ModuleMember -Function Show-Calendar