PowerShell导入自定义公共函数

编写公共函数,然后将其保存为D: empSend.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:	empSend.psm1
Import-Module #可以看到导入的函数模块
SendWechatMsg $touser $data
原文地址:https://www.cnblogs.com/dreamer-fish/p/13960092.html