我有一个长脚本,为用户提供提示:输入票号(暂时注释掉),输入电子邮件地址(也注释)并从一系列4个复选框中选择。这个想法是一个新人可以访问4个不同位置的任何组合。 2个位置由1个管理员处理,另2个由单个管理员处理(IE,A& B由管理员A处理,C由管理员B处理,D由C管理)。因此,我编写了一个长脚本,其中包含每个位置组合的代码片段,没有重复。因此,您可以选择A,AB,AC,AD,ABC,ABD等等。
如果我选择的代码只是在选择A,B,C或D时发送电子邮件,那么一切都很有效。当我包含代码时,比如选择A& B,脚本无法正常工作。
示例:我选择包含对建筑物A的访问权限。发送的电子邮件说用户需要访问A. 另一个例子:我选择包含对建筑物B的访问。发送的电子邮件说用户需要访问B. 问题的最后一个例子:我选择包含对建筑物A和B的访问。我得到一个提示,包括对A的访问以及包含对A和B的访问的提示,但是我收到2封电子邮件以包含对A的访问权限。我应该得到1个提示和1个电子邮件,同时说明对A和B的访问。
为了简洁起见,除了代码A和A& A之外,我已经修剪了很多代码以删除所有组合。 B.如果您需要我提供更多内容,请告诉我。
# initialize the script
if ($startupvariables) { try {Remove-Variable -Name startupvariables -Scope Global -ErrorAction SilentlyContinue } catch { } }
New-Variable -force -name startupVariables -value ( Get-Variable | ForEach-Object { $_.Name } )
# make sure the cmd window is not closed
Write-Output "Don't close this window!"
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
$Separator = ".", "@"
# advise what the script does
Add-Type -AssemblyName PresentationCore,PresentationFramework
$ButtonType = [System.Windows.MessageBoxButton]::OKCancel
$MessageIcon = [System.Windows.MessageBoxImage]::Warning
$MessageTitle = "Building Access - US Corp Locations"
$MessageBody = "This script sends the building admins for the US corporate locations an email advising them that a user needs access to their location.`n`nTo use it, enter the below information:`n`n`n`tSCTask Ticket Number`n`n`tUser's Email Address`n`n`tBuilding Access Requested`n`n`nIf this is the script you want to use, click OK.`nIf not, click Cancel."
$Result = [System.Windows.MessageBox]::Show($MessageBody,$MessageTitle,$ButtonType,$MessageIcon)
if ($Result -eq "Cancel")
{
Exit-PSSession
}
else
{
# get the ticket number
###################
# $Ticket = [Microsoft.VisualBasic.Interaction]::InputBox("Enter the SCTask ticket number" , "Ticket Number")
$Ticket = 1234567890
###################
# get the user id via the email address
###################
# $UserID = [Microsoft.VisualBasic.Interaction]::InputBox("Enter the user's email address" , "User Email Address")
$UserID = "xxxxxxxxxxx.com"
###################
$User = $UserID.split($Separator)
$Firstname = $User[0].substring(0,1).toupper()+$User[0].substring(1).tolower()
$Lastname = $User[1].substring(0,1).toupper()+$User[1].substring(1).tolower()
$User = $Firstname, $Lastname
# get the local username
$Username = [System.Environment]::UserName
# provide checkboxes for locations requested
function Location_Access{
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$Form = New-Object System.Windows.Forms.Form
$Form.width = 415
$Form.height = 235
$Form.Text = ”Select Building Locations"
$Font = New-Object System.Drawing.Font("Verdana",11)
$Form.Font = $Font
$MyGroupBox = New-Object System.Windows.Forms.GroupBox
$MyGroupBox.Location = '5,5'
$MyGroupBox.size = '385,135'
$Checkbox1 = New-Object System.Windows.Forms.Checkbox
$Checkbox1.Location = '20,20'
$Checkbox1.size = '360,25'
$Checkbox1.Checked = $false
$Checkbox1.Text = "World Headquarters (Maumee, OH)"
$CB1 = "World Headquarters"
$Checkbox2 = New-Object System.Windows.Forms.Checkbox
$Checkbox2.Location = '20,45'
$Checkbox2.size = '360,25'
$Checkbox2.Checked = $false
$Checkbox2.Text = "Maumee Technology Center (Maumee, OH)"
$CB2 = "Maumee Technology Center"
$Checkbox3 = New-Object System.Windows.Forms.Checkbox
$Checkbox3.Location = '20,70'
$Checkbox3.size = '360,25'
$Checkbox3.Checked = $false
$Checkbox3.Text = "Global IT Center/Longbow (Maumee, OH)"
$CB3 = "Global IT Center/Longbow"
$Checkbox4 = New-Object System.Windows.Forms.Checkbox
$Checkbox4.Location = '20,95'
$Checkbox4.size = '360,25'
$Checkbox4.Checked = $false
$Checkbox4.Text = "Novi (Novi, MI)"
$CB4 = "Novi"
$OKButton = new-object System.Windows.Forms.Button
$OKButton.Location = '10,150'
$OKButton.Size = '90,35'
$OKButton.Text = 'OK'
$OKButton.DialogResult=[System.Windows.Forms.DialogResult]::OK
$CancelButton = new-object System.Windows.Forms.Button
$CancelButton.Location = '110,150'
$CancelButton.Size = '90,35'
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$CancelButton.DialogResult=[System.Windows.Forms.DialogResult]::Cancel
$form.Controls.AddRange(@($MyGroupBox,$OKButton,$CancelButton))
$MyGroupBox.Controls.AddRange(@($Checkbox1,$Checkbox2,$Checkbox3,$Checkbox4))
$form.AcceptButton = $OKButton
$form.CancelButton = $CancelButton
$form.Add_Shown({$form.Activate()})
$dialogResult = $form.ShowDialog()
if ($DialogResult -eq "OK")
{
# if WHQ is selected
if ($Checkbox1.Checked)
{
$subject = "Ticket $Ticket - $User's Building Access Request"
$body = @"
<html>
<body style="font-family:calibri">
Team,<br>
<br>
<br>
Per ticket $Ticket, $User is requesting building access to:<br>
<br>
<br>
<b><u>$CB1</b></u><br>
<br>
<br>
<br>
Thank You,<br>
<br>
IT Service Desk<br>
<br>
------------------------------------------------------------------ <br>
IT Service Desk Operation Information <br>
------------------------------------------------------------------ <br>
Hours of Operation : 24 hours a day<br>
xxxxxxxxxxxxxxxx
</body>
</html>
"@
}
# create confirmation message
$ButtonType = [System.Windows.MessageBoxButton]::YesNo
$MessageIcon = [System.Windows.MessageBoxImage]::Warning
$MessageTitle = "Corporate Building Access Request"
$MessageBody = "The information you have entered is show below:`n`n`nTicket Number: $Ticket`n`nUser's Email Address: $UserID`n`nAccess To: $CB1`n`n`nIf you would like to send the email, click Yes.`nOtherwise, click No."
$Result = [System.Windows.MessageBox]::Show($MessageBody,$MessageTitle,$ButtonType,$MessageIcon)
if ($Result -eq "No")
{
Exit-PSSession
}
else
# send email
{
Send-MailMessage -To "<$Username@xxxx.com>" -from "<itservicedesk@xxxx.com>" -Subject $subject -SmtpServer "mailrelay.xxxx.com" -BodyAsHtml -body $body
}
# if WHQ & MTC is selected
elseif ($Checkbox1.Checked -And $Checkbox2.Checked)
{
$subject = "Ticket $Ticket - $User's Building Access Request"
$body = @"
<html>
<body style="font-family:calibri">
Team,<br>
<br>
<br>
Per ticket $Ticket, $User is requesting building access to:<br>
<br>
<br>
<b><u>$CB1 & $CB2</b></u><br>
<br>
<br>
<br>
Thank You,<br>
<br>
IT Service Desk<br>
<br>
------------------------------------------------------------------ <br>
IT Service Desk Operation Information <br>
------------------------------------------------------------------ <br>
Hours of Operation : 24 hours a day<br>
xxxxxxxxx
</body>
</html>
"@
}
# create confirmation message
$ButtonType = [System.Windows.MessageBoxButton]::YesNo
$MessageIcon = [System.Windows.MessageBoxImage]::Warning
$MessageTitle = "Corporate Building Access Request"
$MessageBody = "The information you have entered is show below:`n`n`nTicket Number: $Ticket`n`nUser's Email Address: $UserID`n`nAccess To: $CB1 & $CB2`n`n`nIf you would like to send the email, click Yes.`nOtherwise, click No."
$Result = [System.Windows.MessageBox]::Show($MessageBody,$MessageTitle,$ButtonType,$MessageIcon)
if ($Result -eq "No")
{
Exit-PSSession
}
else
# send email
{
Send-MailMessage -To "<$Username@xxxx.com>" -from "<itservicedesk@xxxx.com>" -Subject $subject -SmtpServer "mailrelay.xxxx.com" -BodyAsHtml -body $body
}
}
elseif ($DialogResult -eq "Cancel")
{
break
}
}
Location_Access
}
Function Clean-Memory {
Get-Variable |
Where-Object { $startupVariables -notcontains $_.Name } |
ForEach-Object {
try { Remove-Variable -Name "$($_.Name)" -Force -Scope "global" -ErrorAction SilentlyContinue -WarningAction SilentlyContinue}
catch { }
}
}
我不应该得到中间提示,只有第二个提示选择两个位置。每个提示都包含一封用于建筑物访问的电子邮件。但是,我收到了2封电子邮件,用于访问第一栋楼,也没有提到第二栋楼。
我知道这很奇怪,可能看起来很复杂。请告诉我您可能需要的其他信息。如果需要,我可以包含完整的代码;只是想减少尽可能多的额外费用。
更新:运行脚本时出错
You cannot call a method on a null-valued expression.
At line:109 char:42
+ $MyGroupBox.Controls | % {if($_.Checked){[Void]$TextTotal.AppendLine($_.Text);[V ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At line:142 char:154
+ ... `nAccess To: $($TextTotal.ToString())`n`n`nIf you would like to send the email, ...
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
答案 0 :(得分:1)
我认为您需要根据检查的元素构建最终消息。 在表单showdialog调用之后添加这段代码( $ dialogResult = $ form.ShowDialog())
$CheckedBoxes = New-Object System.Text.StringBuilder
$CheckedBoxesForEmail = New-Object System.Text.StringBuilder
$MyGroupBox.Controls | % {if($_.Checked){[Void]$TextTotal.AppendLine($_.Text);[Void]$CheckedBoxesForEmail.AppendLine('<b><u>{0}</b></u><br>' -f $_.Text)}}
然后为每个复选框丢弃所有那些不必要的IF语句。我已将变量 $ CB1 更改为此 $($ CheckedBoxesForEmail.ToString())以及此 $($ TextTotal.ToString())。那些将带有你想要的所有文字。
$CheckedBoxes = New-Object System.Text.StringBuilder
$CheckedBoxesForEmail = New-Object System.Text.StringBuilder
$MyGroupBox.Controls | % {if($_.Checked){[Void]$TextTotal.AppendLine($_.Text);[Void]$CheckedBoxesForEmail.AppendLine('<b><u>{0}</b></u><br>' -f $_.Text)}}
if ($DialogResult -eq "OK")
{
$subject = "Ticket $Ticket - $User's Building Access Request"
$body = @"
<html>
<body style="font-family:calibri">
Team,<br>
<br>
<br>
Per ticket $Ticket, $User is requesting building access to:<br>
<br>
<br>$($CheckedBoxesForEmail.ToString())<br>
<br>
<br>
Thank You,<br>
<br>
IT Service Desk<br>
<br>
------------------------------------------------------------------ <br>
IT Service Desk Operation Information <br>
------------------------------------------------------------------ <br>
Hours of Operation : 24 hours a day<br>
xxxxxxxxxxxxxxxx
</body>
</html>
"@
}
# create confirmation message
$ButtonType = [System.Windows.MessageBoxButton]::YesNo
$MessageIcon = [System.Windows.MessageBoxImage]::Warning
$MessageTitle = "Corporate Building Access Request"
$MessageBody = "The information you have entered is show below:`n`n`nTicket Number: $Ticket`n`nUser's Email Address: $UserID`n`nAccess To: $($TextTotal.ToString())`n`n`nIf you would like to send the email, click Yes.`nOtherwise, click No."
$Result = [System.Windows.MessageBox]::Show($MessageBody,$MessageTitle,$ButtonType,$MessageIcon)