לדלג לתוכן

2.4 אובייקטי COM פתרון

הנה הפתרונות לתרגילים שניתנו:


פתרון 1: אינטגרציה עם Excel

# צור אובייקט COM של Excel
$Excel = New-Object -ComObject Excel.Application

# הפוך את Excel לגלוי
$Excel.Visible = $True

# צור גיליון עבודה חדש
$workbook = $Excel.Workbooks.Add()
$sheet = $workbook.Sheets.Item(1)

# הוסף נתונים לתוך הגיליון
$sheet.Cells.Item(1,1).Value = "שם"
$sheet.Cells.Item(1,2).Value = "גיל"
$sheet.Cells.Item(1,3).Value = "כתובת"

$sheet.Cells.Item(2,1).Value = "John"
$sheet.Cells.Item(2,2).Value = 30
$sheet.Cells.Item(2,3).Value = "123 Street, City"

$sheet.Cells.Item(3,1).Value = "Alice"
$sheet.Cells.Item(3,2).Value = 28
$sheet.Cells.Item(3,3).Value = "456 Avenue, Town"

# החלת עיצוב על השם
$sheet.Range("A1:C1").Font.Bold = $True
$sheet.Columns.AutoFit()

# שמור את הקובץ
$workbook.SaveAs("C:\work\Example.xlsx")

# סגור את Excel
$Excel.Quit()

פתרון 2: אוטומציה של שליחת מייל ב-Outlook

# צור אובייקט COM של Outlook
$Outlook = New-Object -ComObject Outlook.Application

# צור אובייקט של מייל חדש
$mail = $Outlook.CreateItem(0)

# הוסף פרטי המייל
$mail.Subject = "Hello from PowerShell"
$mail.Body = "This is an automated email sent using PowerShell."
$mail.To = "recipient@example.com"

# שלח את המייל
$mail.Send()

פתרון 3: יצירת מסמך Word עם טבלאות

# צור אובייקט COM של Word
$MSWord = New-Object -ComObject Word.Application

# הפוך את Word לגלוי
$MSWord.Visible = $True

# צור מסמך חדש
$mydoc = $MSWord.Documents.Add()

# צור טבלה עם 3 עמודות ו-5 שורות
$table = $mydoc.Tables.Add($MSWord.Selection.Range, 5, 3)

# הוסף נתונים לתוך הטבלה
$table.Cell(1,1).Range.Text = "שם"
$table.Cell(1,2).Range.Text = "גיל"
$table.Cell(1,3).Range.Text = "כתובת"

$table.Cell(2,1).Range.Text = "John"
$table.Cell(2,2).Range.Text = "30"
$table.Cell(2,3).Range.Text = "123 Street, City"

# שמור את המסמך
$mydoc.SaveAs("C:\work\WordTableExample.docx")

# סגור את המסמך ו-Word
$mydoc.Close()
$MSWord.Quit()

פתרון 4: הפעלת יישום חיצוני בעזרת COM

# צור אובייקט COM של מדפסת (לדוגמה)
$printer = New-Object -ComObject WScript.Network

# הפעל את המדפסת כדי להדפיס (במקום זה תוכל לשלוח פקודה למדפסת פיזית)
$printer.AddWindowsPrinterConnection("\\server\printer")

פתרון 5: אינטגרציה עם Microsoft PowerPoint

# צור אובייקט COM של PowerPoint
$PowerPoint = New-Object -ComObject PowerPoint.Application

# הפוך את PowerPoint לגלוי
$PowerPoint.Visible = $True

# צור מצגת חדשה
$presentation = $PowerPoint.Presentations.Add()

# הוסף שקף חדש
$slide = $presentation.Slides.Add(1, 1)  # 1 = סוג השקף "כותרת"

# הוסף טקסט לשקף
$slide.Shapes.Title.TextFrame.TextRange.Text = "Welcome to PowerShell"
$slide.Shapes.Placeholders.Item(2).TextFrame.TextRange.Text = "Created by PowerShell script"

# שמור את המצגת
$presentation.SaveAs("C:\work\PowerPointExample.pptx")

# סגור את המצגת ו-PowerPoint
$presentation.Close()
$PowerPoint.Quit()

פתרון 6: ניהול תהליך חיפוש ב-Word

# צור אובייקט COM של Word
$MSWord = New-Object -ComObject Word.Application

# הפוך את Word לגלוי
$MSWord.Visible = $True

# צור מסמך חדש
$mydoc = $MSWord.Documents.Add()

# הוסף טקסט למסמך
$mydoc.Content.Text = "This is a test document with some sample text."

# חפש טקסט במסמך
$findText = "sample"
$selection = $MSWord.Selection
$selection.Find.Text = $findText
$found = $selection.Find.Execute()

if ($found) {
    Write-Output "Text '$findText' found in the document."
} else {
    Write-Output "Text '$findText' not found in the document."
}

# סגור את המסמך ו-Word
$mydoc.Close()
$MSWord.Quit()