本帖最后由 skyruanboy 于 2023-3-3 14:04 编辑
最近换了一家外企,软件安全审核严格。因为powershell是系统自带的,直接用powershell开发能避免很多问题。powershell有完整的基础语法,要完成各项任务是没有问题的,就是网上的文档太少。
这次完成的是一个邮件批量发送。其中要保存邮件中的格式,并且要能替换里面的名字及密码等关键词,最后批量发送给每个人。
这里邮箱模板用的是outlook的msg,然后批量用的excel表格。
虽然用powershell,但是还是放不下UI,就简单设计了一下。文档太少,自己研究有局限性,如果有可以改进的地方,欢迎指正。
[PowerShell] 纯文本查看 复制代码 function gui-label {
param (
$x = 10, $y = 10, $text = "请输入:"
)
$Label = New-Object System.Windows.Forms.Label
$Label.Location = New-Object System.Drawing.Size($x, $y)
$Label.Size = New-Object System.Drawing.Size(180, 20)
$Label.Text = $text
return $Label
}
function gui-textbox {
param (
$x = 10, $y = 10
)
$TextBox = New-Object System.Windows.Forms.TextBox
$TextBox.Location = New-Object System.Drawing.Size($x, $y)
$TextBox.Size = New-Object System.Drawing.Size(150, 50)
return $TextBox
}
function OutlookSend_accountInformation {
param (
$PathToMsg = "",
$userName = "none",
$Mail1 = "none",
$Mail2 = "none",
$CC = "",
$modify1 = "none",
$modify2 = "none"
)
$outlook = New-Object -comObject Outlook.Application
$session = $outlook.Session
$session.Logon()
$mail = $outlook.Session.OpenSharedItem($PathToMsg)
$mail.HTMLBody = $mail.HTMLBody -replace "modify1", $modify1
$mail.HTMLBody = $mail.HTMLBody -replace "modify2", $modify2
$mail.Forward()
$mail.Recipients.Add($Mail1)
$mail.Recipients.Add($Mail2)
$mail.CC = $CC
$mail.send()
}
function g-win {
Add-Type -AssemblyName system.windows.forms
$main_form = New-Object System.Windows.Forms.Form
$main_form.Text = 'skyruan-outlook'
$main_form.Width = 400
$main_form.Height = 300
$main_form.AutoSize = $true
$Richtextbox = New-Object System.Windows.Forms.richtextbox
$Richtextbox.Location = New-Object System.Drawing.Point(10, 200)
$Richtextbox.Size = New-Object System.Drawing.Size(380, 20)
$Richtextbox.Height = 280
$main_form.Controls.Add($Richtextbox)
$main_form.Controls.Add((gui-label 10 12 "收件箱1"))
$TextBoxTo = (gui-textbox 220 10)
$main_form.Controls.Add($TextBoxTo)
$main_form.Controls.Add((gui-label 10 42 "收件箱2"))
$TextBoxCC1 = (gui-textbox 220 40)
$main_form.Controls.Add($TextBoxCC1)
$main_form.Controls.Add((gui-label 10 72 "抄送"))
$TextBoxCC2 = (gui-textbox 220 70)
$main_form.Controls.Add($TextBoxCC2)
$main_form.Controls.Add((gui-label 10 102 "修改1"))
$TextBoxName = (gui-textbox 220 100)
$main_form.Controls.Add($TextBoxName)
$main_form.Controls.Add((gui-label 10 132 "修改2"))
$TextBoxDate = (gui-textbox 220 130)
$main_form.Controls.Add($TextBoxDate)
$ButtonExcel = New-Object System.Windows.Forms.Button
$ButtonExcel.Location = New-Object System.Drawing.Size(60, 160)
$ButtonExcel.Size = New-Object System.Drawing.Size(120, 30)
$ButtonExcel.Text = "导入"
$main_form.Controls.Add($ButtonExcel)
$ButtonExcel.Add_Click(
{
$ExcelObj = New-Object -comobject Excel.Application
$ExcelObj.visible = $true
$ExcelWorkBook = $ExcelObj.Workbooks.Open("C:\1\1.xlsx")
$ExcelWorkSheet = $ExcelWorkBook.Sheets.Item("1")
#判断有多少行数据
for ($i = 1; $i -lt 100; $i++) {
if ($ExcelWorkSheet.Range("A" + $i).Text -eq "") {
$number = $i
break
}
else {}
}
for ($j = 1; $j -lt $number; $j++) {
$Mail1 = $ExcelWorkSheet.Range("A" + $j).Text
$Mail2 = $ExcelWorkSheet.Range("B" + $j).Text
$CC = $ExcelWorkSheet.Range("C" + $j).Text
$modify1 = $ExcelWorkSheet.Range("D" + $j).Text
$modify2 = $ExcelWorkSheet.Range("E" + $j).Text
OutlookSend_accountInformation "C:\1\1.msg" $Mail1 $Mail2 $CC $modify1 $modify2
$Richtextbox.AppendText("发送完毕" + "`n")
}
}
)
$ButtonSend = New-Object System.Windows.Forms.Button
$ButtonSend.Location = New-Object System.Drawing.Size(220, 160)
$ButtonSend.Size = New-Object System.Drawing.Size(120, 30)
$ButtonSend.Text = "发送"
$main_form.AcceptButton = $ButtonSend
$main_form.Controls.Add($ButtonSend)
$ButtonSend.Add_Click(
{
#逻辑和批量发送一样,这里就不填这么多了,这是为了方便只有一条数据的时候,不需要通过excel操作
}
)
$main_form.ShowDialog()
}
|