2.3 פאוורשל מתקדם פתרון
פתרונות לתרגולים על יצירת Cmdlet ו-Class ב-PowerShell¶
- יצירת Cmdlet בסיסי:
יצירת קובץ Module בשם MyCustomCmdlets.psm1 עם Cmdlet שמחזיר הודעה מותאמת אישית:
# MyCustomCmdlets.psm1
function Get-Greeting {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true, Position=0)]
[string]$Name
)
process {
Write-Output "Hello, $Name!"
}
}
ייבוא המודול והפעלת ה-Cmdlet:
# ייבוא המודול
Import-Module -Name "C:\path\to\MyCustomCmdlets.psm1"
# הפעלת ה-Cmdlet
Get-Greeting -Name "John"
- יצירת Cmdlet עם פרמטרים אופציונליים:
יצירת Cmdlet שמקבל פרמטר גיל אופציונלי:
# MyCustomCmdlets.psm1
function Get-GreetingWithAge {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true, Position=0)]
[string]$Name,
[Parameter(Position=1)]
[int]$Age = 0
)
process {
if ($Age -gt 0) {
Write-Output "Hello, $Name! You are $Age years old."
} else {
Write-Output "Hello, $Name!"
}
}
}
ייבוא המודול והפעלת ה-Cmdlet:
Import-Module -Name "C:\path\to\MyCustomCmdlets.psm1"
Get-GreetingWithAge -Name "John"
Get-GreetingWithAge -Name "John" -Age 30
- יצירת Class עם מאפיינים ושיטות:
יצירת מחלקה Car עם מאפיינים ושיטה להצגת פרטי הרכב:
class Car {
[string]$Model
[int]$Year
Car([string]$model, [int]$year) {
$this.Model = $model
$this.Year = $year
}
[void]DisplayInfo() {
Write-Output "Car Model: $($this.Model), Year: $($this.Year)"
}
}
# יצירת אובייקט מהמחלקה
$myCar = [Car]::new("Toyota Corolla", 2020)
$myCar.DisplayInfo()
- הגדרת Class עם שדות ופעולות מתקדמות:
יצירת מחלקה BankAccount עם שיטות הפקדה ומשיכה:
class BankAccount {
[string]$AccountHolder
[decimal]$Balance
BankAccount([string]$accountHolder, [decimal]$balance) {
$this.AccountHolder = $accountHolder
$this.Balance = $balance
}
[void]Deposit([decimal]$amount) {
$this.Balance += $amount
Write-Output "Deposited $amount. New balance: $($this.Balance)"
}
[void]Withdraw([decimal]$amount) {
if ($this.Balance -ge $amount) {
$this.Balance -= $amount
Write-Output "Withdrew $amount. New balance: $($this.Balance)"
} else {
Write-Output "Insufficient funds for withdrawal."
}
}
}
# יצירת אובייקט
$account = [BankAccount]::new("John Doe", 1000)
$account.Deposit(500)
$account.Withdraw(200)
$account.Withdraw(1500)
- יצירת Cmdlet שמקבל מערך כפרמטר ומבצע עליו פעולה:
Cmdlet שמקבל מערך ומחזיר את המספר הגדול ביותר:
# MyCustomCmdlets.psm1
function Get-MaxNumber {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)]
[int[]]$Numbers
)
process {
$maxNumber = $Numbers | Sort-Object -Descending | Select-Object -First 1
Write-Output "The largest number is: $maxNumber"
}
}
ייבוא המודול והפעלת ה-Cmdlet:
- הפעלת Cmdlet משולב עם Class:
יצירת מחלקה Employee עם מאפיינים ושיטה, ו-Cmdlet שמציג את פרטי העובד:
class Employee {
[string]$Name
[string]$Position
[decimal]$Salary
Employee([string]$name, [string]$position, [decimal]$salary) {
$this.Name = $name
$this.Position = $position
$this.Salary = $salary
}
[void]DisplayInfo() {
Write-Output "Employee Name: $($this.Name), Position: $($this.Position), Salary: $($this.Salary)"
}
}
function Get-EmployeeInfo {
param (
[Parameter(Mandatory=$true)]
[Employee]$Employee
)
$Employee.DisplayInfo()
}
# יצירת אובייקט Employee והפעלת ה-Cmdlet
$emp = [Employee]::new("John Doe", "Manager", 80000)
Get-EmployeeInfo -Employee $emp