< Back

Invoke-CommandWithEmailWrapper

Sat Jan 18, 2020 8:16 pm

NAME Invoke-CommandWithEmailWrapper



SYNOPSIS

Executes a Script or ScriptBlock via Invoke-Command, and provide email alerts either before or before and after

execution.





SYNTAX

Invoke-CommandWithEmailWrapper [-ComputerName <String>] [-Script <String>] -JobName <String> -SmtpServer <String>

[-EmailMode <String>] [-SmtpPort <Int32>] [-EmailFrom <String>] -EmailTo <String[]> [-EmailUseSsl]

[<CommonParameters>]



Invoke-CommandWithEmailWrapper [-ComputerName <String>] [-ScriptBlock <ScriptBlock>] -JobName <String> -SmtpServer

<String> [-EmailMode <String>] [-SmtpPort <Int32>] [-EmailFrom <String>] -EmailTo <String[]> [-EmailUseSsl]

[<CommonParameters>]





DESCRIPTION

Executes a Script or ScriptBlock either locally on on a remote computer. Provides output of Script/ScriptBlock

via email after execution completes. Optionally sends an additional email alert at the start of execution.





PARAMETERS

-ComputerName <String>

Computer to execute the command on. Defaults to localhost.



Required? false

Position? named

Default value

Accept pipeline input? false

Accept wildcard characters? false



-Script <String>

Script to execute.



Required? false

Position? named

Default value

Accept pipeline input? false

Accept wildcard characters? false



-ScriptBlock <ScriptBlock>

ScriptBlock to execute.



Required? false

Position? named

Default value

Accept pipeline input? false

Accept wildcard characters? false



-JobName <String>

A short job name to include in emails to identify this execution.



Required? true

Position? named

Default value

Accept pipeline input? false

Accept wildcard characters? false



-SmtpServer <String>

Specifies SMTP server used to send email



Required? true

Position? named

Default value

Accept pipeline input? false

Accept wildcard characters? false



-EmailMode <String>

Specifies to send mail either After or BeforeAndAfter command execution. Defaults to After.



Required? false

Position? named

Default value After

Accept pipeline input? false

Accept wildcard characters? false



-SmtpPort <Int32>

TCP Port to connect to SMTP server on. Defaults to 25.



Required? false

Position? named

Default value 25

Accept pipeline input? false

Accept wildcard characters? false



-EmailFrom <String>

Specifies a source address for messages. Defaults to computername@domain



Required? false

Position? named

Default value "$($env:computername)@$($env:userdnsdomain)"

Accept pipeline input? false

Accept wildcard characters? false



-EmailTo <String[]>

Specifies a comma-separated (i.e. "a@b.com","b@b.com") list of email addresses to email upon job completion



Required? true

Position? named

Default value

Accept pipeline input? false

Accept wildcard characters? false



-EmailUseSsl [<SwitchParameter>]

Indicates that the cmdlet uses the Secure Sockets Layer (SSL) protocol to establish a connection to the remote

computer to send mail. Defaults to $true



Required? false

Position? named

Default value True

Accept pipeline input? false

Accept wildcard characters? false



<CommonParameters>

This cmdlet supports the common parameters: Verbose, Debug,

ErrorAction, ErrorVariable, WarningAction, WarningVariable,

OutBuffer, PipelineVariable, and OutVariable. For more information, see

about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).



INPUTS

No inputs





OUTPUTS

Outputs whatever the Script/ScriptBlock you are invoking outputs.





-------------------------- EXAMPLE 1 --------------------------



PS C:\\>Invoke-CommandWithEmailWrapper -ScriptBlock { robocopy c:\\source d:\\dest } -JobName "RoboCopy" -SmtpServer

"smtp01" -EmailTo "admin@contoso.com"



Executes the robocopy command in the ScriptBlock on the local computer, then sends an email with the command's

output once it completes.









-------------------------- EXAMPLE 2 --------------------------



PS C:\\>Invoke-CommandWithEmailWrapper -Script "c:\\scripts\\script1.ps1" -JobName "Script1" -SmtpServer "smtp01"

-EmailTo "admin@contoso.com" -ComputerName "serv01" -EmailMode "BeforeAndAfter"



Executes the the script c:\\scripts\\script1.ps1 (on the local computer) on the remote computer "serv01", sending

emails when the script begins and finishes running.











RELATED LINKS

https://github.com/natescherer/PoshEmail