New-Object cmdlet:我如何知道它是新进程还是现有进程?

时间:2016-02-05 12:31:35

标签: powershell outlook

PowerShell 4.0

我通过Outlook发送电子邮件:

# Code sources:
# http://www.computerperformance.co.uk/powershell/powershell_function_send_email.htm
Function Global:Send-Email {
[cmdletbinding()]
Param (
[Parameter(Mandatory=$False,Position=0)]
[String]$Address = "abcd@yandex.ru",
[Parameter(Mandatory=$False,Position=1)]
[String]$Subject = "Hello!",
[Parameter(Mandatory=$False,Position=2)]
[String]$Body = "Hello from PowerShell through Outlook."
      )
Begin {
Clear-Host
# Add-Type -assembly "Microsoft.Office.Interop.Outlook"
    }
Process {
# Create an instance Microsoft Outlook
$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.CreateItem(0)
$Mail.To = "$Address"
$Mail.Subject = $Subject
# $Mail.Body =$Body
$Mail.HTMLBody = "<b>When</b> is swimming?"
$File = "$env:HOMEDRIVE\Temp\1.txt"
$Mail.Attachments.Add($File)
$Mail.Send()
       } # End of Process section
End {
# Section to prevent error message in Outlook
$Outlook.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Outlook)
$Outlook = $null
   } # End of End section!
} # End of function

# Example of using this function
Send-Email #-Address deck@swimmingpool.com

此代码要么创建新的Outlook进程(此类进程仍然不存在),要么使用现有的Outlook进程。我只想在以前没有工作的情况下关闭Outlook。因此,如果Outlook在我的脚本开始工作之前由用户启动,那么我就不应该完成Outlook进程。我怎么知道$Outlook使用新流程还是使用现有的Outlook流程?

如果我启动Get-Command -Noun *object*,那么我看不到get-object之类的内容(首先尝试接收现有对象)。

0 个答案:

没有答案