随笔 - 911  文章 - 5  评论 - 94  阅读 - 243万

PowerShell导入自定义公共函数

编写公共函数,然后将其保存为D:\temp\Send.psm1,脚本内容如下:

复制代码
Function SendMsg($touser,$data){
    $url='http://msg.xx.com/rmsg'
    $key = 'Mj111'   
    $secret = 'b3228'   
    $today = Get-Date -uformat "%YY-%M-%D"
    $snstr = "key=$key&content=$data&touser=$touser$secret"
    $md5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
    $utf8 = New-Object -TypeName System.Text.UTF8Encoding
    $sn = [System.BitConverter]::ToString($md5.ComputeHash($utf8.GetBytes($snstr))).replace('-','').ToLower()
    #$textmod="key=$key&batch=1&content=$data&touser=$touser&sn=$sn"
    $textmod = @{key=$key;content=$data;touser=$touser;sn=$sn}
    Invoke-WebRequest -UseBasicParsing $url -Method POST -Body $textmod  |Out-Null
    #Invoke-WebRequest -UseBasicParsing $url -ContentType 'application/x-www-form-urlencoded;charset=UTF-8' -Method POST -Body $textmod  
}
复制代码

 注:该psm1脚本中可以包含多个函数,导入后都可以直接调用,可以通过get-module查看到

 

在另外脚本中先导入psm1文件,然后即可调用SendMsg函数,如下:

Import-Module D:\temp\Send.psm1
Import-Module #可以看到导入的函数模块
SendWechatMsg $touser $data

 

###########################

定义公共函数,用来发送告警邮件

1.定义发送邮件公共函数,并将其保存为名为PublicModule_Custom.psm1的文件

复制代码
###########################脚本功能#####################
#该脚本用于保存公共函数,可被其他脚本导入使用
########################################################

#定义函数,对凭据加密
Function GetCred()
    {
    $RemoteUserName = "domain\s-app-user"
    $RemotePassword = 'password'
    $RemotePassword_sec = ConvertTo-SecureString $RemotePassword -AsPlainText –Force
    $cred = New-Object System.Management.Automation.PSCredential($RemoteUserName,$RemotePassword_sec)
    return $cred
    }


#定义函数,发送邮件
Function Sendmail($MailtoAddress,$Subject,$Body,$BCC=$null)
{
#定义邮件服务器
    $mailDomain = "domain.com"
    $smtpServer = "10.0.1.1"
    $smtpUser = "MailSendUser"
    $smtpPassword = "mailpassword"
    $smtpPassword_sec = ConvertTo-SecureString $smtpPassword -AsPlainText –Force
    $cred = New-Object System.Management.Automation.PSCredential($smtpUser,$smtpPassword_sec)
    $from = $smtpUser + "@" + $mailDomain 

    #if (!($MailtoAddress.Contains("@")))
    #    { $MailtoAddress + "@" + $mailDomain  }

    if ($BCC -ne $null)
        { Send-MailMessage -SmtpServer $smtpServer -From $from -To $MailtoAddress  -Bcc $BCC -Port 587 -Subject $Subject -Credential $cred -Body $Body -Encoding default }
    else { Send-MailMessage -SmtpServer $smtpServer -From $from -To $MailtoAddress -Port 587 -Subject $Subject -Credential $cred -Body $Body -Encoding default }

}
复制代码

2.在其他脚本中引用以上公共脚本,用来发送邮件

复制代码
#定义公共模块位置,导入发信功能
$current_path = $MyInvocation.MyCommand.Path.substring(0,$MyInvocation.MyCommand.Path.LastIndexOf('\')+1)
#$current_parentPath = Split-Path $current_path
#$current_path = "D:\Operations\Scripts"
$module_public = Join-Path  $current_path "PublicModule_Custom.psm1"
Import-Module $module_public

[array]$SystemMailAdmin = "recipient@domain.com"$Subject = "Mail Alert"
$Body = “Mail Content”
Sendmail $SystemMailAdmin $Subject $Body

复制代码

 

posted on   momingliu11  阅读(798)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2014-11-11 收集计算机分区信息,去除列中的重复值(Excel)(空行)
2013-11-11 内存--工作集
2013-11-11 Windows常用性能计数器总结
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示