表格中的空白条目-不接受,继续-Powershell

时间:2018-06-23 17:58:16

标签: powershell

我的脚本检查AD来查找在表单中输入的用户。如果找到,则接受,拒绝,然后再次询问是否。我发现,如果该字段为空,它将对其进行处理。我创建了一个新选项,不接受.length -eq 0,然后提示并再次询问。它继续处理脚本,并再次提示在AD中找不到用户帐户。我需要脚本不发出第二个提示,而是要求一个新条目。

function CreateXLS {

function ProcessOut ($x , $group) {
            $result = @()

            Foreach ($Line in $x){
                            $GroupName = "domain.local\" + $group
                            $OutList = New-Object System.Object
                            $OutList | Add-Member -type NoteProperty -Name "DisplayPath_GroupName" -value $GroupName
                            $OutList | Add-Member -type NoteProperty -Name "RuleName" -value "AutomaticApproval"

                            $result+= $OutList

  }
            #Output to XLS
            $outputfilepath = 'c:\users\admin\desktop\'
            $outputfilename = $outputfilepath + 'bulkupload.csv'
          $result | export-csv $outputfilename  -Append -encoding unicode -NoTypeInformation

  } 


 function PromptInput {
            add-Type -AssemblyName System.Windows.Forms
            Add-Type -AssemblyName System.Drawing



            $form = New-Object System.Windows.Forms.Form
            $form.Text = 'Group Auto-Approval Setup'
            $form.Size = New-Object System.Drawing.Size(500,230)
            $form.StartPosition = 'CenterScreen'

            $OKButton = New-Object System.Windows.Forms.Button
            $OKButton.Location = New-Object System.Drawing.Point(170,100)
            $OKButton.Size = New-Object System.Drawing.Size(75,23)
            $OKButton.Text = 'OK'
            $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
            $form.AcceptButton = $OKButton
            $form.Controls.Add($OKButton)

            $CancelButton = New-Object System.Windows.Forms.Button
            $CancelButton.Location = New-Object System.Drawing.Point(260,100)
            $CancelButton.Size = New-Object System.Drawing.Size(75,23)
            $CancelButton.Text = 'Cancel'
            $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
            $form.CancelButton = $CancelButton    
            $form.Controls.Add($CancelButton) 




            $label = New-Object System.Windows.Forms.Label
            $label.Location = New-Object System.Drawing.Point(200,40)
            $label.Size = New-Object System.Drawing.Size(280,20)
            $label.Text = "Enter a group name:"
            $form.Controls.Add($label)

            $textBox = New-Object System.Windows.Forms.TextBox
            $textBox.Location = New-Object System.Drawing.Point(100,65)
            $textBox.Size = New-Object System.Drawing.Size(300,120)
            $form.Controls.Add($textBox)

            $form.Topmost = $true

            $form.Add_Shown({$textBox.Select()})
            $result = $form.ShowDialog()


      #Message box results: blank, found group, group doesn't exist.
           if($textbox.text -eq 0){[System.Windows.Forms.MessageBox]::OK("Entry Cannot Be Blank, Continue?") , "Status" , 4)

           if ($result -eq 'Cancel'){
                            Exit
            }

            if ($result -eq [System.Windows.Forms.DialogResult]::OK){
                            $x = $textBox.Text
            }



            return $x


}






Add-Type -AssemblyName microsoft.visualbasic
$continue = $true
while($continue){
            $input = PromptInput


            $searcher = [ADSISearcher]"(SAMAccountName=$input)"
            $result = $searcher.FindOne()



            if($result){
                            ProcessOut $result $input
                            $additional = [System.Windows.Forms.MessageBox]::Show("Would you like to enter another group?" , "Status" , 4)
                            if ($additional -eq "NO"){
                                             createxls}
                            }}

1 个答案:

答案 0 :(得分:0)

$textbox.text -eq 0区域中我发现了一些问题。 $textbox.text -eq 0的检查将检查文本内容而不是长度。没有静态方法[System.Windows.Forms.MessageBox]::OK(...。假设它应该是[System.Windows.Forms.MessageBox]::Show(...,那么语法会有点偏离。 }在if块的末尾似乎丢失了。

在脚本的主要部分中,您将$input用作变量之一。这是一个自动变量,应避免将其用作变量之一(请参见link)。 continuereserved word,因此您可以考虑不要将其用作变量名。即使在创建输出之后,您在这里似乎也有一个连续的循环。

用此片段替换代码的底部应执行您想要的操作。它会循环供用户输入,直到他们取消或选择不继续。这是主要变化。请注意ShowDialog()在此循环内移动。另外请注意变量名称的更改,并在脚本的主要部分中添加了$continue = $false

    # The rest of your script above stays the same

    $form.Add_Shown({$textBox.Select()})
    #$result = $form.ShowDialog() # Moved to inside do..while $promptAgain loop

    #Message box results: blank, found group, group doesn't exist.
    $promptAgain = 'Yes'
    do {
        if ($promptAgain -eq 'Yes') {
            $result = $form.ShowDialog()
            $promptAgain = 'No'
        }
        if ($result -eq 'Cancel'){
            Exit
        }
        if ($textbox.text.Length -eq 0) {
            $promptAgain = [System.Windows.Forms.MessageBox]::Show("Entry Cannot Be Blank, Continue?" , "Status" , 4)
            if ($promptAgain -eq 'No'){
                Exit
            }
        }
    } while ($promptAgain -eq 'Yes')
    if ($result -eq [System.Windows.Forms.DialogResult]::OK){
        $x = $textBox.Text
    }
    return $x
} # function PromptInput

Add-Type -AssemblyName microsoft.visualbasic
$continue = $true
while($continue) {
    $userInput = PromptInput

    $searcher = [ADSISearcher]"(SAMAccountName=$userInput)"
    $result = $searcher.FindOne()

    if($result){
        ProcessOut $result $userInput
        $additional = [System.Windows.Forms.MessageBox]::Show("Would you like to enter another group?" , "Status" , 4)
        if ($additional -eq "NO"){
            createxls
            $continue = $false # added to be able to exit while($continue)
        }
    }
}