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 2020-11-11 18:27  momingliu11  阅读(726)  评论(0编辑  收藏  举报