I am trying to develop a lite app to run a set of powershell scripts. I have a small user base that will need to run the app. This app will have a few presentable options (scripts) that it can run from the start. The user needs to be able to click a button to run the script.
The issue I'm having is how to reference the file needed when I don't know the specific location that the user will be running the script from.
I need to be able to have a background image, and scripts stored in a small folder that will be included in the app. Is there a way to direct the script to look in the current directory for files?
Here is my code currently:
#Credit: Portions of this code were provided at the following URL:
http://ift.tt/1HcQGyV
# LOAD WINDOWS FORMS
# Load the Winforms assembly
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
#Welcome Screen:
#code for welcome splash here. Haven't gotten that far yet.
# CREATE FORM
$Form = New-Object System.Windows.Forms.Form
$Form.Size = New-Object System.Drawing.Size(500,400)
# background. This is where I need help.
$Image = [system.drawing.image]::FromFile("C:\Users\zstewart\Pictures\script runner.png")
#this location is where my question arises. It won't work on another user's machine.
$Form.BackgroundImage = $Image
$Form.BackgroundImageLayout = "Center"
# None, Tile, Center, Stretch, Zoom
#SET FORM TITLE
$form.text = "ScriptRunner v.001"
#TEXT FIELD FOR PRINTING A RESULT
$outputBox = New-Object System.Windows.Forms.TextBox
$outputBox.Location = New-Object System.Drawing.Size(40,60)
$outputBox.Size = New-Object System.Drawing.Size(400,30)
$outputBox.MultiLine = $True
$outputBox.ScrollBars = "Horizontal"
# BUTTON
# Create Button and set text and location
$button = New-Object Windows.Forms.Button
$button.Size = New-Object Drawing.Point 120,30
$button.text = "Select Script"
$button.Location = New-Object Drawing.Point 170, 100
# INPUT HANDLER BUTTON - ON CLICK IN THIS CASE
# Set up event handler to exit
$button.add_click({
Function Get-FileName($initialDirectory)
{
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
Out-Null
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $initialDirectory
$OpenFileDialog.filter = "All files (*.*)| *.*"
$OpenFileDialog.ShowDialog() | Out-Null
$OpenFileDialog.filename
$outputBox.text=$OpenFileDialog.filename
} #end function Get-FileName
Get-FileName -initialDirectory "c:\fso"
})
# Create Button and set text and location
$button2 = New-Object Windows.Forms.Button
$button2.text = "Run Script"
$button2.Size = New-Object Drawing.Point 120,30
$button2.Location = New-Object Drawing.Point 170,140
# INPUT HANDLER RUN BUTTON - RUN SCRIPT
# close button - on click close app
$button2.add_click({
$form.Close() #need to add code to run some included scripts.
})
# ADD CONTROLS TO FORM
$form.controls.add($button)
$form.controls.add($button2)
$Form.Controls.Add($outputBox)
# DISPLAY DIALOG
$form.ShowDialog()
I realize that this is a very VERY dirty code, but I am still trying to learn as I go along. Please excuse my code skills.
Aucun commentaire:
Enregistrer un commentaire