Good Morning Everyone,
PROBLEM: Can't seem to get a value to return out of my function and be persistent.
Ok, I've read a bunch of different posts on here and I am still at a loss as to what is wrong with my script. I've used two other scripts together to get (close to) the results I'm looking for. I hope I'm probably missing something very basic here.
Overall Goal - Have the user select the client from the drop-down and then input a job # to create a standard folder structure we use for all client projects.
3 parts to this script.
Part 1 is to put the folder list of a directory in an array and return that list as a drop down menu. (Borrowed from http://www.powershell.nu/2009/01/21/dropdown-menu-using-windowsforms/)
Part 2 is to ask the user for input for a Job # (Borrowed from http://technet.microsoft.com/en-us/library/ff730941.aspx)
Part 3 is to use the information from parts 1 & 2 to create the folder structure.
From what I can tell, the ONLY thing that isn't working is getting the value the user selects from the drop-down into a variable I can then use to create the folder structure. When I put poor man's debugging in the script and write-host $Client it will print the value selected from the drop down on the screen, but in the very next line that uses that variable the value of the variable seems to be blank. I have tried adding this to my function:
I've tried adding return $Client before and after $Form.Close
I've also tried defining $Client in the start of the function like this function Return-Dropdown($Client)
I've tried a massive jumble of different things and the closest I've gotten is adding $Client = Return-Dropdown right before the part of the script that creates the folder structure, but it is still a blank value, even if I put the poor man's debugging in the script and write-host $Client (which returns the correct value to the screen), the script then acts like the variable $Client is blank when it starts the section with all the New-Item commands.
Anyway, thanks in advance for taking a look.
# Define the path where I want dir to be run for my drop-down list. This path # will be used later to create the folder structure. $Path = "c:\PS\TestFolder\" # Change my present working directory to $Path so dir returns the desired results cd $Path [array]$DropDownArray = dir # This Function Returns the Selected Value and Closes the Form function Return-DropDown($Client){ $Choice=$DropDown.SelectedItem.ToString() $Form.Close() Write-Host $Choice } # This is the GUI input for the client name. [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") $Form = New-Object System.Windows.Forms.Form $Form.width = 300 $Form.height = 150 $Form.Text = ”Client List” $Form.StartPosition = "CenterScreen" $DropDown = new-object System.Windows.Forms.ComboBox $DropDown.Location = new-object System.Drawing.Size(100,10) $DropDown.Size = new-object System.Drawing.Size(130,30) ForEach ($Item in $DropDownArray) { $DropDown.Items.Add($Item) } $Form.Controls.Add($DropDown) $DropDownLabel = new-object System.Windows.Forms.Label $DropDownLabel.Location = new-object System.Drawing.Size(10,10) $DropDownLabel.size = new-object System.Drawing.Size(100,20) $DropDownLabel.Text = "Clients" $Form.Controls.Add($DropDownLabel) $Button = new-object System.Windows.Forms.Button $Button.Location = new-object System.Drawing.Size(100,50) $Button.Size = new-object System.Drawing.Size(100,20) $Button.Text = "Select Client" $Button.Add_Click({Return-DropDown}) $Form.Controls.Add($Button) $Form.Add_Shown({$Form.Activate()}) $Form.ShowDialog() # This is the GUI input for the job number. [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") $objForm = New-Object System.Windows.Forms.Form $objForm.Text = "Data Entry Form" $objForm.Size = New-Object System.Drawing.Size(300,200) $objForm.StartPosition = "CenterScreen" $objForm.KeyPreview = $True $objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") {$Job=$objTextBox.Text;$objForm.Close()}}) $objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") {$objForm.Close()}}) $OKButton = New-Object System.Windows.Forms.Button $OKButton.Location = New-Object System.Drawing.Size(75,120) $OKButton.Size = New-Object System.Drawing.Size(75,23) $OKButton.Text = "OK" $OKButton.Add_Click({$Job=$objTextBox.Text;$objForm.Close()}) $objForm.Controls.Add($OKButton) $CancelButton = New-Object System.Windows.Forms.Button $CancelButton.Location = New-Object System.Drawing.Size(150,120) $CancelButton.Size = New-Object System.Drawing.Size(75,23) $CancelButton.Text = "Cancel" $CancelButton.Add_Click({$objForm.Close()}) $objForm.Controls.Add($CancelButton) $objLabel = New-Object System.Windows.Forms.Label $objLabel.Location = New-Object System.Drawing.Size(10,20) $objLabel.Size = New-Object System.Drawing.Size(280,20) $objLabel.Text = "Please enter the job number in the space below:" $objForm.Controls.Add($objLabel) $objTextBox = New-Object System.Windows.Forms.TextBox $objTextBox.Location = New-Object System.Drawing.Size(10,40) $objTextBox.Size = New-Object System.Drawing.Size(260,20) $objForm.Controls.Add($objTextBox) $objForm.Topmost = $True $objForm.Add_Shown({$objForm.Activate()}) [void] $objForm.ShowDialog() $Client = Return-DropDown Write-Host $Client #This section builds the folder structure using the input from above. New-Item "$Path\Crucible\$Client" -type directory New-Item "$Path\Crucible\$Client\Client Fonts" -type directory New-Item "$Path\Crucible\$Client\Client Images" -type directory Move-item "$Path\Crucible\$Client" $Path -Force New-Item "$Path\Forge\$Job\$Client-$Job Account Management" -type directory New-Item "$Path\Forge\$Job\$Client-$Job Creative\Latest PDF\Drafts" -type directory New-Item "$Path\Forge\$Job\$Client-$Job Creative\Layout\Drafts" -type directory New-Item "$Path\Forge\$Job\$Client-$Job Creative\Copy\Drafts" -type directory New-Item "$Path\Forge\$Job\$Client-$Job Creative\Fonts" -type directory New-Item "$Path\Forge\$Job\$Client-$Job Creative\Links" -type directory New-Item "$Path\Forge\$Job\$Client-$Job Creative\Production" -type directory New-Item "$Path\Forge\$Job\$Client-$Job Development" -type directory New-Item "$Path\Forge\$Job\$Client-$Job Project Management" -type directory Move-Item "$Path\Forge\$Job" "$Path\$Client" -Force
M C Richardson Remarqable IT