Gui's and Aray imput

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Brian Bourque Brian Bourque 1 year, 10 months ago.

  • Author
    Posts
  • #26935

    I am Trying to make a Simple Gui for a script I wrote for others on my Team.
    long story shot the code goes stops the service, deletes the files then restarts the service.
    Here is the Code

    # Test Error 49 Toolbox
     
    #region Boring beginning stuff
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
     
    #endregion 
    #region begin to draw forms
     
    $Form = New-Object System.Windows.Forms.Form
    $Form.Text = "Error49 Clearing Tool"
    $Form.Size = New-Object System.Drawing.Size(300,170)
    $Form.StartPosition = "CenterScreen"
    $Form.KeyPreview = $True
    $Form.MaximumSize = $Form.Size
    $Form.MinimumSize = $Form.Size
     
    $label = New-Object System.Windows.Forms.label
    $label.Location = New-Object System.Drawing.Size(5,5)
    $label.Size = New-Object System.Drawing.Size(240,30)
    $label.Text = "Type in the results of the BTI print Job scan from the MS Toolbox"
    $Form.Controls.Add($label)
    $textbox = New-Object System.Windows.Forms.TextBox
    $textbox.Location = New-Object System.Drawing.Size(5,40)
    $textbox.Size = New-Object System.Drawing.Size(120,20)
     
    #$textbox.Text = "Select source PC:"
    $Form.Controls.Add($textbox) 
    $Scan_computer_click =
    {
     
    #region Actual Code
     
    $statusBar1.Text = "Clearing Print Jobs..."
    [String[]]$ComputerName = $textbox.Text 
     
    #Beging ForEach Loop
    foreach ($Computer in $Computername)
    {
      
            Invoke-Command -computername $Computer -ScriptBlock {Stop-Service -Displayname "Citrix Print Manager Service"}
            Invoke-Command -computername $Computer -ScriptBlock {Stop-Service -Name spooler -force}
            Remove-Item -Path \\$Computer\c$\Windows\System32\spool\PRINTERS\* -recurse  
            Invoke-Command -computername $Computer -ScriptBlock {Start-Service -Displayname "Citrix Print Manager Service"}
            Invoke-Command -computername $Computer -ScriptBlock {Start-Service -Name spooler}
            Get-Service -Computername $Computer -Displayname "Citrix Print Manager Service", "Print Spooler" | Select name,status,$Computer | sort $Computer | Out-GridView
    } #End For each Loop
     
     
    $statusBar1.Text = "Print Jobs Cleared"
    #endregion
    }
     
    $OKButton = New-Object System.Windows.Forms.Button
    $OKButton.Location = New-Object System.Drawing.Size(140,38)
    $OKButton.Size = New-Object System.Drawing.Size(75,23)
    $OKButton.Text = "OK"
    $OKButton.Add_Click($Scan_computer_click)
    $Form.Controls.Add($OKButton) 
    $result_label = New-Object System.Windows.Forms.label
    $result_label.Location = New-Object System.Drawing.Size(5,65)
    $result_label.Size = New-Object System.Drawing.Size(240,30)
    $result_label.Text = $OutPut
    $Form.Controls.Add($result_label) 
    $statusBar1 = New-Object System.Windows.Forms.StatusBar
    $statusBar1.Name = "statusBar1"
    $statusBar1.Text = "Ready..."
    $form.Controls.Add($statusBar1)
    $Form.Add_KeyDown({if ($_.KeyCode -eq "Enter"){& $Scan_computer_click}})
    $Form.Add_KeyDown({if ($_.KeyCode -eq "Escape")
    {$Form.Close()}})
    #endregion begin to draw forms 
    #Show form
    $Form.Topmost = $True
    $Form.Add_Shown({$Form.Activate()})
    [void] $Form.ShowDialog()
    

    The above code works Just fine for a Single hostname, but when trying to input more then one at a time I get the following error

    Invoke-Command : One or more computer names is not valid. If you are trying to pass a Uri, use the -ConnectionUri parameter or pass Uri objects instead of strings.
    At C:\Scripts\Building\Test49Box_AlphaTest.ps1:41 char:23
    + Invoke-Command <<<< -computername $Computer -ScriptBlock {Stop-Service -Displayname "Citrix Print Manager Service"} + CategoryInfo : InvalidArgument: (System.String[]:String[]) [Invoke-Command], ArgumentException + FullyQualifiedErrorId : PSSessionInvalidComputerName,Microsoft.PowerShell.Commands.InvokeCommandCommand I know the Code that runs the Foreach loop works as i have it working already in a different script, and adding [String[]] in front of computername did not seem to help either. is this an issue with Powershell or with the Forms them self ? update: I tried to add in [String[]]$Computername = [String[]]$Textbox.txt and it now seems to read the string, only now it reads it as a Single Stringinstead of an separate entries in an array.

  • #26936
    Profile photo of Don Jones
    Don Jones
    Keymaster

    How is it you're entering multiple computer names into the textbox?

    If it's "Computer1,Computer2,Computer3," that's your problem. That isn't an array, it's a single string that contains two commas.

    [String]$ComputerNames = $textbox.Text 
    $ComputerName = $ComputerNames -Split ","
    
  • #26937

    Thank you Don,
    a few Type-o's later and I finally understand what you mean.
    Thanks again.

You must be logged in to reply to this topic.