powershell PowerShell:关闭Outlook
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了powershell PowerShell:关闭Outlook相关的知识,希望对你有一定的参考价值。
<#
.SYNOPSIS
Closes all running Outlook processes
.DESCRIPTION
This script closes all running Outlook processes. At first it does so in a "nice way".
It tries to close the Outlook Window as if the user shuts down Outlook. There are some
situations when this procedure will be unsuccesfull and then this script becomes impatient.
So after these attempts it will just kill all running Outlook processes using stop-process.
This is why you should not use this script on a terminal server environment!
To start this script just run it.
.EXAMPLE
Close-Outlook
PS H:\Mijn Documenten> H:\Buroblad\close-outlook.ps1
Script starts with user: 9321038
Found 1 process(es):
System.Diagnostics.Process (OUTLOOK)
Outlook is still open...
Pressing ENTER key
False
Outlook is still open...
Pressing ENTER key
False
Outlook is still open...
Pressing ENTER key
False
WARNING: Using Stop-Process now!
WARNING: Using Stop-Process now!
No Outlook processes are running anymore
Script ends normally.
.NOTES
Author: WPS department Vivat verzekeringen + Robert van Reems
Date: 25-11-2015
Title: Exchange & Lync consultant
Version: 1.2 added handtekening folder check and write its content to the log
1.3 Fixed Handtekeninen folder to check the owner of the Outlook process
Added environment variables to the log.
replaced mechanism to stop Outlook with force
1.4 Removed enviroment variables from log
Added other method to find owner of process
Added a some process information to log
#>
<# **********************************************************************************************************************
DISCLAIMER
THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE
************************************************************************************************************************* #>
$LogFileName = $MyInvocation.MyCommand.Name + ".log"
$LogPath = "C:\logs"
$LogFile = $LogPath + "\" + $LogFileName
$CIMOutlookProcesses = @() #array of WMI outlook processes, they will be added later.
$UserName = @() #array hold the username(s) of those who have Outlook processes open.
$isOutlookOpen = @() #contains the Outlook processes
$MaxNiceTries = 10 # The maximum number of closing Outlook before killing the process
function Write-Log{
param(
[parameter(mandatory=$true,position=0,ValueFromPipeLine=$true)][string[]]$Message,
[parameter(mandatory=$false)][switch]$Error,
[parameter(mandatory=$false)][switch]$FatalError,
[parameter(mandatory=$false)][switch]$Warning
)
foreach($line in $Message){
if($Warning){
write-warning $line
$line = "**** WARNING ****:$line"
}
elseif($Error){
Write-Error $line
$line = "**** ERROR ****:$line"
}
elseif($FatalError){
Write-Error $line
$line = "**** FATAL ERROR **** ($(get-date)):$line"
}
else{
Write-Host $line
$line = "**** INFO ****:($(get-date))$line"
}
try{
Add-Content "$script:LogFile" $line -ErrorAction Stop
}Catch{
throw "Fatal error writing to logfile. Details: $_"
}
}
}
if(Test-Path $LogFile){
Write-Verbose "Removing old logfile"
try{
Remove-Item $LogFile
}catch{
throw "Failed to remove the logfile with error $_"
}
}
Write-Log -Message "Script version 1.4"
Write-Log -Message "Script starts with user: $($env:USERNAME)"
Write-Log -Warning "Do not run this script on a Terminal server environment! All Outlook processess will be stopped!"
#Put the Outlook processes in $isOutlookOpen
try{
$isOutlookOpen = @()
$isOutlookOpen += Get-Process outlook* -ErrorAction Stop
}catch{
throw "Failed to find outlook processes"
}
#For some checks we need to obtain the username of the Outlook process owner
try{
Write-Log -Message "Trying to get the Outlook processes using WMI"
$CIMOutlookProcesses += Get-CimInstance Win32_Process -Property * -Filter "name = 'outlook.exe'"
foreach($proces in $CIMOutlookProcesses){
$UserName += $(Invoke-CimMethod -InputObject $proces -MethodName GetOwner | select User).User
}
Write-Log -Message "Succeded"
}catch{
Write-Log -Error -Message "Failed to get outlook WMI processes, these are needed to get the username."
}
#add the process information to the log
Write-Log "Process information:"
foreach($proces in $CIMOutlookProcesses){
Write-Log -Message ("Name" + " = " + $proces.Name)
Write-Log -Message ("CreationDate" + " = " + $proces.CreationDate)
Write-Log -Message ("CSName" + " = " + $proces.CSName)
}
#Check if the path still exists and log the contents
#the Handtekeningen path is only known when an Outlook process is found because it's path is constructed using the Environment variables
if($isOutlookOpen.Length -gt 0){
#$HandtekeningPath = "C:\Users\"+$isOutlookOpen[0].StartInfo.EnvironmentVariables.Item('USERNAME')+"\AppData\Roaming\Microsoft\Handtekeningen"
$HandtekeningPath = "C:\Users\$UserName\AppData\Roaming\Microsoft\Handtekeningen"
if(Test-Path $HandtekeningPath){
Write-Log -Message "Content of $HandtekeningPath"
Write-Log -Message $(Get-ChildItem $HandtekeningPath)
}else{
Write-Log -Warning "Handtekeningen path was not found: $HandtekeningPath"
}
}else{
Write-Log -Warning "Could not determine the handtekeningen path."
}
if($isOutlookOpen.length -gt 0){
Write-Log -message "Found $($isOutlookOpen.length) process(es): "
Write-Log $isOutlookOpen
# while loop makes sure all outlook windows are closed before moving on to other code:
while($isOutlookOpen.length -gt 0){
#try to close Outlook
try{
Get-Process outlook* | ForEach-Object {$_.CloseMainWindow() | Out-Null }
}catch {
Write-Log -Error "Failed to close Outlook with error $_"
}
sleep 5
#If it's still open try to press enter to close and safe any new messages
$isOutlookOpen = @()
If(($isOutlookOpen += Get-Process outlook*) -ne $null){
Write-Log -Message "Outlook is still open..."
#try to press "ENTER"
try{
Write-Log "Pressing ENTER key"
$wshell = new-object -com wscript.shell
$wshell.AppActivate("Microsoft Outlook") | Out-Null
$wshell.Sendkeys("{ENTER}") | Out-Null
}catch{
Write-Log -Error "Failed to press the ENTER key with error $_"
}
#Check Current status
}
$MaxNiceTries--
#Stop trying after # attempts and just kill it!
if($MaxNiceTries -le 0){
Write-Log -Warning "The script was not able to close Outlook nicely and will kill the processes now"
foreach($proces in $isOutlookOpen){
try{
$proces.Kill()
Write-Log "Succesfully killed a process"
}catch{
Write-Log -Error "Failed to close outlook process with error $_"
exit
}
}
break
}
$isOutlookOpen =@()
$isOutlookOpen += Get-Process outlook*
}
}else{
Write-Log -Message "Found NO Outlook processes"
}
Write-Log "Script ends normally."
以上是关于powershell PowerShell:关闭Outlook的主要内容,如果未能解决你的问题,请参考以下文章
powershell PowerShell:关闭Outlook
powershell [Manipulandousuários通过PowerShell执行Office 365] Comandos para manipular o Office 365.
powershell 能取到关闭的虚拟机内存、容量、cpu等信息吗