Is there a way to generate a url to download files either when uploading or after a file has been uploaded?
I'm working a powershell script that will either upload a single file or zip a directory and upload it to Sharefile.
I have most of it complete, but I cannot figured out how generate a download url or have one created at the time of the upload.
Code for the form below
###### Share Link Automation
### Environment Setting
If (-not(test-path -path "C:\windows\ARP\ShareLink")) {
New-Item -Force -Path C:\Windows\ARP\ShareLink -ItemType Directory
}
$SFLinkDestination = "C:\windows\ARP\ShareLink"
# Function to upload files to share file
Function Upload-File {
param (
$LocalData
)
Add-PSSnapin ShareFile
#Run the following interactively to create a login token that can be used by Get-SfClient in unattended scripts
$sfClient = Get-SfClient -Name ((Join-Path $env:USERPROFILE "Documents\Sharefile") + "\MySubdomain.sfps")
#upload directory is relative to the root of the account
#get the current user's home folder to use as the starting point
$ShareFileHomeFolder = (Send-SfRequest $sfClient -Entity Items).Url
# Create a PowerShell provider for ShareFile pointing to Personal Folsers\Send
New-PSDrive -Name sfDrive -PSProvider ShareFile -Client $sfClient -Root "\Personal Folders\Send" -RootUri $ShareFilePath
#upload all the files (recursively) in the local folder to the specified folder in ShareFile
Copy-SfItem -Path $LocalData -Destination "sfDrive:"
#Remove the PSProvider when we are done
Remove-PSDrive sfdrive
Exit
}
#### Init PowerShell Gui
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
#### The Form
[System.Windows.Forms.Application]::EnableVisualStyles()
$SFLinkForm = New-Object system.Windows.Forms.Form
$SFLinkForm.ClientSize = '480,300'
$SFLinkForm.text = "Share File Link"
$SFLinkForm.BackColor = "#ffffff"
$SFLinkForm.TopMost = $false
# $Image = [system.drawing.image]::FromFile("G:\APPS\temp\abp_main.bmp")
$SFLinkForm.BackgroundImage = $Image
$SFLinkForm.BackgroundImageLayout = "None"
$SFLinkForm.AutoSizeMode = "GrowAndShrink"
$SFLinkFormTitle = New-Object system.Windows.Forms.Label
$SFLinkFormTitle.text = "Share File Link"
$SFLinkFormTitle.ForeColor = "DarkRed"
$SFLinkFormTitle.AutoSize = $true
$SFLinkFormTitle.width = 45
$SFLinkFormTitle.height = 20
$SFLinkFormTitle.location = New-Object System.Drawing.Point(20,100)
$SFLinkFormTitle.Font = 'Microsoft Sans Serif,13,style=Bold'
$SFLinkFormTitleZiP = New-Object system.Windows.Forms.Label
$SFLinkFormTitleZip.text = "Share File Zip Name:"
$SFLinkFormTitleZip.ForeColor = "DarkRed"
$SFLinkFormTitleZip.AutoSize = $true
$SFLinkFormTitleZip.width = 45
$SFLinkFormTitleZip.height = 20
$SFLinkFormTitleZip.location = New-Object System.Drawing.Point(20,175)
$SFLinkFormTitleZip.Font = 'Microsoft Sans Serif,10,style=Bold'
$SFLinkFormDescription = New-Object system.Windows.Forms.Label
$SFLinkFormDescription.text = "When creating a link ..... TBD"
$SFLinkFormDescription.ForeColor = "DarkRed"
$SFLinkFormDescription.AutoSize = $false
$SFLinkFormDescription.width = 450
$SFLinkFormDescription.height = 20
$SFLinkFormDescription.location = New-Object System.Drawing.Point(20,125)
$SFLinkFormDescription.Font = 'Microsoft Sans Serif,10'
### Zip File Namer
$SFLinkFormZip = New-Object System.Windows.Forms.TextBox
$SFLinkFormZip.Location = New-Object System.Drawing.Size(20,200)
$SFLinkFormZip.Size = New-Object System.Drawing.Size(220,20)
$SFLinkForm.Controls.Add($SFLinkFormZip)
#### File Selector
$SFLinkFormFileBtn = New-Object system.Windows.Forms.Button
$SFLinkFormFileBtn.BackColor = "#ffffff"
$SFLinkFormFileBtn.text = "File"
$SFLinkFormFileBtn.width = 90
$SFLinkFormFileBtn.height = 20
$SFLinkFormFileBtn.location = New-Object System.Drawing.Point(200,230)
$SFLinkFormFileBtn.Font = 'Microsoft Sans Serif,10,style=Bold'
$SFLinkFormFileBtn.ForeColor = "#0"
$SFLinkForm.Controls.Add($SFLinkFormFileBtn)
$SFLinkFormFileBtnLabel = New-Object system.Windows.Forms.Label
$SFLinkFormFileBtnLabel.text = "Click to Select a File:"
$SFLinkFormFileBtnLabel.ForeColor = "DarkRed"
$SFLinkFormFileBtnLabel.AutoSize = $true
$SFLinkFormFileBtnLabel.width = 25
$SFLinkFormFileBtnLabel.height = 20
$SFLinkFormFileBtnLabel.location = New-Object System.Drawing.Point(20,232)
$SFLinkFormFileBtnLabel.Font = 'Microsoft Sans Serif,10,style=Bold'
$SFLinkFormFileBtn.Add_Click(
{
Add-Type -AssemblyName System.Windows.Forms
$SFLinkFormFClick = New-Object System.Windows.Forms.OpenFileDialog
$SFLinkFormFClick.Title = "Please Select File"
$SFLinkFormFClick.InitialDirectory = $InitialDirectory
$SFLinkFormFClick.filter = “All files (*.*)| *.*”
# If ($SFLinkFormFClick.ShowDialog() -eq "Cancel") {
If ($SFLinkFormFClick.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) {
$LocalData = ($SFLinkFormFClick).FileName
# [System.Windows.Forms.MessageBox]::Show("No File Selected. Please select a file !", "Error", 0,
# [System.Windows.Forms.MessageBoxIcon]::Exclamation)
}
Write-Host "The File Selected is ($SFLinkFormFClick).FileName"
Write-Host "$LocalData"
Upload-File -LocalData "$LocalData"
$SFLinkForm.Close()
Return
Exit
# $LocalData = ($SFLinkFormFClick).FileName
}
)
### Directory Selector
$SFLinkFormDirBtn = New-Object system.Windows.Forms.Button
$SFLinkFormDirBtn.BackColor = "#ffffff"
$SFLinkFormDirBtn.text = "Directory"
$SFLinkFormDirBtn.width = 90
$SFLinkFormDirBtn.height = 20
$SFLinkFormDirBtn.location = New-Object System.Drawing.Point(200,255)
$SFLinkFormDirBtn.Font = 'Microsoft Sans Serif,10,style=Bold'
$SFLinkFormDirBtn.ForeColor = "#0"
$SFLinkForm.Controls.Add($SFLinkFormDirBtn)
$SFLinkFormDirBtnLabel = New-Object system.Windows.Forms.Label
$SFLinkFormDirBtnLabel.text = "Click to Select a Directory:"
$SFLinkFormDirBtnLabel.ForeColor = "DarkRed"
$SFLinkFormDirBtnLabel.AutoSize = $true
$SFLinkFormDirBtnLabel.width = 25
$SFLinkFormDirBtnLabel.height = 20
$SFLinkFormDirBtnLabel.location = New-Object System.Drawing.Point(20,257)
$SFLinkFormDirBtnLabel.Font = 'Microsoft Sans Serif,10,style=Bold'
$SFLinkFormDirBtn.Add_Click(
{
$SFLinkFormDClick = New-Object System.Windows.Forms.FolderBrowserDialog
if ($SFLinkFormDClick.ShowDialog() -eq [System.Windows.Forms.DialogResult]::"OK") {
$SFLinkFormDirName = $SFLinkFormDClick.SelectedPath
$ZipWrapper = $SFLinkFormZip.Text
$LocalData = (Join-Path C:\windows\ARP\ShareLink\ "$ZipWrapper.zip")
### Directgory Compression
# cd C:\windows\ARP\ShareLink
Compress-Archive "$SFLinkFormDirName\*" "$LocalData"
Upload-File -LocalData "$LocalData"
$SFLinkForm.Close()
Return
Exit
}
}
)
$SFLinkForm.controls.AddRange(#($SFLinkFormTitle,$SFLinkFormDescription,$SFLinkFormFileBtn,$SFLinkFormDirBtn,$SFLinkFormFileBtnLabel,$SFLinkFormDirBtnLabel,$SFLinkFormZip,$SFLinkFormTitleZiP))
$SFLinkForm.ShowDialog()
$SFLinkForm.Close()
Exit
Search all the API codes for Sharefile and could not find the powershell equivalence of "Get A Link" in the API Entities.
Related
I am trying to create a GUI which always should create a folder with the string from a textbox and after add oder Item and folder
With the First Button i create the folder using the string from the textbox
With the Butto "save" another folder should be added to the new created folder
Here my script
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$window = New-Object System.Windows.Forms.Form
$window.Width = 1300
$window.Height = 1000
$window.Text = "Project Management Dashboard"
$Label1 = New-Object System.Windows.Forms.Label
$Label1.Location = New-Object System.Drawing.Size(10,10)
$Label1.Text = "Creator"
$Label1.AutoSize = $True
$Label1.ForeColor = "Black"
$window.Controls.Add($Label1)
$windowTextBox1 = New-Object System.Windows.Forms.TextBox
$windowTextBox1.Location = New-Object System.Drawing.Size(10,40)
$windowTextBox1.Size = New-Object System.Drawing.Size (300,500)
$window.Controls.Add($windowTextBox1)
$windowsbuttonquery = New-Object System.Windows.Forms.Button
$windowsbuttonquery.Location = New-Object System.Drawing.Point(400,800)
$windowsbuttonquery.Size = New-Object System.Drawing.Size(175,80)
$windowsbuttonquery.Text = "Project Package"
$windowsbuttonquery.ForeColor = "Black"
$window.Controls.Add($windowsbuttonquery)
$windowsbuttonquery.Add_Click({
New-Item -Path C:\Test_Ps1 -Name $windowTextBox1.Text -ItemType directory
}
)
$save = New-Object System.Windows.Forms.Button
$save.Location = New-Object System.Drawing.Point(700,800)
$save.Size = New-Object System.Drawing.Size(175,80)
$save.Text = "SAVE"
$save.ForeColor = "Black"
$window.Controls.Add($save)
$save.Add_Click({
$to = "c:\Test_Ps1\$windowTextBox1.Text"
Copy-Item -Path C:\Test_Ps1\Z -Destination $to
$window.Dispose()
}
)
[void]$window.ShowDialog()
Thanks for help.
I have one question. I don't understand what I am doing wrong, I wrote the following script to tell me if the folder exists to show a message, but it is now showing it.
If (Test-Path -path ($LabServer + $CaseName) -PathType Container){
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.MessageBox]::Show('Folder name exists, please enter a new name')
The script is supposed to create multiple folders in a specific location. I want to check if the folder name (Case Name) already exists, and if it does, do not create the folder with the same name, but the script continues and creates the folder and sub-folders.
Here is the full script.
Thank you for all your help
# Load required assemblies
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
# Drawing form and controls
$CreateFolder = New-Object System.Windows.Forms.Form
$CreateFolder.Text = "Create Multiple Custodian Folders"
$CreateFolder.Size = New-Object System.Drawing.Size(350,415)
$CreateFolder.FormBorderStyle = "FixedDialog"
$CreateFolder.TopMost = $true
$CreateFolder.MaximizeBox = $false
$CreateFolder.MinimizeBox = $false
$CreateFolder.ControlBox = $true
$CreateFolder.StartPosition = "CenterScreen"
$CreateFolder.Font = "Segoe UI"
#======================== CASE NAME ========================#
# adding a label to my form
$label_message = New-Object System.Windows.Forms.Label
$label_message.Location = New-Object System.Drawing.Size(20,8)
$label_message.Size = New-Object System.Drawing.Size(100,15)
$label_message.Text = "Case Name"
$CreateFolder.Controls.Add($label_message)
# CaseName
$CaseName = New-Object System.Windows.Forms.TextBox
$CaseName.Location = New-Object System.Drawing.Size(20,30)
$CaseName.Size = New-Object System.Drawing.Size(300,25)
$CaseName.ScrollBars = "Vertical"
$CreateFolder.Controls.Add($CaseName)
#======================== DROPBOX ========================#
$label_messageCombobox = New-Object System.Windows.Forms.Label
$label_messageCombobox.Location = New-Object System.Drawing.Size(20,60)
$label_messageCombobox.Size = New-Object System.Drawing.Size(100,15)
$label_messageCombobox.Text = "Pick a Server"
$CreateFolder.Controls.Add($label_messageCombobox)
$DropdownBox = New-Object System.Windows.Forms.ComboBox
$DropdownBox.Location = New-Object System.Drawing.Size(20,80)
$DropdownBox.Size = New-Object System.Drawing.Size(300,15)
$DropdownBox.Height = 200
$Dropdownbox.DropDownStyle = "DropDownList"
$CreateFolder.Controls.Add($DropdownBox)
$Servers = #("Lab Machine 40","Lab Machine 45","Lab Machine 50","Lab Machine 55")
foreach($Server in $Servers){
$DropdownBox.Items.Add($Server) | Out-Null
}
#======================== FUNCTION TO GET SERVER ========================#
Function Get-Server(){
$SelectedServer = $DropdownBox.SelectedItem.ToString()
if($SelectedServer -eq "Lab Machine 50") {
$Script:LabServer = Set-Location "\\Server50\K$" -PassThru
}
elseif($SelectedServer -eq "Lab Machine 55") {
$Script:LabServer = Set-Location "\\Server55\K$" -PassThru
}
elseif($SelectedServer -eq "Lab Machine 40") {
$Script:LabServer = Set-Location "\\Server40\K$" -PassThru
}
elseif($SelectedServer -eq "Lab Machine 45") {
$Script:LabServer = Set-Location "\\Server45\K$" -PassThru
}
}
#======================== INPUTBOX ========================#
$label_message2 = New-Object System.Windows.Forms.Label
$label_message2.Location = New-Object System.Drawing.Size(20,110)
$label_message2.Size = New-Object System.Drawing.Size(100,15)
$label_message2.Text = "Custodian Names"
$CreateFolder.Controls.Add($label_message2)
# Inputbox
$Inputbox = New-Object System.Windows.Forms.TextBox
$Inputbox.Multiline = $True;
$Inputbox.Location = New-Object System.Drawing.Size(20,130)
$Inputbox.Size = New-Object System.Drawing.Size(300,200)
$Inputbox.ScrollBars = "Vertical"
$CreateFolder.Controls.Add($Inputbox)
#======================== BUTTON ========================#
# add a button ti create folder
$button_ClickMe = New-Object System.Windows.Forms.Button
$button_ClickMe.Location = New-Object System.Drawing.Size(45,340)
$button_ClickMe.Size = New-Object System.Drawing.Size(240,32)
$button_ClickMe.TextAlign = "MiddleCenter"
$button_ClickMe.Text = "Create Folders"
$button_ClickMe.Add_Click({
#$FolderExist = $LabServer.Text + $CaseName.Text
If ($CaseName.TextLength -eq 0){
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.MessageBox]::Show('Please Enter a Case Name')
}
else {
If (Test-Path -path ($LabServer + $CaseName) -PathType Container){
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.MessageBox]::Show('Folder name exists, please enter a new name')
}
else {
if ($Inputbox.TextLength -eq 0){
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.MessageBox]::Show('Please enter 1 custodian name')
}
else {
Get-Server
Set-Location $LabServer
New-Item $CaseName.Text -type directory
Start-Sleep -Seconds 5
Set-Location ($LabServer.Text + $CaseName.Text)
ForEach ($Folder in $Inputbox.lines) {
New-Item $Folder -type directory
}
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.MessageBox]::Show('Folders were created')
[System.Windows.Forms.Application]::Exit()
}
}
}
})
$CreateFolder.Controls.Add($button_ClickMe)
# show form
$CreateFolder.Add_Shown({$CreateFolder.Activate()})
[void] $CreateFolder.ShowDialog()
You almost answered your own question here:
#$FolderExist = $LabServer.Text + $CaseName.Text
Check out the comments in the code:
If ($CaseName.TextLength -eq 0)
{
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.MessageBox]::Show('Please Enter a Case Name')
}
else
{
# If we are here $LabServer is not defined because Get-Server did not run yet
# You can take 2 different approaches, either use your function or
# Test-Path using the ComboBox
# Approach 1: Define $LabServer
Get-Server
# Now you can Test-Path
$path = Join-Path $LabServer -ChildPath $CaseName.Text
If (Test-Path -Path $path -PathType Container)
{
.....
}
# Approach 2:
$path = Join-Path ("\\{0}\K$" -f $DropdownBox.SelectedItem) -ChildPath $CaseName.Text
If (Test-Path -Path $path -PathType Container)
{
.....
}
.....
Edit
I think it's worth adding a few recommendations to reduce the number of nested conditions you have on your AddClick listener.
First, by default at the start of your form, disable the button:
$button_ClickMe.Enabled = $false
Now you can add 2 listeners on TextChanged for both TextBox:
$textChangedEvent = {
if($caseName.TextLength -and $DropdownBox.SelectedItem -and $Inputbox.TextLength)
{
$button_ClickMe.Enabled = $true
}
else
{
$button_ClickMe.Enabled = $false
}
}
$CaseName.Add_TextChanged($textChangedEvent)
$Inputbox.Add_TextChanged($textChangedEvent)
With this you can remove the 2 conditions you have on $CaseName.TextLength -eq 0 and $Inputbox.TextLength -eq 0.
Lastly, your AddClick event would look like this (Note that you wouldn't need the Get-Server function):
$buttonClickEvent = {
$path = "\\{0}\K$" -f $DropdownBox.SelectedItem
$path = Join-Path $path -ChildPath $CaseName.Text
if(Test-Path $path -PathType Container)
{
[System.Windows.Forms.MessageBox]::Show('Folder name exists, please enter a new name')
return
}
New-Item $path -Type Directory
Start-Sleep -Seconds 5
foreach($folder in $Inputbox.Lines)
{
$folder = $Folder.Trim()
$newFolder = Join-Path $path -ChildPath $folder
New-Item $newFolder -Type Directory
[System.Windows.Forms.MessageBox]::Show('Folders were created')
[System.Windows.Forms.Application]::Exit()
}
}
$button_ClickMe.Add_Click($buttonClickEvent)
I need to create a checkbox automatically depends on folder I selected. I create ComboBox, then in the ComboBox, I can select which folder that I want to select. Inside of my folder, I have some file. The file consist of some extension file. I just need to pick 2 extension file from the folder, example(*.txt and *.csv).
After I select the folder, the checkBox will create automatically, the total of the checkBox depends on how many file exist in that folder with specific extension(*.txt and *.csv).
In my code, I already do some stuff, which is select the folder that I need to select, but still struggle with the checkBox. Anyone can help me please. Thank you so much. I really appreciate for the help.
I put my script in the ##2nd Updated.
Updated
Consider to #f6a4 answer, this is the result
The first picture is I just use this path to get the folder
'D:\Data\'
In the first picture, I already double click the folder1 and folder2, but the file do not appear.
The second picture, I specify the folder path
'D:\Data\folder1'
The second picture, the file appear because I specify the folder in the path, so the folder name do not appear in the please select folder box and return this error $CTRL_ListFolder.Items.AddRange( (Get-ChildItem -Path $GH.CurrentFold ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ArgumentNullException
2n Updated
I updated my script. But the file only appear 1, once I click the folder. And when I change to click other folder, It does not appear the file.
Add-Type -AssemblyName PresentationFramework
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Global:status = "inactive"
$Global:array = New-Object System.Collections.Generic.List[System.Object]
$Form = New-Object system.Windows.Forms.Form
$Form.text = "BPS Image Automation Utility"
$Form.BackColor = "#f6f6f6"
$Form.AutoSize = $true
$Form.FormBorderStyle = "FixedDialog"
$Form.MaximizeBox = $false
$Form.startposition = "centerscreen"
$Form.WindowState = 'Maximized'
$Label1 = New-Object system.Windows.Forms.Label
$Label1.text = "Please select the image"
$Label1.AutoSize = $true
$Label1.width = 25
$Label1.height = 10
$Label1.location = New-Object System.Drawing.Point(50,50)
$Label1.Font = 'Microsoft Sans Serif,10'
$Label1.ForeColor = "#000000"
$label1.AutoSize = $true
$Button3 = New-Object system.Windows.Forms.Button
$Button3.BackColor = "#136aa4"
$Button3.ForeColor = "#ffffff"
$Button3.text = "Done"
$Button3.width = 90
$Button3.height = 32
$Button3.AutoSize = $true
$Button3.UseCompatibleTextRendering = $True
$Button3.UseVisualStyleBackColor = $False
# $Button3.location = New-Object System.Drawing.Point(1700,920)
$Button3.Font = 'Microsoft Sans Serif,10'
# $Button3.Visible = $false
$Button2 = New-Object system.Windows.Forms.Button
$Button2.BackColor = "#136aa4"
$Button2.ForeColor = "#ffffff"
$Button2.text = "Delete"
$Button2.width = 90
$Button2.height = 32
$Button2.UseCompatibleTextRendering = $True
$Button2.UseVisualStyleBackColor = $False
$Button2.AutoSize = $true
# $Button2.location = New-Object System.Drawing.Point(1600,920)
$Button2.Font = 'Microsoft Sans Serif,10'
# $Button2.Visible = $false
$Panel = New-Object System.Windows.Forms.TableLayoutPanel
$panel.Dock = "Fill"
$panel.ColumnCount = 1
$panel.RowCount = 1
$panel.CellBorderStyle = "single"
$panel.ColumnStyles.Add((new-object System.Windows.Forms.ColumnStyle([System.Windows.Forms.SizeType]::Percent, 100)))
$panel.RowStyles.Add((new-object System.Windows.Forms.RowStyle([System.Windows.Forms.SizeType]::Percent, 100)))
$Groupbox1 = New-Object system.Windows.Forms.Groupbox
$Groupbox1.text = "Job Handling"
$Groupbox1.Font = 'Microsoft Sans Serif,9'
$Groupbox1.AutoSize = $true
$Groupbox1.ForeColor = "#032d5d"
$Groupbox1.location = New-Object System.Drawing.Point(8,13)
$Groupbox1.Padding = New-Object -TypeName System.Windows.Forms.Padding -ArgumentList (0,5,5,0)
$Groupbox1.Dock = "fill"
$Groupbox1.UseCompatibleTextRendering = $True
$Groupbox2 = New-Object system.Windows.Forms.Groupbox
$Groupbox2.text = "Job Information"
$Groupbox2.Font = 'Microsoft Sans Serif,9'
$Groupbox2.AutoSize = $true
$Groupbox2.ForeColor = "#032d5d"
$Groupbox2.Dock = "None"
$Groupbox2.UseCompatibleTextRendering = $True
$ComboBox1 = New-Object system.Windows.Forms.ComboBox
$ComboBox1.BackColor = "#e8f3ff"
$ComboBox1.width = 190
$ComboBox1.height = 20
$ComboBox1.location = New-Object System.Drawing.Point(35,80)
$ComboBox1.Font = 'Microsoft Sans Serif,12'
$ComboBox1.AutoSize = $true
$ImageList = #(Get-ChildItem -Directory "D:\Process")
foreach ($img in $ImageList) {
$ComboBox1.Items.Add($img)
}
$ComboBox1.Add_Click({
if($ComboBox1.SelectedItem){
$Checkbox.Visible = $true
}
})
$Checkboxes += New-Object System.Windows.Forms.CheckBox
$Checkboxes = #()
$y = 20
$files = Get-ChildItem "D:\Process\$img" -Filter *.txt, *.csv
$files
foreach ($file in $files)
{
$Checkbox = New-Object System.Windows.Forms.CheckBox
$Checkbox.Text = $file
$Checkbox.Location = New-Object System.Drawing.Size(10,$y)
$Checkbox.Size = New-Object System.Drawing.Size(330,20)
$y += 30
$Groupbox2.Controls.Add($Checkbox)
$Checkboxes += $Checkbox
$Checkbox.Visible = $false
}
$Form.controls.AddRange(#($Panel))
$Panel.controls.AddRange(#($Groupbox1))
$Groupbox1.Controls.AddRange(#($Groupbox2, $ComboBox1, $Label1, $Button3, $Button2))
[void]$Form.Show()
$g2w = $Form.Width - 90
$g2h = $Form.Height - 270
$g2h
$Groupbox2.location = New-Object System.Drawing.Point(35,110)
$Groupbox2.size = New-Object System.Drawing.Size($g2w,$g2h)
$Groupbox2.AutoSize = $true
$bt2_w = $g2w - 55
$bt2_h = $g2h + 130
$Button2.location = New-Object System.Drawing.Point($bt2_w,$bt2_h)
$bt3_w = $g2w - 160
$Button3.location = New-Object System.Drawing.Point($bt3_w,$bt2_h)
$Form.Visible = $false
[void]$Form.ShowDialog()
This should do exactly what you want. By double click on a Directory you can browse through subdirectories as well.
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
# Global variables
$GH = [hashtable]::Synchronized(#{})
$GH.FolderPath = 'C:\Users\myUser\Desktop\csvfiles'
$GH.CurrentFolderPath = $GH.FolderPath
$GH.FileMask = #('*.txt','*.csv')
# windows form
$form = New-Object System.Windows.Forms.Form
$form.Visible = $false
[void]$form.SuspendLayout()
$form.Text = "File Selection"
$form.ClientSize = New-Object System.Drawing.Size(320,430)
$form.StartPosition = 'CenterScreen'
$form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle
# tab control
$CTRL_TabCtrl = New-Object System.Windows.Forms.TabControl
$CTRL_TabCtrl.Location = New-Object System.Drawing.Point(5,5)
$CTRL_TabCtrl.Size = New-Object System.Drawing.Size(310,420)
[void]$form.Controls.Add($CTRL_TabCtrl)
$CTRL_Tab1 = New-Object System.Windows.Forms.TabPage
$CTRL_Tab1.AutoSize = $true
$CTRL_Tab1.Text = 'Main'
$CTRL_Tab1.TabIndex = 1
[void]$CTRL_TabCtrl.Controls.Add($CTRL_Tab1)
# list folder
$CTRL_label10 = New-Object System.Windows.Forms.Label
$CTRL_label10.Location = New-Object System.Drawing.Point(10,10)
$CTRL_label10.Size = New-Object System.Drawing.Size(260,20)
$CTRL_label10.Name = 'Label10'
$CTRL_label10.Text = 'Please select a folder:'
[void]$CTRL_Tab1.Controls.Add($CTRL_label10)
$CTRL_ListFolder = New-Object System.Windows.Forms.Listbox
$CTRL_ListFolder.Location = New-Object System.Drawing.Point(10,30)
$CTRL_ListFolder.Size = New-Object System.Drawing.Size(280,60)
$CTRL_ListFolder.SelectionMode = [System.Windows.Forms.SelectionMode]::One
$CTRL_ListFolder.Items.AddRange( (Get-ChildItem -Path $GH.CurrentFolderPath -Directory).Name )
$CTRL_ListFolder.Enabled = $true
$CTRL_ListFolder.Add_MouseDoubleClick( {
$listFolder_innerevent = $true
if( $CTRL_ListFolder.SelectedItem -eq '..' ) {
$GH.CurrentFolderPath = $GH.CurrentFolderPath.Substring( 0, $GH.CurrentFolderPath.LastIndexOf( '\' ) )
[void]$CTRL_ListFolder.Items.Clear()
if( $GH.CurrentFolderPath.Length -gt $GH.FolderPath.Length ) {
[void]$CTRL_ListFolder.Items.Add( '..' )
}
$CTRL_ListFolder.Items.AddRange( (Get-ChildItem -Path $GH.CurrentFolderPath -Directory).Name )
[void]$CTRL_CheckListBox.Items.Clear()
$files = (Get-ChildItem -Path ($GH.CurrentFolderPath + '\*') -Include $GH.FileMask -File ).Name
if( $files ) {
[void]$CTRL_CheckListBox.Items.AddRange( $files )
}
}
else {
if( (Get-ChildItem -Path ($GH.CurrentFolderPath + '\' + $CTRL_ListFolder.SelectedItem) -Directory).Name ) {
$GH.CurrentFolderPath += '\' + $CTRL_ListFolder.SelectedItem
[void]$CTRL_ListFolder.Items.Clear()
[void]$CTRL_ListFolder.Items.Add( '..' )
[void]$CTRL_ListFolder.Items.AddRange( (Get-ChildItem -Path $GH.CurrentFolderPath -Directory).Name )
[void]$CTRL_CheckListBox.Items.Clear()
$files = (Get-ChildItem -Path ($GH.CurrentFolderPath + '\*') -Include $GH.FileMask -File ).Name
if( $files ) {
[void]$CTRL_CheckListBox.Items.AddRange( $files )
}
}
}
} )
[void]$CTRL_Tab1.Controls.Add($CTRL_ListFolder)
# list folder with check boxes for files
$CTRL_label12 = New-Object System.Windows.Forms.Label
$CTRL_label12.Location = New-Object System.Drawing.Point(10,100)
$CTRL_label12.Size = New-Object System.Drawing.Size(260,20)
$CTRL_label12.Name = 'Label12'
$CTRL_label12.Text = 'Files found:'
[void]$CTRL_Tab1.Controls.Add($CTRL_label12)
$CTRL_CheckListBox = New-Object System.Windows.Forms.CheckedListbox
$CTRL_CheckListBox.Location = New-Object System.Drawing.Point(10,120)
$CTRL_CheckListBox.Size = New-Object System.Drawing.Size(280,230)
$CTRL_CheckListBox.CheckOnClick = $true
$CTRL_CheckListBox.Enabled = $true
$files = (Get-ChildItem -Path ($GH.CurrentFolderPath + '\*') -Include $GH.FileMask -File ).Name
if( $files ) {
[void]$CTRL_CheckListBox.Items.AddRange( $files )
}
[void]$CTRL_Tab1.Controls.Add($CTRL_CheckListBox)
$CTRL_OKButton1 = New-Object System.Windows.Forms.Button
$CTRL_OKButton1.Location = New-Object System.Drawing.Point(70,365)
$CTRL_OKButton1.Size = New-Object System.Drawing.Size(75,23)
$CTRL_OKButton1.Text = 'OK'
$CTRL_OKButton1.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $CTRL_OKButton1
[void]$CTRL_Tab1.Controls.Add($CTRL_OKButton1)
$CTRL_CancelButton1 = New-Object System.Windows.Forms.Button
$CTRL_CancelButton1.Location = New-Object System.Drawing.Point(150,365)
$CTRL_CancelButton1.Size = New-Object System.Drawing.Size(75,23)
$CTRL_CancelButton1.Text = 'Cancel'
$CTRL_CancelButton1.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CTRL_CancelButton1
[void]$CTRL_Tab1.Controls.Add($CTRL_CancelButton1)
[void]$form.ResumeLayout()
$userInput = $form.ShowDialog()
if ($userInput -eq [System.Windows.Forms.DialogResult]::OK) {
# User clicked OK Button
}
I want to decide which folder that I need to choose based on my data, then if I can't find it, it will show the GUI for waiting and do looping to check it. I try this code, I can find the folder, but when I can't find it once I want to show the GUI it returns some error.
This is how I checking the folder
function FIND {
Write-Host "call the function that can call the GUI.ps1 script"
$Path = "D:\Process"
Write-Host "Starting Mapping SSID and Finding Job"
$SSID_Unit = "111dddddfafafesa"
Try{
$Path_Job = (Get-Item (Get-ChildItem "$Path\*\SSID_LST" | Select-String -Pattern "$SSID_Unit").Path).Directory.FullName
$global:Result = [PSCustomObject]#{
Exists = $true
FileName = $Path_Job.FullName
Attempts = 1
}
Write-Host "Job'$($global:Result.FileName)' Exists. Found after $($global:Result.Attempts) attempts." -ForegroundColor Green
Write-Host "Continue to Assigned Job"
Pause
} Catch {
Write-Host "Waiting for the jobss"
& D:\X\Wait_GUI.ps1 -Path $Path_Job -MaxAttempts 20
Write-Host "Job not found after $($global:Result.Attempts) attempts." -ForegroundColor Red
}
}
FIND
This is the GUI
Param (
[string]$Path = '*.*',
[string]$MaxAttempts = 5
)
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
# set things up for the timer
$script:nAttempts = 0
$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = 1000 # 1 second
$timer.Add_Tick({
$global:Result = $null
$script:nAttempts++
$Path_Job = Get-Item -Path $Path
if ($Path_Job) {
$global:Result = [PSCustomObject]#{
Exists = $true
FileName = $Path_Job.FullName
Attempts = $script:nAttempts
}
$timer.Dispose()
$Form.Close()
}
elseif ($script:nAttempts -ge $MaxAttempts) {
$global:Result = [PSCustomObject]#{
Exists = $false
FileName = ''
Attempts = $script:nAttempts
}
$timer.Dispose()
$Form.Close()
}
})
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '617,418'
$Form.text = "AutoGM"
$Form.BackColor = "#8b572a"
$Form.TopMost = $false
$Form.WindowState = 'Maximized'
$Label1 = New-Object system.Windows.Forms.Label
$Label1.text = "UNDER AUTOMATION PROCESS"
$Label1.AutoSize = $true
$Label1.width = 25
$Label1.height = 10
$Label1.Anchor = 'top,right,bottom,left'
$Label1.ForeColor = "#ffffff"
$Label1.Anchor = "None"
$Label1.TextAlign = "MiddleCenter"
$Label2 = New-Object system.Windows.Forms.Label
$Label2.text = "Waiting for the job..."
$Label2.AutoSize = $true
$Label2.width = 25
$Label2.height = 10
$Label2.ForeColor = "#ffffff"
$Label2.Anchor = "None"
$Label2.TextAlign = "MiddleCenter"
$Form.controls.AddRange(#($Label1,$Label2))
[void]$Form.Show()
# Write-Host $Form.Height
# Write-Host $Form.Width
$Label1.location = New-Object System.Drawing.Point(($Form.Width*0.35), ($Form.Height*0.4))
$Label2.location = New-Object System.Drawing.Point(($form.Width*0.43), ($Form.Height*0.5))
$L_S = (($Form.Width/2) - ($Form.Height / 2)) / 15
$Label1.Font = "Microsoft Sans Serif, $L_S, style=Bold"
$Label2.Font = "Microsoft Sans Serif, $L_S, style=Bold"
$Form.controls.AddRange(#($Label1,$Label2))
# start the timer as soon as the dialog is visible
$Form.Add_Shown({ $timer.Start() })
$Form.Visible = $false
[void]$Form.ShowDialog()
# clean up when done
$Form.Dispose()
if not found, it return this
Waiting for the jobss
Job not found after 1 attempts.
and the GUI is not shown
Ok, first of all, you are using different tests for the file and/or directory in the code and in the GUI. Furthermore, you call the GUI.ps1 file with a path set to a $null value.
I would change your code to something like this:
$Path = "D:\Process\*\SSID_LST\*" # the path to look for files
$SSID_Unit = "111dddddfafafesa" # the Search pattern to look for inside the files
function Test-FileWithGui {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true, Position = 0)]
[string]$Path,
[Parameter(Mandatory = $true, Position = 2)]
[string]$Pattern,
[int]$MaxAttempts = 5
)
Write-Host "Starting Mapping SSID and Finding Job"
# set up an 'empty' $global:Result object to return on failure
$global:Result = '' | Select-Object #{Name = 'Exists'; Expression = {$false}}, FileName, Directory, #{Name = 'Attempts'; Expression = {1}}
# test if the given path is valid. If not, exit the function
if (!(Test-Path -Path $Path -PathType Container)) {
Write-Warning "Path '$Path' does not exist."
return
}
# try and find the first file that contains your search pattern
$file = Select-String -Path $Path -Pattern $Pattern -SimpleMatch -ErrorAction SilentlyContinue | Select-Object -First 1
if ($file) {
$file = Get-Item -Path $file.Path
$global:Result = [PSCustomObject]#{
Exists = $true
FileName = $file.FullName
Directory = $file.DirectoryName
Attempts = 1
}
}
else {
& "D:\GUI.ps1" -Path $Path -Pattern $Pattern -MaxAttempts $MaxAttempts
}
}
# call the function that can call the GUI.ps1 script
Test-FileWithGui -Path $Path -Pattern $SSID_Unit -MaxAttempts 20
# show the $global:Result object with all properties
$global:Result | Format-List
# check the Global result object
if ($global:Result.Exists) {
Write-Host "File '$($global:Result.FileName)' Exists. Found after $($global:Result.Attempts) attempts." -ForegroundColor Green
}
else {
Write-Host "File not found after $($global:Result.Attempts) attempts." -ForegroundColor Red
}
Next the GUI file.
As you are now searching for a file that contains some text, you need a third parameter to call this named Pattern.
Inside the GUI file we perform the exact same test as we did in the code above, using the parameter $Pattern as search string:
Param (
[string]$Path,
[string]$Pattern,
[int]$MaxAttempts = 5
)
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
# set things up for the timer
$script:nAttempts = 0
$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = 1000 # 1 second
$timer.Add_Tick({
$global:Result = $null
$script:nAttempts++
# use the same test as you did outside of the GUI
# try and find the first file that contains your search pattern
$file = Select-String -Path $Path -Pattern $Pattern -SimpleMatch -ErrorAction SilentlyContinue | Select-Object -First 1
if ($file) {
$file = Get-Item -Path $file.Path
$global:Result = [PSCustomObject]#{
Exists = $true
FileName = $file.FullName
Directory = $file.DirectoryName
Attempts = $script:nAttempts
}
$timer.Dispose()
$Form.Close()
}
elseif ($script:nAttempts -ge $MaxAttempts) {
$global:Result = [PSCustomObject]#{
Exists = $false
FileName = $null
Directory = $null
Attempts = $script:nAttempts
}
$script:nAttempts = 0
$timer.Dispose()
$Form.Close()
}
})
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '617,418'
$Form.Text = "AutoGM"
$Form.BackColor = "#8b572a"
$Form.TopMost = $true
$Form.WindowState = 'Maximized'
# I have removed $Label2 because it is easier to use
# just one label here and Dock it to Fill.
$Label1 = New-Object system.Windows.Forms.Label
$Label1.Text = "UNDER AUTOMATION PROCESS`r`n`r`nWaiting for the job..."
$Label1.AutoSize = $false
$Label1.Dock = 'Fill'
$Label1.TextAlign = "MiddleCenter"
$Label1.ForeColor = "#ffffff"
$L_S = (($Form.Width/2) - ($Form.Height / 2)) / 10
$Label1.Font = "Microsoft Sans Serif, $L_S, style=Bold"
$Form.controls.Add($Label1)
# start the timer as soon as the dialog is visible
$Form.Add_Shown({ $timer.Start() })
[void]$Form.ShowDialog()
# clean up when done
$Form.Dispose()
The results during testing came out like below
If the file was found within the set MaxAttempts tries:
Starting Mapping SSID and Finding Job
Exists : True
FileName : D:\Process\test\SSID_LST\blah.txt
Directory : D:\Process\test\SSID_LST
Attempts : 7
File 'D:\Process\test\SSID_LST\blah.txt' Exists. Found after 7 attempts.
When the file was NOT found:
Starting Mapping SSID and Finding Job
Exists : False
FileName :
Directory :
Attempts : 20
File not found after 20 attempts.
If even the folder $Path was not found, the output is
Starting Mapping SSID and Finding Job
WARNING: Path 'D:\Process\*\SSID_LST\*' does not exist.
Exists : False
FileName :
Directory :
Attempts : 1
File not found after 1 attempts.
Hope that helps
I have built a powershell script using the GUI .net framework that provides the user with a graphical interface to add alternate data streams (ADS) to files on a NTFS file system.
Below is the code I wrote for the powershell script:
<#
This script is a GUI featured way to add extended attributes to files
#>
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
#region begin GUI{
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '600,600'
$Form.text = "Add Extended Attributes"
$Form.TopMost = $false
# Add Extended Attributes Label
$mainLabel = New-Object system.Windows.Forms.Label
$mainLabel.text = "Add Extended Attributes"
$mainLabel.AutoSize = $true
$mainLabel.width = 25
$mainLabel.height = 10
$mainLabel.location = New-Object System.Drawing.Point(180,10)
$mainLabel.Font = 'Microsoft Sans Serif,18'
# text box for entering file path
$filePath = New-Object system.Windows.Forms.TextBox
$filePath.multiline = $false
$filePath.width = 300
$filePath.height = 20
$filePath.location = New-Object System.Drawing.Point(200,80)
$filePath.Font = 'Microsoft Sans Serif,10'
# label for the file path text box "File Path: "
$FilePathLabel = New-Object system.Windows.Forms.Label
$FilePathLabel.text = "File Path: "
$FilePathLabel.AutoSize = $true
$FilePathLabel.width = 25
$FilePathLabel.height = 10
$FilePathLabel.location = New-Object System.Drawing.Point(80,85)
$FilePathLabel.Font = 'Microsoft Sans Serif,10'
# Attributes Label
$idLabel = New-Object system.Windows.Forms.Label
$idLabel.text = "Attributes"
$idLabel.AutoSize = $true
$idLabel.width = 25
$idLabel.height = 10
$idLabel.location = New-Object System.Drawing.Point(80,150)
$idLabel.Font = 'Microsoft Sans Serif,12'
# Values Label
$valueLabel = New-Object system.Windows.Forms.Label
$valueLabel.text = "Value"
$valueLabel.AutoSize = $true
$valueLabel.width = 25
$valueLabel.height = 10
$valueLabel.location = New-Object System.Drawing.Point(300,150)
$valueLabel.Font = 'Microsoft Sans Serif,12'
# Checkbox for ID attribute
$fileId = New-Object System.Windows.Forms.CheckBox
$fileId.text = "Id"
$fileId.AutoSize = $true
$fileId.width = 104
$fileId.height = 20
$fileId.location = New-Object System.Drawing.Point(100,200)
$fileId.Font = 'Microsoft Sans Serif,10'
# Label for the ID checkbox
$idValue = New-Object system.Windows.Forms.TextBox
$idValue.multiline = $false
$idValue.width = 300
$idValue.height = 20
$idValue.location = New-Object System.Drawing.Point(202,200)
$idValue.Font = 'Microsoft Sans Serif,10'
# Checkbox for Description attribute
$description = New-Object System.Windows.Forms.CheckBox
$description.text = "Description"
$description.AutoSize = $true
$description.width = 104
$description.height = 20
$description.location = New-Object System.Drawing.Point(100,250)
$description.Font = 'Microsoft Sans Serif,10'
# Label for the Description checkbox
$descriptionValue = New-Object system.Windows.Forms.TextBox
$descriptionValue.multiline = $false
$descriptionValue.width = 300
$descriptionValue.height = 20
$descriptionValue.location = New-Object System.Drawing.Point(202,250)
$descriptionValue.Font = 'Microsoft Sans Serif,10'
# Checkbox for Type attribute
$type = New-Object System.Windows.Forms.CheckBox
$type.text = "Type"
$type.AutoSize = $true
$type.width = 104
$type.height = 20
$type.location = New-Object System.Drawing.Point(100,300)
$type.Font = 'Microsoft Sans Serif,10'
# Label for the type checkbox
$typeValue = New-Object system.Windows.Forms.TextBox
$typeValue.multiline = $false
$typeValue.width = 300
$typeValue.height = 20
$typeValue.location = New-Object System.Drawing.Point(202,300)
$typeValue.Font = 'Microsoft Sans Serif,10'
# Checkbox for silo attribute
$silo = New-Object System.Windows.Forms.CheckBox
$silo.text = "Silo"
$silo.AutoSize = $true
$silo.width = 104
$silo.height = 20
$silo.location = New-Object System.Drawing.Point(100,350)
$silo.Font = 'Microsoft Sans Serif,10'
# Label for the silo checkbox
$siloValue = New-Object system.Windows.Forms.TextBox
$siloValue.multiline = $false
$siloValue.width = 300
$siloValue.height = 20
$siloValue.location = New-Object System.Drawing.Point(202,350)
$siloValue.Font = 'Microsoft Sans Serif,10'
# submitt button
$button = New-Object System.Windows.Forms.Button
$button.text = "Submit"
$button.AutoSize = $true
$button.location = New-Object System.Drawing.Point(250,500)
$button.Font = 'Microsoft Sans Serif,10'
$Form.controls.AddRange(#($mainLabel, $fileId,$filePath,$idLabel,$valueLabel,$FilePathLabel,$idValue,$descriptionValue,$description, $type, $typeValue, $silo, $siloValue, $button))
#region gui events {
function SubmitForm(){
if($fileId.checked -eq $true){
sc -path $filePath.Text -stream $fileId.text -value $idValue.Text
}
if($description.checked -eq $true){
sc -path $filePath.Text -stream $description.text -value $descriptionValue.text
}
if($type.checked -eq $true){
sc -path $filePath.Text -stream $type.text -value $typeValue.text
}
if($silo.checked -eq $true){
sc -path $filePath.Text -stream $silo.text -value $siloValue.text
}
[System.Windows.Forms.MessageBox]::Show("Successfully Added Attributes")
}
#Add Button event
$Button.Add_Click({SubmitForm})
#endregion events }
#endregion GUI }
# logic here
[void]$Form.ShowDialog()
Currently the user would have to actually run the powershell script from the root folder and then add the file path to the text input the GUI, along with the rest of the extended attributes. An example of what I currently have is below:
I would like the user to be able to right click on any file and have the form come up with the path of the file that was right clicked in windows explorer, instead of the path being manually entered by the individual making the updates to the alternate data streams. Something similar as how you would extract a file using zip7 (example below).
Can someone tell me if this is even possible? Should I be trying to tackle this problem in another language than using powershell?
You can do all this with Powershell.
First you want to create a script from your code and make input parameter for chosen folder. Like so:
param($FileName)
<#
This script is a GUI featured way to add extended attributes to files
#>
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
#region begin GUI{
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '600,600'
$Form.text = "Add Extended Attributes"
$Form.TopMost = $false
# Add Extended Attributes Label
$mainLabel = New-Object system.Windows.Forms.Label
$mainLabel.text = "Add Extended Attributes"
$mainLabel.AutoSize = $true
$mainLabel.width = 25
$mainLabel.height = 10
$mainLabel.location = New-Object System.Drawing.Point(180,10)
$mainLabel.Font = 'Microsoft Sans Serif,18'
# text box for entering file path
$filePath = New-Object system.Windows.Forms.TextBox
$filePath.multiline = $false
$filePath.width = 300
$filePath.height = 20
$filePath.location = New-Object System.Drawing.Point(200,80)
$filePath.Font = 'Microsoft Sans Serif,10'
$filePath.Text = $FileName
# label for the file path text box "File Path: "
$FilePathLabel = New-Object system.Windows.Forms.Label
$FilePathLabel.text = "File Path: "
$FilePathLabel.AutoSize = $true
$FilePathLabel.width = 25
$FilePathLabel.height = 10
$FilePathLabel.location = New-Object System.Drawing.Point(80,85)
$FilePathLabel.Font = 'Microsoft Sans Serif,10'
# Attributes Label
$idLabel = New-Object system.Windows.Forms.Label
$idLabel.text = "Attributes"
$idLabel.AutoSize = $true
$idLabel.width = 25
$idLabel.height = 10
$idLabel.location = New-Object System.Drawing.Point(80,150)
$idLabel.Font = 'Microsoft Sans Serif,12'
# Values Label
$valueLabel = New-Object system.Windows.Forms.Label
$valueLabel.text = "Value"
$valueLabel.AutoSize = $true
$valueLabel.width = 25
$valueLabel.height = 10
$valueLabel.location = New-Object System.Drawing.Point(300,150)
$valueLabel.Font = 'Microsoft Sans Serif,12'
# Checkbox for ID attribute
$fileId = New-Object System.Windows.Forms.CheckBox
$fileId.text = "Id"
$fileId.AutoSize = $true
$fileId.width = 104
$fileId.height = 20
$fileId.location = New-Object System.Drawing.Point(100,200)
$fileId.Font = 'Microsoft Sans Serif,10'
# Label for the ID checkbox
$idValue = New-Object system.Windows.Forms.TextBox
$idValue.multiline = $false
$idValue.width = 300
$idValue.height = 20
$idValue.location = New-Object System.Drawing.Point(202,200)
$idValue.Font = 'Microsoft Sans Serif,10'
# Checkbox for Description attribute
$description = New-Object System.Windows.Forms.CheckBox
$description.text = "Description"
$description.AutoSize = $true
$description.width = 104
$description.height = 20
$description.location = New-Object System.Drawing.Point(100,250)
$description.Font = 'Microsoft Sans Serif,10'
# Label for the Description checkbox
$descriptionValue = New-Object system.Windows.Forms.TextBox
$descriptionValue.multiline = $false
$descriptionValue.width = 300
$descriptionValue.height = 20
$descriptionValue.location = New-Object System.Drawing.Point(202,250)
$descriptionValue.Font = 'Microsoft Sans Serif,10'
# Checkbox for Type attribute
$type = New-Object System.Windows.Forms.CheckBox
$type.text = "Type"
$type.AutoSize = $true
$type.width = 104
$type.height = 20
$type.location = New-Object System.Drawing.Point(100,300)
$type.Font = 'Microsoft Sans Serif,10'
# Label for the type checkbox
$typeValue = New-Object system.Windows.Forms.TextBox
$typeValue.multiline = $false
$typeValue.width = 300
$typeValue.height = 20
$typeValue.location = New-Object System.Drawing.Point(202,300)
$typeValue.Font = 'Microsoft Sans Serif,10'
# Checkbox for silo attribute
$silo = New-Object System.Windows.Forms.CheckBox
$silo.text = "Silo"
$silo.AutoSize = $true
$silo.width = 104
$silo.height = 20
$silo.location = New-Object System.Drawing.Point(100,350)
$silo.Font = 'Microsoft Sans Serif,10'
# Label for the silo checkbox
$siloValue = New-Object system.Windows.Forms.TextBox
$siloValue.multiline = $false
$siloValue.width = 300
$siloValue.height = 20
$siloValue.location = New-Object System.Drawing.Point(202,350)
$siloValue.Font = 'Microsoft Sans Serif,10'
# submitt button
$button = New-Object System.Windows.Forms.Button
$button.text = "Submit"
$button.AutoSize = $true
$button.location = New-Object System.Drawing.Point(250,500)
$button.Font = 'Microsoft Sans Serif,10'
$Form.controls.AddRange(#($mainLabel, $fileId,$filePath,$idLabel,$valueLabel,$FilePathLabel,$idValue,$descriptionValue,$description, $type, $typeValue, $silo, $siloValue, $button))
#region gui events {
function SubmitForm(){
if($fileId.checked -eq $true){
sc -path $filePath.Text -stream $fileId.text -value $idValue.Text
}
if($description.checked -eq $true){
sc -path $filePath.Text -stream $description.text -value $descriptionValue.text
}
if($type.checked -eq $true){
sc -path $filePath.Text -stream $type.text -value $typeValue.text
}
if($silo.checked -eq $true){
sc -path $filePath.Text -stream $silo.text -value $siloValue.text
}
[System.Windows.Forms.MessageBox]::Show("Successfully Added Attributes")
}
#Add Button event
$Button.Add_Click({SubmitForm})
#endregion events }
#endregion GUI }
# logic here
[void]$Form.ShowDialog()
Next you would need to create registry reference for context menu item and powershell script according to it. Like so:
New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT
New-Item HKCR:\directory\shell\PowerShellScript
New-Item HKCR:\directory\shell\PowerShellScript\command
Set-ItemProperty 'HKCR:\directory\shell\PowerShellScript\command' -Name '(default)' -Value 'Powershell -WindowStyle Hidden -ExecutionPolicy Bypass -NoExit -File "C:\Test.ps1" "%L"'
Context menu item:
Chosen directory passed to script's input parameter: