We help IT Professionals succeed at work.

Cannot upload files via Powershell GUI

Kasper Katzmann
on
65 Views
Last Modified: 2018-10-18
I have a Powershell script that opens a GUI where I am supposed to upload a file from.

The script looks like this:
$Button = 'system.Windows.Forms.Button'
$SanSerif10Bold = 'Microsoft Sans Serif,10,style=Bold'
$sanSerif10 = 'Microsoft Sans Serif,10'
$SysDrawPoint = 'System.Drawing.Point'
# InitialDirectory not defined in submitted code
$InitialDirectory = $env:USERPROFILE #dummy initial directory
#filepath missing from submitted Code
$FilePath = $env:USERPROFILE #dummy Filepath

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

#region begin GUI{ 

$PuzzelFileUpload                = New-Object -TypeName system.Windows.Forms.Form
$PuzzelFileUpload.ClientSize     = '400,124'
$PuzzelFileUpload.text           = 'Puzzel File Uploader'
$PuzzelFileUpload.TopMost        = $false

$txtFileName                     = New-Object -TypeName system.Windows.Forms.TextBox
$txtFileName.multiline           = $false
$txtFileName.width               = 280
$txtFileName.height              = 20
$txtFileName.location            = New-Object -TypeName $SysDrawPoint -ArgumentList (107,39)
$txtFileName.Font                = $sanSerif10

$btnFileBrowser                  = New-Object -TypeName $Button
$btnFileBrowser.BackColor        = '#858585'
$btnFileBrowser.text             = 'BROWSE'
$btnFileBrowser.width            = 79
$btnFileBrowser.height           = 38
$btnFileBrowser.location         = New-Object -TypeName $SysDrawPoint -ArgumentList (16,25)
$btnFileBrowser.Font             = $SanSerif10Bold
$btnFileBrowser.ForeColor        = '#ffffff'

$lblFileName                     = New-Object -TypeName system.Windows.Forms.Label
$lblFileName.text                = 'Filnavn'
$lblFileName.AutoSize            = $true
$lblFileName.width               = 25
$lblFileName.height              = 10
$lblFileName.location            = New-Object -TypeName $SysDrawPoint -ArgumentList (110,21)
$lblFileName.Font                = $sanSerif10

$btnUpload                       = New-Object -TypeName $Button
$btnUpload.BackColor             = '#7ed321'
$btnUpload.text                  = 'Upload'
$btnUpload.width                 = 96
$btnUpload.height                = 30
$btnUpload.location              = New-Object -TypeName $SysDrawPoint -ArgumentList (291,78)
$btnUpload.Font                  = $SanSerif10Bold
$btnUpload.ForeColor             = '#000000'

$PuzzelFileUpload.controls.AddRange(@($txtFileName,$btnFileBrowser,$lblFileName,$btnUpload))

#region gui events {
$btnFileBrowser.Add_Click({
Add-Type -AssemblyName System.windows.forms | Out-Null
    
$OpenFileDialog = New-Object -TypeName System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $initialDirectory
$OpenFileDialog.filter = 'CSV (*.csv)| *.csv'
$OpenFileDialog.ShowDialog() | Out-Null
$filePath = $OpenFileDialog.filename

$txtFileName.Text = $filePath 
$form.Refresh()
})
$btnUpload.Add_Click({
$destPath = '\\server1.my.domain.dk\Puzzel\'

Copy-Item -Path $filePath -Destination $destPath
})
#endregion events }

#endregion GUI }


#Write your logic code here

$null = $PuzzelFileUpload.ShowDialog()

Open in new window


But... when I try to upload a file, strange things happen.
First I get this error message:
You cannot call a method on a null-valued expression.
At line:66 char:1
+ $form.Refresh()
+ ~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Open in new window


And when I press Upload, a folder is created with my username, instead of the file I have selected.
The selected file is chosen from my Documents folder.

Any idea what's going on and why?
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
ste5anSenior Developer
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Kasper KatzmannSeniorkonsulent

Author

Commented:
Thank you both for clarifying. This is my first attempt to make a powershell gui, so any help and claryfication is appriciated.

@oBda
Your changes did the job, but as with so many other aspects in life, one answer rises two new questions :-)
I have tried to add a simple error handling, where I check if the source and destination files have the same hash.
When the form is loaded, I dont want to show any text in $lblResult, but after I have submitted the file, I want to show one of the messages, depending on the result of the hash-check. But nothing happens.

With my limited understanding of the GUI concept, I would guess that I could put the refresh part in where i did, but...
$sourceHash = Get-FileHash $FilePath
    $destHash   = Get-FileHash "$destPath\Test.csv"

        if($sourceHash -eq $destHash)
        {
            $lblResult.text       = "Filen er nu overført til $FilePath"
            $lblResult.ForeColor  = '#009933'
	         $PuzzelFileUpload.Refresh()
        }
        ELSE
        {
            $lblResult.text       = "Filen er IKKE nu overført"
            $lblResult.ForeColor  = '#ff0000'
	         $PuzzelFileUpload.Refresh()
        }

Open in new window

Complete script here:
$Button = 'system.Windows.Forms.Button'
$SanSerif10Bold = 'Microsoft Sans Serif,10,style=Bold'
$sanSerif10 = 'Microsoft Sans Serif,10'
$sanSerif8 = 'Microsoft Sans Serif,8'
$SysDrawPoint = 'System.Drawing.Point'
# InitialDirectory not defined in submitted code
$InitialDirectory = $env:USERPROFILE #dummy initial directory

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

#region begin GUI{ 

$PuzzelFileUpload                = New-Object -TypeName system.Windows.Forms.Form
$PuzzelFileUpload.ClientSize     = '400,124'
$PuzzelFileUpload.text           = 'Puzzel File Uploader'
$PuzzelFileUpload.TopMost        = $false

$txtFileName                     = New-Object -TypeName system.Windows.Forms.TextBox
$txtFileName.multiline           = $false
$txtFileName.width               = 280
$txtFileName.height              = 20
$txtFileName.location            = New-Object -TypeName $SysDrawPoint -ArgumentList (107,39)
$txtFileName.Font                = $sanSerif10

$btnFileBrowser                  = New-Object -TypeName $Button
$btnFileBrowser.BackColor        = '#858585'
$btnFileBrowser.text             = 'BROWSE'
$btnFileBrowser.width            = 79
$btnFileBrowser.height           = 38
$btnFileBrowser.location         = New-Object -TypeName $SysDrawPoint -ArgumentList (16,25)
$btnFileBrowser.Font             = $SanSerif10Bold
$btnFileBrowser.ForeColor        = '#ffffff'

$lblFileName                     = New-Object -TypeName system.Windows.Forms.Label
$lblFileName.text                = 'Filnavn'
$lblFileName.AutoSize            = $true
$lblFileName.width               = 25
$lblFileName.height              = 10
$lblFileName.location            = New-Object -TypeName $SysDrawPoint -ArgumentList (110,21)
$lblFileName.Font                = $sanSerif10

$lblResult                       = New-Object -TypeName system.Windows.Forms.Label
$lblResult.text                  = ''
$lblResult.width                 = 270
$lblResult.height                = 30
$lblResult.location              = New-Object -TypeName $SysDrawPoint -ArgumentList (10,78)
$lblResult.Font                  = $SanSerif8
$lblResult.ForeColor             = ''

$btnUpload                       = New-Object -TypeName $Button
$btnUpload.BackColor             = '#7ed321'
$btnUpload.text                  = 'Upload'
$btnUpload.width                 = 96
$btnUpload.height                = 30
$btnUpload.location              = New-Object -TypeName $SysDrawPoint -ArgumentList (291,78)
$btnUpload.Font                  = $SanSerif10Bold
$btnUpload.ForeColor             = '#000000'

$PuzzelFileUpload.controls.AddRange(@($txtFileName, $btnFileBrowser, $lblFileName, $lblResult, $btnUpload))

#region gui events {
$btnFileBrowser.Add_Click({
	Add-Type -AssemblyName System.windows.forms | Out-Null
		
	$OpenFileDialog = New-Object -TypeName System.Windows.Forms.OpenFileDialog
	$OpenFileDialog.initialDirectory = $initialDirectory
	$OpenFileDialog.filter = 'CSV (*.csv)| *.csv'
	$OpenFileDialog.ShowDialog() | Out-Null
	$filePath = $OpenFileDialog.filename

	$txtFileName.Text = $filePath 
	$PuzzelFileUpload.Refresh()
})
$btnUpload.Add_Click({
	$destPath = '\\server1.my.domain.dk\Puzzel\'
    
    Copy-Item -Path $txtFileName.Text -Destination $destPath

    $sourceHash = Get-FileHash $FilePath
    $destHash   = Get-FileHash "$destPath\Test.csv"

        if($sourceHash -eq $destHash)
        {
            $lblResult.text       = "Filen er nu overført til $FilePath"
            $lblResult.ForeColor  = '#009933'
	        $PuzzelFileUpload.Refresh()
        }
        ELSE
        {
            $lblResult.text       = "Filen er IKKE nu overført"
            $lblResult.ForeColor  = '#ff0000'
	        $PuzzelFileUpload.Refresh()
        }

})

Open in new window

Kasper KatzmannSeniorkonsulent

Author

Commented:
Found the answer to the last bit... I forgot to remove .text '' and .forecolor '' from
$lblResult                       = New-Object -TypeName system.Windows.Forms.Label
$lblResult.text                  = ''
$lblResult.width                 = 270
$lblResult.height                = 30
$lblResult.location              = New-Object -TypeName $SysDrawPoint -ArgumentList (10,78)
$lblResult.Font                  = $SanSerif8
$lblResult.ForeColor             = ''

Open in new window

Kasper KatzmannSeniorkonsulent

Author

Commented:
Thank you both - I really appriciate the help.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.