לדלג לתוכן

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

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


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

# Create a COM object for Excel
$Excel = New-Object -ComObject Excel.Application

# Make Excel visible
$Excel.Visible = $True

# Create a new worksheet
$workbook = $Excel.Workbooks.Add()
$sheet = $workbook.Sheets.Item(1)

# Add data to the worksheet
$sheet.Cells.Item(1,1).Value = "Name"
$sheet.Cells.Item(1,2).Value = "Age"
$sheet.Cells.Item(1,3).Value = "Address"

$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"

# Apply formatting to the header
$sheet.Range("A1:C1").Font.Bold = $True
$sheet.Columns.AutoFit()

# Save the file
$workbook.SaveAs("C:\work\Example.xlsx")

# Close Excel
$Excel.Quit()

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

# Create a COM object for Outlook
$Outlook = New-Object -ComObject Outlook.Application

# Create a new email object
$mail = $Outlook.CreateItem(0)

# Add the email details
$mail.Subject = "Hello from PowerShell"
$mail.Body = "This is an automated email sent using PowerShell."
$mail.To = "recipient@example.com"

# Send the email
$mail.Send()

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

# Create a COM object for Word
$MSWord = New-Object -ComObject Word.Application

# Make Word visible
$MSWord.Visible = $True

# Create a new document
$mydoc = $MSWord.Documents.Add()

# Create a table with 3 columns and 5 rows
$table = $mydoc.Tables.Add($MSWord.Selection.Range, 5, 3)

# Add data to the table
$table.Cell(1,1).Range.Text = "Name"
$table.Cell(1,2).Range.Text = "Age"
$table.Cell(1,3).Range.Text = "Address"

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

# Save the document
$mydoc.SaveAs("C:\work\WordTableExample.docx")

# Close the document and Word
$mydoc.Close()
$MSWord.Quit()

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

# Create a COM object for a printer (example)
$printer = New-Object -ComObject WScript.Network

# Activate the printer to print (here you could send a command to a physical printer)
$printer.AddWindowsPrinterConnection("\\server\printer")

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

# Create a COM object for PowerPoint
$PowerPoint = New-Object -ComObject PowerPoint.Application

# Make PowerPoint visible
$PowerPoint.Visible = $True

# Create a new presentation
$presentation = $PowerPoint.Presentations.Add()

# Add a new slide
$slide = $presentation.Slides.Add(1, 1)  # 1 = slide type "Title"

# Add text to the slide
$slide.Shapes.Title.TextFrame.TextRange.Text = "Welcome to PowerShell"
$slide.Shapes.Placeholders.Item(2).TextFrame.TextRange.Text = "Created by PowerShell script"

# Save the presentation
$presentation.SaveAs("C:\work\PowerPointExample.pptx")

# Close the presentation and PowerPoint
$presentation.Close()
$PowerPoint.Quit()

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

# Create a COM object for Word
$MSWord = New-Object -ComObject Word.Application

# Make Word visible
$MSWord.Visible = $True

# Create a new document
$mydoc = $MSWord.Documents.Add()

# Add text to the document
$mydoc.Content.Text = "This is a test document with some sample text."

# Search for text in the document
$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."
}

# Close the document and Word
$mydoc.Close()
$MSWord.Quit()