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

 

上一篇:bilibiliclass45_C语言_求Sn=a+aa+aaa+aaaa+aaaaa的前5项之和,其中a是一个数字V2.0


下一篇:Mongoose索引、Mongoose内置CURD方法、扩展Mongoose Model的静态方法和实例方法