troubleshooting Question

Cannot upload files via Powershell GUI

Avatar of Kasper Katzmann
Kasper KatzmannFlag for Denmark asked on
5 Comments2 Solutions69 ViewsLast Modified:
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

#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'


#region gui events {
Add-Type -AssemblyName | 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 
$destPath = '\\\Puzzel\'

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

#endregion GUI }

#Write your logic code here

$null = $PuzzelFileUpload.ShowDialog()

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

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?
Join our community to see this answer!
Unlock 2 Answers and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros