locked
Trying to pass input from form to powershell and copy file. RRS feed

  • Question

  • I am trying to capture an IP address that any user wants to input. Once they put it in and click the OK button "accept", then it is returned to powershell and used to copy from a server to the IP address that was inputted.

    #Add needed Assemblies

    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 
    [void] [System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")

    function Gather-Input{
    $form = New-Object System.Windows.Forms.Form
    $form.Text = 'TWIST Update by IP'
    $form.Size = New-Object System.Drawing.Size(300,200)
    $form.StartPosition = 'CenterScreen'
    $form.KeyPreview = $True

    $OKButton = New-Object System.Windows.Forms.Button
    $OKButton.Location = New-Object System.Drawing.Point(75,120)
    $OKButton.Size = New-Object System.Drawing.Size(75,23)
    $OKButton.Text = 'Update'
    $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(150,120)
    $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(10,20)
    $label.Size = New-Object System.Drawing.Size(280,20)
    $label.Text = 'Please enter the IP address you want to update below:'
    $form.Controls.Add($label)

    $textBox1 = New-Object System.Windows.Forms.TextBox
    $textBox1.Location = New-Object System.Drawing.Point(60,50)
    $textBox1.Size = New-Object System.Drawing.Size(30, 20)
    $textBox1.TabIndex = 1
    $textBox1.TabStop
    $textBox1.MaxLength = 3
    $textBox1.Add_TextChanged({
        $this.Text = $this.Text -replace '\D'
    })
    $form.Controls.Add($textBox1)

    $textBox2 = New-Object System.Windows.Forms.TextBox
    $textBox2.Location = New-Object System.Drawing.Point(100,50)
    $textBox2.Size = New-Object System.Drawing.Size(30,20)
    $textBox2.TabIndex = 2
    $textBox2.TabStop
    $textBox2.MaxLength = 3
    $textBox2.Add_TextChanged({
        $this.Text = $this.Text -replace '\D'
    })
    $form.Controls.Add($textBox2)

    $textBox3 = New-Object System.Windows.Forms.TextBox
    $textBox3.Location = New-Object System.Drawing.Point(140,50)
    $textBox3.Size = New-Object System.Drawing.Size(30,20)
    $textBox3.MaxLength = 3
    $textBox3.TabIndex = 4
    $textBox3.TabStop
    $textBox3.Add_TextChanged({
        $this.Text = $this.Text -replace '\D'
    })
    $form.Controls.Add($textBox3)

    $textBox4 = New-Object System.Windows.Forms.TextBox
    $textBox4.Location = New-Object System.Drawing.Point(180,50)
    $textBox4.Size = New-Object System.Drawing.Size(30,20)
    $textBox4.MaxLength = 3
    $textBox4.TabIndex = 4
    $textBox4.TabStop
    $textBox4.Add_TextChanged({
        $this.Text = $this.Text -replace '\D'
    })
    $form.Controls.Add($textBox4)

    $form.Topmost = $True
    $form.Add_Shown({$form.Activate(); $textBox1.focus()})

    $dialogResult = $form.ShowDialog()

    return $textBox1.Text, $textBox2.Text, $textBox3.Text, $textBox4.Text
    }


    $return = Gather-Input 

    $return[0]
    $return[1]
    $return[2]
    $return[3]

    # Remote folder path
    $Destination = "C$\SomeFolder\"

    # Local folder path
    $Source = "C:\AnotherFolder\*"

    $IP = $return[0].$return[1].$return[2].$return[3]

    Copy-Item -Recurse $Source -Destination \\$IP\$destination -Exclude *.ini -Force –Verbose

    Wednesday, May 30, 2018 1:22 AM

All replies

  • First fix you post and place the code in  the code posting tool provided on the edit bar.


    \_(ツ)_/

    Wednesday, May 30, 2018 1:46 AM
  • I think this is what you are trying to do …

    function Gather-Input{
        
        Add-Type -AssemblyName System.Windows.Forms 
        $form = New-Object System.Windows.Forms.Form
        $form.Text = 'TWIST Update by IP'
        $form.Size = '300,200'
        $form.StartPosition = 'CenterScreen'
    
        $OKButton = New-Object System.Windows.Forms.Button
        $OKButton.Location = '75,120'
        $OKButton.Size = '75,23'
        $OKButton.Text = 'Update'
        $OKButton.DialogResult = 'OK'
        $form.AcceptButton = $OKButton
        [void]$form.Controls.Add($OKButton)
    
        $CancelButton = New-Object System.Windows.Forms.Button
        $CancelButton.Location = '150,120'
        $CancelButton.Size = '75,23'
        $CancelButton.Text = 'Cancel'
        $CancelButton.DialogResult = 'Cancel'
        $form.CancelButton = $CancelButton
        [void]$form.Controls.Add($CancelButton)
    
        $label = New-Object System.Windows.Forms.Label
        $label.Location = '10,20'
        $label.Size = '280,20'
        $label.Text = 'Please enter the IP address you want to update below:'
        [void]$form.Controls.Add($label)
    
        $textBox1 = New-Object System.Windows.Forms.TextBox
        $textBox1.Location = '60,50'
        $textBox1.Size = '30, 20'
        $textBox1.TabIndex = 1
        $textBox1.MaxLength = 3
        $textBox1.Add_TextChanged({$this.Text = $this.Text -replace '\D'})
        [void]$form.Controls.Add($textBox1)
    
        $textBox2 = New-Object System.Windows.Forms.TextBox
        $textBox2.Location = '100,50'
        $textBox2.Size = '30,20'
        $textBox2.TabIndex = 2
        $textBox2.MaxLength = 3
        $textBox2.Add_TextChanged({$this.Text = $this.Text -replace '\D'})
        [void]$form.Controls.Add($textBox2)
    
        $textBox3 = New-Object System.Windows.Forms.TextBox
        $textBox3.Location = '140,50'
        $textBox3.Size = '30,20'
        $textBox3.MaxLength = 3
        $textBox3.TabIndex = 3
        $textBox3.Add_TextChanged({$this.Text = $this.Text -replace '\D'})
        [void]$form.Controls.Add($textBox3)
    
        $textBox4 = New-Object System.Windows.Forms.TextBox
        $textBox4.Location ='180,50'
        $textBox4.Size = '30,20'
        $textBox4.MaxLength = 3
        $textBox4.TabIndex = 4
        $textBox4.Add_TextChanged({$this.Text = $this.Text -replace '\D'})
        $form.Controls.Add($textBox4)
    
        $form.Topmost = $True
        $form.Add_Shown({$form.Activate(); $textBox1.focus()})
    
        if('Ok' -eq $form.ShowDialog()){
            '{0}.{1}.{2}.{3}' -f $textBox1.Text, $textBox2.Text, $textBox3.Text, $textBox4.Text
        }else{
    Write-Warning 'IP selection aborted'
    } } $ip = Gather-Input Copy-Item $Source -Destination "\\$IP\C$\SomeFolder" -Exclude *.ini -Recurse –Verbose


    \_(ツ)_/


    • Edited by jrv Wednesday, May 30, 2018 2:28 AM
    • Proposed as answer by j0rt3g4 Wednesday, May 30, 2018 5:23 AM
    Wednesday, May 30, 2018 2:26 AM
  • I got a XAML answer, try it :)

    [CmdletBinding()]
    param()
    
    #load required dll for xaml
    [void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
    
    
    # Remote folder path
    $global:Destination = "C$\SomeFolder" #not required to end in /
    # Local folder path
    $global:Source = "C:\AnotherFolder" #no star in here
    
    [xml]$XAML = @'
    <Window 
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:WpfApp1"
            Title="TWIST Update by IP" Height="150" Width="300">
        <Grid>
            <Button x:Name="BUpdate" Content="_Update" HorizontalAlignment="Left" Margin="10,84,0,0" VerticalAlignment="Top" Width="131"/>
            <Button x:Name="BCancel" Content="_Cancel" HorizontalAlignment="Left" Margin="152,84,0,0" VerticalAlignment="Top" Width="132"/>
            <TextBox x:Name="A1" HorizontalAlignment="Left" Height="25" Margin="24,41,0,0" TextWrapping="Wrap" Text="000" VerticalAlignment="Top" Width="50" MaxLength="3" FontSize="16" TextAlignment="Center"/>
            <TextBox x:Name="A2" HorizontalAlignment="Left" Height="25" Margin="88,41,0,0" TextWrapping="Wrap" Text="000" VerticalAlignment="Top" Width="50" MaxLength="3" FontSize="16" TextAlignment="Center"/>
            <TextBox x:Name="A3" HorizontalAlignment="Left" Height="25" Margin="152,41,0,0" TextWrapping="Wrap" Text="000" VerticalAlignment="Top" Width="50" MaxLength="3" FontSize="16" TextAlignment="Center"/>
            <TextBox x:Name="A4" HorizontalAlignment="Left" Height="25" Margin="216,41,0,0" TextWrapping="Wrap" Text="000" VerticalAlignment="Top" Width="50" MaxLength="3" FontSize="16" TextAlignment="Center"/>
            <Label Content="." HorizontalAlignment="Left" Margin="73,41,0,0" VerticalAlignment="Top" FontSize="18"/>
            <Label Content="." HorizontalAlignment="Left" Margin="138,41,0,0" VerticalAlignment="Top" FontSize="18"/>
            <Label Content="." HorizontalAlignment="Left" Margin="202,41,0,0" VerticalAlignment="Top" FontSize="18"/>
            <Label Content="Please enter the IP to update below:" HorizontalAlignment="Left" Margin="24,9,0,0" VerticalAlignment="Top"/>
    
        </Grid>
    </Window>
    
    '@
    function Do-Copy{
        [Cmdletbinding()]
        param(
            [Parameter(mandatory=$true,position=0)]$a1,
            [Parameter(mandatory=$true,position=1)]$a2,
            [Parameter(mandatory=$true,position=2)]$a3,
            [Parameter(mandatory=$true,position=3)]$a4
        )
        begin{
            $ip ="$($a1.Text).$($a2.Text).$($a3.Text).$($a4.Text)"
        }
        process{
    
            # Remote folder path
            try{
                Copy-Item -Recurse $global:Source -Destination \\$IP\$Global:destination -Exclude *.ini -Force –Verbose
            }
            catch{
                Write-Error "On Do-Copy Folder with message: $($_.Exception.Message)"
            }
        }
        end{
            write-host -ForegroundColor Green "Finished"
        }
    }
    
    #Read XAML
    $reader=(New-Object System.Xml.XmlNodeReader $XAML )
    try{
    #$Form=[Windows.Markup.XamlReader]::Load( $reader )
    $Window=[Windows.Markup.XamlReader]::Load(  $reader ) 
    
    
    #Connect to Controls 
        #store Form objects
        $xaml.SelectNodes("//*[@*[contains(translate(name(.),'n','N'),'Name')]]")  | ForEach {
        New-Variable  -Name $_.Name -Value $Window.FindName($_.Name) -Force
    
        #add events to form objects
        $BCancel.Add_Click({$window.Close()})
        $BUpdate.Add_Click({Do-Copy $a1 $a2 $a3 $a4})
      }
        $Window.ShowDialog() | out-null 
    
    
    }
    catch{Write-Host "Unable to load Windows.Markup.XamlReader.`nMessage: $($_.Exception.Message)`n Some possible causes for this problem include: .NET Framework is missing PowerShell must be launched with PowerShell -sta`nInvalid XAML code was encountered."; exit}
    

    • Proposed as answer by j0rt3g4 Wednesday, May 30, 2018 5:23 AM
    Wednesday, May 30, 2018 5:22 AM
  • Sorry about that... this was my first post.

    I did run your script changes but for some reason I am getting the below error... Any ideas why?

    VERBOSE: Performing the operation "Copy Directory" on target "Item: \\192.168.0.250\c$\FOLDER1 Destination: \\192.168.0.3\C:\FOLDER1".
    VERBOSE: Performing the operation "Create Directory" on target "Destination: \\192.168.0.3\C:\FOLDER1".
    Copy-Item : The given path's format is not supported.
    At C:\WorkSpace\DEV\Untitled3.ps1:82 char:1
    + Copy-Item \\192.168.0.250\c$\FOLDER1  -Destination "\\$IP\C:\FOLDER1 ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [Copy-Item], NotSupportedException
        + FullyQualifiedErrorId : System.NotSupportedException,Microsoft.PowerShell.Commands.CopyItemCommand
     

    Wednesday, May 30, 2018 4:22 PM
  • Sorry about that... this was my first post.

    Who's script did you run?

    The following is the issue.  "\\192.168.0.3\C:\FOLDER1".

    It should be: "\\192.168.0.3\C$\FOLDER1".


    \_(ツ)_/

    Wednesday, May 30, 2018 4:29 PM
  • The non xml version. I couldn't get that one to run without errors.

    Wouldn't read the xml code.

    And thank you... the $ was what I needed. 

    Wednesday, May 30, 2018 4:42 PM
  • THis is what I posted:

    Copy-Item $Source -Destination "\\$IP\C$\SomeFolder" -Exclude *.ini -Recurse Verbose

    Note that "destination is correct so you must have changed it.  Note it has "C$" and not "C:"

    Fix the destination  syntax and the error will go away.


    \_(ツ)_/

    Wednesday, May 30, 2018 4:51 PM
  • Jum what errors are you getting with my version?
    Friday, June 1, 2018 2:27 AM
  • No errors any longer. But I do have a weird behavior. When I try to close out using the form close button or the cancel button I must hit it twice in order for it to close. Any reason why I would be experiencing this? 

    #Add needed Assemblies
    
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 
    [void] [System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")
    
    function Gather-Input{
        
        Add-Type -AssemblyName System.Windows.Forms 
        $form = New-Object System.Windows.Forms.Form
        $form.Text = 'Update by IP'
        $form.Size = '300,200'
        $form.StartPosition = 'CenterScreen'
    
    
        $OKButton = New-Object System.Windows.Forms.Button
        $OKButton.Location = '75,120'
        $OKButton.Size = '75,23'
        $OKButton.Text = 'Update'
        $OKButton.DialogResult = 'OK'
        $form.AcceptButton = $OKButton
        [void]$form.Controls.Add($OKButton)
    
        $CancelButton = New-Object System.Windows.Forms.Button
        $CancelButton.Location = '150,120'
        $CancelButton.Size = '75,23'
        $CancelButton.Text = 'Cancel'
        $CancelButton.DialogResult = 'Cancel'
        $form.CancelButton = $CancelButton
        [void]$form.Controls.Add($CancelButton)
    
        $label1 = New-Object System.Windows.Forms.Label
        $label1.Location = '10,20'
        $label1.Size = '280,20'
        $label1.Text = 'Please enter the IP address you want to update below:'
        [void]$form.Controls.Add($label1)
    
        $label2 = New-Object System.Windows.Forms.Label
        $label2.Location = '92,68'
        $label2.Size = '7,12'
        $label2.Text = '*'
        [void]$form.Controls.Add($label2)
    
        $label3 = New-Object System.Windows.Forms.Label
        $label3.Location = '132,68'
        $label3.Size = '7,12'
        $label3.Text = '*'
        [void]$form.Controls.Add($label3)
    
        $label4 = New-Object System.Windows.Forms.Label
        $label4.Location = '172,68'
        $label4.Size = '7,12'
        $label4.Text = '*'
        [void]$form.Controls.Add($label4)
    
        $textBox1 = New-Object System.Windows.Forms.TextBox
        $textBox1.Location = '60,50'
        $textBox1.Size = '30,20'
        $textBox1.TabIndex = 1
        $textBox1.MaxLength = 3
        $textBox1.Add_TextChanged({$this.Text = $this.Text -replace '\D'})
        [void]$form.Controls.Add($textBox1)
    
        $textBox2 = New-Object System.Windows.Forms.TextBox
        $textBox2.Location = '100,50'
        $textBox2.Size = '30,20'
        $textBox2.TabIndex = 2
        $textBox2.MaxLength = 3
        $textBox2.Add_TextChanged({$this.Text = $this.Text -replace '\D'})
        [void]$form.Controls.Add($textBox2)
    
        $textBox3 = New-Object System.Windows.Forms.TextBox
        $textBox3.Location = '140,50'
        $textBox3.Size = '30,20'
        $textBox3.MaxLength = 3
        $textBox3.TabIndex = 3
        $textBox3.Add_TextChanged({$this.Text = $this.Text -replace '\D'})
        [void]$form.Controls.Add($textBox3)
    
        $textBox4 = New-Object System.Windows.Forms.TextBox
        $textBox4.Location ='180,50'
        $textBox4.Size = '30,20'
        $textBox4.MaxLength = 3
        $textBox4.TabIndex = 4
        $textBox4.Add_TextChanged({$this.Text = $this.Text -replace '\D'})
        $form.Controls.Add($textBox4)
    
        $Icon = New-Object system.drawing.icon ("\\192.168.0.2\c$\Somefolder.ico")
        $Form.Icon = $Icon
    
        $form.Topmost = $True
        $form.Add_Shown({$form.Activate(); $textBox1.focus()})
    
        if('Ok' -eq $form.ShowDialog()){
            '{0}.{1}.{2}.{3}' -f $textBox1.Text, $textBox2.Text, $textBox3.Text, $textBox4.Text
        }
        else{
             Write-Warning 'IP selection aborted'
        }
    }
    
    
    
    $ip = Gather-Input 
    
    
    Get-WmiObject -ComputerName $IP -Class win32_process -Filter "name='notepad.exe'" | ForEach { $_.Terminate() }
    Copy-Item -Recurse \\192.168.0.2\c$\Somefolder -Destination "\\$IP\C$\Anotherfolder" -Force –Verbose

    Friday, June 1, 2018 12:48 PM
  • THese lines are unnecessary.

    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    [void] [System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")

    You only need this line:

     Add-Type -AssemblyName System.Windows.Forms

    The rest is either unused or loaded by this command.


    \_(ツ)_/

    Friday, June 1, 2018 1:46 PM
  • This works correctly and closes with no issues.  Do not change it before testing.  Add no code and run in new console.

    function Gather-Input{
        
        Add-Type -AssemblyName System.Windows.Forms
        
        $form = New-Object System.Windows.Forms.Form
        $form.Text = 'Update by IP'
        $form.Size = '300,200'
        $form.StartPosition = 'CenterScreen'
        $Form.Icon = New-Object system.Drawing.Icon('\\192.168.0.2\c$\Somefolder.ico')
    
    
    
        $OKButton = New-Object System.Windows.Forms.Button
        $OKButton.Location = '75,120'
        $OKButton.Size = '75,23'
        $OKButton.Text = 'Update'
        $OKButton.DialogResult = 'OK'
        $form.AcceptButton = $OKButton
        [void]$form.Controls.Add($OKButton)
    
        $CancelButton = New-Object System.Windows.Forms.Button
        $CancelButton.Location = '150,120'
        $CancelButton.Size = '75,23'
        $CancelButton.Text = 'Cancel'
        $CancelButton.DialogResult = 'Cancel'
        $form.CancelButton = $CancelButton
        [void]$form.Controls.Add($CancelButton)
    
        $label1 = New-Object System.Windows.Forms.Label
        $label1.Location = '10,20'
        $label1.Size = '280,20'
        $label1.Text = 'Please enter the IP address you want to update below:'
        [void]$form.Controls.Add($label1)
    
        $label2 = New-Object System.Windows.Forms.Label
        $label2.Location = '92,68'
        $label2.Size = '7,12'
        $label2.Text = '*'
        [void]$form.Controls.Add($label2)
    
        $label3 = New-Object System.Windows.Forms.Label
        $label3.Location = '132,68'
        $label3.Size = '7,12'
        $label3.Text = '*'
        [void]$form.Controls.Add($label3)
    
        $label4 = New-Object System.Windows.Forms.Label
        $label4.Location = '172,68'
        $label4.Size = '7,12'
        $label4.Text = '*'
        [void]$form.Controls.Add($label4)
    
        $textBox1 = New-Object System.Windows.Forms.TextBox
        $textBox1.Location = '60,50'
        $textBox1.Size = '30,20'
        $textBox1.TabIndex = 1
        $textBox1.MaxLength = 3
        $textBox1.Add_TextChanged({$this.Text = $this.Text -replace '\D'})
        [void]$form.Controls.Add($textBox1)
    
        $textBox2 = New-Object System.Windows.Forms.TextBox
        $textBox2.Location = '100,50'
        $textBox2.Size = '30,20'
        $textBox2.TabIndex = 2
        $textBox2.MaxLength = 3
        $textBox2.Add_TextChanged({$this.Text = $this.Text -replace '\D'})
        [void]$form.Controls.Add($textBox2)
    
        $textBox3 = New-Object System.Windows.Forms.TextBox
        $textBox3.Location = '140,50'
        $textBox3.Size = '30,20'
        $textBox3.MaxLength = 3
        $textBox3.TabIndex = 3
        $textBox3.Add_TextChanged({$this.Text = $this.Text -replace '\D'})
        [void]$form.Controls.Add($textBox3)
    
        $textBox4 = New-Object System.Windows.Forms.TextBox
        $textBox4.Location ='180,50'
        $textBox4.Size = '30,20'
        $textBox4.MaxLength = 3
        $textBox4.TabIndex = 4
        $textBox4.Add_TextChanged({$this.Text = $this.Text -replace '\D'})
        $form.Controls.Add($textBox4)
    
        $form.Topmost = $True
        $form.Add_Shown({$form.Activate(); $textBox1.focus()})
    
        if('Ok' -eq $form.ShowDialog()){
            '{0}.{1}.{2}.{3}' -f $textBox1.Text, $textBox2.Text, $textBox3.Text, $textBox4.Text
        }
        else{
             Write-Warning 'IP selection aborted'
        }
    }
    
    $ip = Gather-Input 


    \_(ツ)_/


    • Edited by jrv Friday, June 1, 2018 1:54 PM
    Friday, June 1, 2018 1:53 PM