Сценарий Powershell, выполняющий оба пакетных файла, когда выбран только один элемент списка - PullRequest
0 голосов
/ 16 июня 2020

Первый l oop правильно подхватывает все сервера. Я считаю, что разбивка связана с вложенным l oop, который доставляет $ listBox.SelectedItems. Я выбрал только OPENFIRE, но он также остановил JBOSS. Я изучаю PowerShell, поэтому приветствую любую помощь, которая поможет мне понять, почему это происходит.


Add-Type -AssemblyName System.Windows.Forms
# Main Form
$mainForm = New-Object System.Windows.Forms.Form
$font = New-Object System.Drawing.Font(“Consolas”, 13)
$mainForm.Text = ”KRONOS SERVICES MANAGER”
$mainForm.Font = $font
$mainForm.ForeColor = “White”
$mainForm.BackColor = “LightBlue”
$mainForm.Width = 755
$mainForm.Height = 380

# Stop-Start Label
$minTimePickerLabel = New-Object System.Windows.Forms.Label
$minTimePickerLabel.Text = “Select to Stop or Start Service(s)”
$minTimePickerLabel.Location = “10, 5”
$minTimePickerLabel.Height = 22
$minTimePickerLabel.Width = 290
$mainForm.Controls.Add($minTimePickerLabel)

$Button1 = New-Object system.Windows.Forms.Button
$Button1.text = "STOP"
$Button1.width = 80
$Button1.height = 30
$Button1.location = New-Object System.Drawing.Point(100, 32)
$Button1.Font = 'Microsoft Sans Serif,10'
$MainForm.AcceptButton = $Button1
$MainForm.Controls.Add($Button1)

$Button2 = New-Object system.Windows.Forms.Button
$Button2.text = "START"
$Button2.width = 80
$Button2.height = 30
$Button2.location = New-Object System.Drawing.Point(100, 67)
$Button2.Font = 'Microsoft Sans Serif,10'
$MainForm.AcceptButton = $Button2
$MainForm.Controls.Add($Button2)

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(385,50)
$OKButton.Size = New-Object System.Drawing.Size(95,23)
$OKButton.Text = 'OK'
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$MainForm.AcceptButton = $OKButton
$MainForm.Controls.Add($OKButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(500,50)
$CancelButton.Size = New-Object System.Drawing.Size(95,23)
$CancelButton.Text = 'Cancel'
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$MainForm.CancelButton = $CancelButton
$MainForm.Controls.Add($CancelButton)

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,160)
$label.Size = New-Object System.Drawing.Size(590,20)
$label.Text = 'Select Kronos Services:'
$MainForm.Controls.Add($label)

### InputBox
$label2 = New-Object System.Windows.Forms.Label
$label2.Location = New-Object System.Drawing.Point(10,95)
$label2.Size = New-Object System.Drawing.Size(700,20)
$label2.Text = 'Enter server name(s) separated by space or comma below:'
$MainForm.Controls.Add($label2)

$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,120)
$textBox.Size = New-Object System.Drawing.Size(710,20)
$MainForm.Controls.Add($textBox)

$listBox = New-Object System.Windows.Forms.Listbox
$listBox.Location = New-Object System.Drawing.Point(10,190)
$listBox.Size = New-Object System.Drawing.Size(710,20)
$listBox.SelectionMode = 'MultiExtended'

[void] $listBox.Items.Add('JBOSS')
[void] $listBox.Items.Add('OPENFIRE')

$listBox.Height = 110
$MainForm.Controls.Add($listBox)
$MainForm.Toplevel = $true
$MainForm.Topmost = $true
$Button1.add_Click({
    $Button1.Enabled = $false
    $Button1.Font = 'Microsoft Sans Serif,10,style=Bold'
    $global:Action = "stop"
    Write-Host $global:Action
    })
$Button2.add_Click({
    $Button2.Enabled = $false
    $Button2.Font = 'Microsoft Sans Serif,10,style=Bold'
    $global:Action = "start"
    Write-Host $global:Action
    })

$result = $mainForm.ShowDialog()
    if ($result -eq [System.Windows.Forms.DialogResult]::OK)
      {


#########################################

[string[]] $_ServerList= @()
$_ServerList = $TextBox.Text.Split(',').Split(' ')

#loop through all computers entered by user to retrieve Kronos Task status - export to CSV file
foreach ($Computer in $_ServerList) {
    [Microsoft.VisualBasic.Interaction]::MsgBox("Processing $Computer ...", "MsgBoxSetForeground,Information", "Processing $Computer ...")
    $ErrorActionPreference = 'SilentlyContinue'
    Write-Host $global:Action
    ##Nested Loop to be created #####
     $tempList = $listBox.SelectedItems

        foreach ($taskname in $tempList) {

        Set-Variable -Name Service -Value $taskname -Option AllScope

        #Write-Host $MyTest
        $Go = $Global:Service
        Write-Host $Go
        if($Go -eq "OPENFIRE")  {

                        $arguments = "$Action >>ns_bat.log 2>>&1"

                        invoke-command -computername $Computer -Credential $Credentials {param($arguments) Start-Process -FilePath "D:\Kronos\openfire\ns\bin\ns.bat" -ArgumentList $arguments -Verb RunAs} -ArgumentList $arguments
                        Start-Sleep 5
                       }else{
                        $arguments = "$Action >>wfc_bat.log 2>>&1"
                        invoke-command -computername $Computer -Credential $Credentials {param($arguments) Start-Process -FilePath "D:\Kronos\wfc\bin\wfc.bat" -ArgumentList $arguments -Verb RunAs} -ArgumentList $arguments
                        Start-Sleep 5
                       }
    }
    ##End of Nested Loop##
  }

 }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...