Get value back from a text box class - forms

We have a script that was written by someone who is no longer with us. I am new to powershell and this function is not returning a value:
# prompt user for ip/dns address input.
Function get-ip
{
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
# Creates a message box that accepts dns/ip address input.
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "User Input Required"
$objForm.Size = New-Object System.Drawing.Size(300,200)
$objForm.StartPosition = "CenterScreen"
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
{$x=$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({$x=$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,40)
$objLabel.Text = "Please enter the IP address of the server you want to connect to:"
$objForm.Controls.Add($objLabel)
$objTextBox = New-Object System.Windows.Forms.TextBox
$objTextBox.Location = New-Object System.Drawing.Size(10,70)
$objTextBox.Size = New-Object System.Drawing.Size(260,20)
$objForm.Controls.Add($objTextBox)
$objForm.Topmost = $True
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
}
I'm calling it as such:
$ip = get-ip
I enter in a value and I'm not getting anything in the $ip.
How do I capture the value of a text box?
FOR MARTIN
I made the changes that Martin suggested and here is my new code:
# prompt user for ip/dns address input.
Function get-ip
{
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
# Creates a message box that accepts dns/ip address input.
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "User Input Required"
$objForm.Size = New-Object System.Drawing.Size(300,200)
$objForm.StartPosition = "CenterScreen"
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
{$script:x=$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({$script:x=$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,40)
$objLabel.Text = "Please enter the IP address of the server you want to connect to:"
$objForm.Controls.Add($objLabel)
$objTextBox = New-Object System.Windows.Forms.TextBox
$objTextBox.Location = New-Object System.Drawing.Size(10,70)
$objTextBox.Size = New-Object System.Drawing.Size(260,20)
$objForm.Controls.Add($objTextBox)
$objForm.Topmost = $True
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
$x
}
I get the same error message as before:
PS E:\Dropbox\Powershell Scripts\SSS Cloud Icons> .\cloudicons.ps1
Creating Directories...
mkdir : Cannot bind argument to parameter 'Path' because it is an empty string.
At E:\Dropbox\Powershell Scripts\SSS Cloud Icons\cloudicons.ps1:80 char:4
+ md $_.userName
+ ~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [mkdir], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,mkdir
Did I miss something?
** UPDATE **
I was asked how I was calling get-ip:
# Insert user entered IP into pipeline.
$ip = get-ip
# Sets user entered IP into new file called propanehasip.rdp.
(get-content .\propanetest.rdp) -replace 'full address:s:INSERTIPHERE',"full address:s:$ip" | Out-File propanehasip.rdp
Write-Host "Creating Directories..."
Import-Csv $csv | ForEach-Object {
# Creates directories based on the userName field.
md $_.userName
# Creates icons based on propanehasip.rdp and WSID fields.
(get-content ".\propanehasip.rdp") -replace 'remoteapplicationcmdline:s:INSERTWSIDHERE',"remoteapplicationcmdline:s:$($_.wsid1)" | out-file ".\$($_.username)\Propane $($_.wsid1).rdp"
(get-content ".\propanehasip.rdp") -replace 'remoteapplicationcmdline:s:INSERTWSIDHERE',"remoteapplicationcmdline:s:$($_.wsid2)" | out-file ".\$($_.username)\Propane $($_.wsid2).rdp"
}
}
UPDATE #2
I figured out how to debug this in visual studio. The error is happening on this line:
(get-content .\propanetest.rdp) -replace 'full address:s:INSERTIPHERE',"full address:s:$ip" | Out-File propanehasip_new.rdp

$x is only valid inside the block (the scope of variable $x), if you change it to $script:x and add an $x before the ending } of your function, then it works.
The additional $x is needed to represent the return value of your function.
Changes:
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
{$script:x=$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({$script:x=$objTextBox.Text;$objForm.Close()})
$objForm.Controls.Add($OKButton)
End of function:
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
$x
}
And... this script is taken from here and should demonstrate the use of Windows.Forms. However, i didnt found the date, when it was posted.

Related

Create a Powershell ListBox Sticky Notes Backup Utility

I'm building a PowerShell utility that will backup and restore sticky notes using a powershell script to pull the Get-localuser command from a local device into the list box. From list box choose the user profile and it initiates a comand to fetch a file. What im researching is after you select a user in list box, it sends a command to copy a file Thank you.
Example: Admin is the user:
%SystemRoot%\explorer.exe c:\users\Admin\AppData\Local\Packages\Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe\LocalState\pause copy plum.sqlite-wal cd C:\Users\Admin\Documents\SN Utility
working edited: code
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$form = New-Object System.Windows.Forms.Form
$form.Text = 'StickyNotes Utility'
$form.Size = New-Object System.Drawing.Size(300,200)
$form.StartPosition = 'CenterScreen'
$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(10,120)
$okButton.Size = New-Object System.Drawing.Size(75,23)
$okButton.Text = 'OK'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $okButton
$form.Controls.Add($okButton)
$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Point(185,120)
$cancelButton.Size = New-Object System.Drawing.Size(75,23)
$cancelButton.Text = 'Cancel'
$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $cancelButton
$form.Controls.Add($cancelButton)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(200,20)
$label.Text = 'Select a user to backup:'
$form.Controls.Add($label)
$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Location = New-Object System.Drawing.Point(10,40)
$listBox.Size = New-Object System.Drawing.Size(250,20)
$listBox.Height = 80
$listbox.Items.AddRange((get-localuser | select -expand name))
$form.Controls.Add($listBox)
$form.Topmost = $True
do
{
$result = $form.ShowDialog()
if ($ListBox.SelectedIndices.Count -lt 1 -and $result -eq [System.Windows.Forms.DialogResult]::OK)
{
Write-Warning 'Nothing was selected, please select a user.'
}
}
until (($result -eq [System.Windows.Forms.DialogResult]::OK -and $listBox.SelectedIndices.Count -ge 1) -or $result -ne [System.Windows.Forms.DialogResult]::OK)
{
$x = $listBox.SelectedItem
$x
}
directory list inside list box
$subfolders = (Get-ChildItem -Path $rootFolder -Recurse -Directory).FullName
Add-Type -AssemblyName System.Windows.Forms
$form = New-Object System.Windows.Forms.Form
$form.Text = "SubFolders"
$form.Size = New-Object System.Drawing.Size(300,300)
$form.StartPosition = "CenterScreen"
$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Location = New-Object System.Drawing.Point(10,40)
$listBox.Size = New-Object System.Drawing.Size(260,180)
$listBox.Anchor = 'Top,Right,Bottom,Left'
$listbox.Items.AddRange((get-localuser | select -expand name))
$listBox.Add_Click({
$selected = $listBox.GetItemText($listBox.SelectedItem)
[System.Windows.Forms.MessageBox]::Show("You selected subfolder`r`n`r`n$selected", "Subfolder")
})
$form.Controls.Add($listBox)
$form.ShowDialog()
$form.Dispose()
If I understand your question correctly, i believe you want to replace the line
[void] $listBox.Items.AddRange(#("user1", "user2", "user3"))
with
Get-Localuser | select -expand name | foreach {[void] $listbox.Items.Add($_)}
or
$listbox.Items.AddRange((get-localuser | select -expand name))

Powershell GUI script: read variable from textbox

I'm trying to pass the input of the two textboxes to the logic part of the script at the bottom. But that's not happening and I can't figure out the problem... I tried rewriting the code from [here][1].
Add-Type -AssemblyName System.Drawing
$form = New-Object System.Windows.Forms.Form
$form.Text = 'CompareGroups'
$form.Size = New-Object System.Drawing.Size(300,250)
$form.StartPosition = 'CenterScreen'
$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(75,170)
$okButton.Size = New-Object System.Drawing.Size(75,23)
$okButton.Text = 'OK'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $okButton
$form.Controls.Add($okButton)
$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Point(150,170)
$cancelButton.Size = New-Object System.Drawing.Size(75,23)
$cancelButton.Text = 'Cancel'
$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $cancelButton
$form.Controls.Add($cancelButton)
$label1 = New-Object System.Windows.Forms.Label
$label1.Location = New-Object System.Drawing.Point(10,20)
$label1.Size = New-Object System.Drawing.Size(280,20)
$label1.Text = 'Source-PC:'
$form.Controls.Add($label1)
$label2 = New-Object System.Windows.Forms.Label
$label2.Location = New-Object System.Drawing.Point(10,80)
$label2.Size = New-Object System.Drawing.Size(280,20)
$label2.Text = 'Target-PC:'
$form.Controls.Add($label2)
$textBox1 = New-Object System.Windows.Forms.TextBox
$textBox1.Location = New-Object System.Drawing.Point(10,40)
$textBox1.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox1)
$textBox2 = New-Object System.Windows.Forms.TextBox
$textBox2.Location = New-Object System.Drawing.Point(10,100)
$textBox2.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox2)
$form.Topmost = $true
$form.Add_Shown({$textBox1.Select()})
$result = $form.ShowDialog()
if (($result -eq [System.Windows.Forms.DialogResult]::OK)) {
$sourcePc = $textBox.Text
$targetPc = $textBox2.Text
$CopyFromPC = Get-ADComputer $sourcePc -Properties MemberOf
$CopyToPC = Get-ADComputer $targetPc -Properties MemberOf
if (($CopyFromPC.MemberOf | out-string) -eq ($CopyTopc.MemberOf | out-string)) {
Write-Host "Groups are the same."
} else {
$CopyFromPC.MemberOf | Where {$CopyTopc.MemberOf -notcontains $_} | Out-GridView -PassThru | Add-ADGroupMember -Members $CopyTopc -verbose
[System.Windows.Forms.MessageBox]::Show("Groups got copied successfully","CompareGroups",0)
}
}
[1]: https://learn.microsoft.com/en-us/powershell/scripting/samples/creating-a-custom-input-box?view=powershell-7
You added a textbox called textbox1 and not textbox.
Change the variablename from $sourcePc = $textBox.Text to $sourcePc = $textBox1.Text

Use Powershell to simulate right-clicking an Active Directory Group's Properties

Just for clarification, I want the GUI to appear, like the actual window. This is for a side-project I've been trying to work on for a while now the goal was to make a faster way to link a group of users and folder. Since this is a process focusing on saving time, I wanted to have this method of going straight to the properties without having to actively search out the folder.
Ask if more info is needed
Code can be provided (Not sure why you'd need it Id prefer not, but I can be arranged)
I'd REALLY prefer to not use any kind of outside sources or installs. The main goal was to have this powershell script capable of just being put on any ol' computer and have it work. (Not really any old computer but you get my point)
I looked into RUNDLL32 dsquery,OpenQueryWindow as an option, but it didn't give me what I needed.
TL:DR - Pretty much what the title says. I want to be able to run this command and have a specific Security Group's Properties Window appear as if I was in Active Directory myself right clicking on properties.
#######Paths#########
param (
$MYDRIVE_PATH = "\\------\KSD\",
$LOG_FILE_PATH = "C:\Users\Documents\Test_Docs\",
$UserListFile = "C:\Users\Documents\Test_Docs\brenda.txt"
)
##########################
Add-PSSnapin Quest.ActiveRoles.ADManagement
#import-module activedirectory
$LogFile = $LOG_FILE_PATH + "\CreateMyDriveFolderAndPermissions_" + (Get-Date -Format "MM-d-y.h.m.s.ms") + ".log"
$groupRights = [System.Security.AccessControl.FileSystemRights]"ListDirectory, ReadData, WriteData, CreateFiles, CreateDirectories, AppendData, ReadExtendedAttributes, WriteExtendedAttributes, Traverse, ExecuteFile, DeleteSubdirectoriesAndFiles, ReadAttributes, WriteAttributes, Write, ReadPermissions, Read, ReadAndExecute, Synchronize"
$InheritanceFlag = ([System.Security.AccessControl.InheritanceFlags]::ContainerInherit -bor [System.Security.AccessControl.InheritanceFlags]::ObjectInherit)
$PropagationFlag = [System.Security.AccessControl.PropagationFlags]::None
$ErrorCount = 0
$DestinationCheck = 1
$FolderNameCheck = 1
####################################### Option Box #######################################
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Options"
$objForm.Size = New-Object System.Drawing.Size(300,200)
$objForm.StartPosition = "CenterScreen"
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
{$x=$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({$x=$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 = "Options:"
$objForm.Controls.Add($objLabel)
$objBrenda = New-Object System.Windows.Forms.checkbox
$objBrenda.Location = New-Object System.Drawing.Size(10,50)
$objBrenda.Size = New-Object System.Drawing.Size(200,20)
$objBrenda.Checked = $false
$objBrenda.Text = "Brenda Fernandez"
$objForm.Controls.Add($objBrenda)
$objForm.Topmost = $True
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
##############################################################################################
####################################### Destination Box #######################################
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
##### Main Box Creation #####
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "New Folder Destination"
$objForm.Size = New-Object System.Drawing.Size(300,200)
$objForm.StartPosition = "CenterScreen"
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
{$x=$objListBox.SelectedItem;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
{$objForm.Close()}})
###################
##### "Ok" Button Creation #####
$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({$x=$objListBox.SelectedItem;$objForm.Close();$DestinationCheck = 0})
$objForm.Controls.Add($OKButton)
###################
##### "Cancel" Button Creation #####
$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();[System.Environment]::Exit(0)})
$objForm.Controls.Add($CancelButton)
###################
##### Message Right Above Selection Box #####
$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 = "Select a Folder:"
$objForm.Controls.Add($objLabel)
###################
##### List Selection #####
$objListBox = New-Object System.Windows.Forms.ListBox
$objListBox.Location = New-Object System.Drawing.Size(10,40)
$objListBox.Size = New-Object System.Drawing.Size(260,20)
$objListBox.Height = 80
##### Options for Path #####
[void] $objListBox.Items.Add("Department")
[void] $objListBox.Items.Add("Engagement")
[void] $objListBox.Items.Add("Project")
[void] $objListBox.Items.Add("Opportunity")
###################
$objForm.Controls.Add($objListBox)
$objForm.Topmost = $True
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
###Path Creation###
$MYDRIVE_PATH = $MYDRIVE_PATH + $x
###################
$ParentFolder = $x
################################################################################################
####################################### Name Box #######################################
[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")
{$x=$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({$x=$objTextBox.Text;$objForm.Close();$FolderNameCheck = 0})
$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();[System.Environment]::Exit(0)})
$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 = "Enter File Name:"
$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()
$FolderName = $x
#if([string]::IsNullOrEmpty($FolderName)){$FolderNameCheck = 1}
$SecurityName = "US-SG " + $FolderName
################################################################################################
if( $objBrenda.Checked -eq $true ) # Checks if it is a Brenda Fernandez folder and if so adds _QPC to the security group name
{
$SecurityName = $SecurityName + "_QPC"
}
if($FolderNameCheck -eq 0 -and $DestinationCheck -eq 0 ) #Failsafe to make sure all information has been filled out
{
#####Create Security Group#####
$Description = $ParentFolder + "\" + $FolderName
New-QADGroup -Name $SecurityName -samAccountName $SecurityName -Description $Description -GroupScope DomainLocal -ParentContainer "------------"
##################################
$Ar = New-Object system.security.accesscontrol.filesystemaccessrule($SecurityName,$groupRights,$InheritanceFlag, $PropagationFlag,"Allow") #Adds all the security options to the variable $Ar
$UserFolder = New-Item -Path ('{0}\{1}' -f $MYDRIVE_PATH,$FolderName) -ItemType Directory # Creates New Folder
$Acl = get-acl -Path $UserFolder.FullName #Finds location of Folder's Security group
while(1) { #Waits for Security Group to be created and appear in Active Directory
if( $Acl.SetAccessRule($Ar) -eq $NULL ){ break; } ########### Once Completed it will set the $Ar to $Acl and break out of the loop
Start-Sleep -s 1
}
$Acl | Set-Acl $UserFolder.FullName #####Set security group to user folder
##################### Notification Popup ##########################
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$objNotifyIcon = New-Object System.Windows.Forms.NotifyIcon
$objNotifyIcon.Icon = "C:\Users\Downloads\success.ico"
$objNotifyIcon.BalloonTipIcon = "Info"
$objNotifyIcon.BalloonTipText = "The folder " + $FolderName + " has been successfully created."
$objNotifyIcon.BalloonTipTitle = "Proccess Complete"
$objNotifyIcon.Visible = $True
$objNotifyIcon.ShowBalloonTip(10000)
Start-Sleep -s 1
$objNotifyIcon.Dispose()
##############################################################################################
if( $objBrenda.Checked -eq $true )
{
$DPText = "US\" + $SecurityName + "_QPC;"
Add-Content $UserListFile $DPText
}
}
if ($ErrorCount -eq 0)
{
Write-Host "Done." -ForegroundColor Green
}
else{
Write-Host "Done. ($ErrorCount)" -ForegroundColor Red
}
<#
if(() -ne $Null)
{
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$objNotifyIcon = New-Object System.Windows.Forms.NotifyIcon
$objNotifyIcon.Icon = "C:\Users\Downloads\success.ico"
$objNotifyIcon.BalloonTipIcon = "Error"
$objNotifyIcon.BalloonTipText = "The security group " + $FolderName + " already exists."
$objNotifyIcon.BalloonTipTitle = "Something went horribly wrong!"
$objNotifyIcon.Visible = $True
$objNotifyIcon.ShowBalloonTip(10000)
Start-Sleep -s 1
$objNotifyIcon.Dispose()
exit
}
#>
#[Windows.Clipboard]::SetText("Meow")
I think you're getting WAY too complicated. I would try using the ActiveDirectory module installed with RSAT (Remote Server Administration Tools). Don't be afraid to leave the GUI, it's not that scary once you get used to it. :)
Really, I mean you could probably do this with a one-liner.

Modify this Powershell custom input box to store multiple variables?

This textbox was a powershell tip of the week and i really like it.
But I didn't manage to work with it to have 2 variables or even 3 in one input window.
Does anyone know how to do this ? Any help is upvoted immediately.
The Code of the Textbox is here : Input Textbox
The full code is here :
[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")
{$x=$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({$x=$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 information 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()
$x
You need to repeat adding a few more text boxes:
$OKButton.Add_Click({
$x=$objTextBox.Text
$y=$objTextBox2.Text
$z=$objTextBox3.Text
$objForm.Close()})
...
$objTextBox2 = New-Object System.Windows.Forms.TextBox
$objTextBox2.Location = New-Object System.Drawing.Size(10,65)
$objTextBox2.Size = New-Object System.Drawing.Size(260,20)
$objForm.Controls.Add($objTextBox2)
$objTextBox3 = New-Object System.Windows.Forms.TextBox
$objTextBox3.Location = New-Object System.Drawing.Size(10,90)
$objTextBox3.Size = New-Object System.Drawing.Size(260,20)
$objForm.Controls.Add($objTextBox3)
...
$x,$y,$z
You might need to adjust the Y locations of the OK & Close buttons.

Pass parameters from a input box to stored procedure using powershell-please read the whole thing, I know it's long

Hi have a script to create a input box using powershell.
It looks like this
[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")
{$x=$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({$x=$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 #from date:"
$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()
$from
Now I want to pass the value $from I input into stored procedure parameter #from, I tried below but not working. Any suggestion?
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=localhost;Database=AMSDataWarehouse Test;Integrated Security=SSPI"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$SqlCmd.CommandText = "YQBreport1"
$cmd.Parameters.Add("#from", $from)| Out-Null
$Command.ExecuteNonQuery()
$from = $Command.Parameters["#from"].value
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$SqlConnection.Close()
$SQLResult =$DataSet.Tables[0]
$commands = $SQLResult | foreach-object -process { $_.output }> output.ps1
.\output.ps1
The line:
$OKButton.Add_Click({$x=$objTextBox.Text;$objForm.Close()})
Only works within the Add_click functionality. To make it accessible elsewhere you need to alter it slightly to:
$OKButton.Add_Click({$Global:x=$objTextBox.Text;$objForm.Close()})
$x is then a global parameter that you can pass to your procedure.
Yeah you can make a GUI in PowerShell using Windows Presentation Foundation (WPF). I've tried and failed.
Read in values like this:
$from = Read-Host '#From'
$from
Out-GridView is something new I just learned. You should look into it.
ls | Out-GridView -PassThru -Title 'GUI?'
Nice example how to create an input box in PowerShell (using Windows Forms)
http://technet.microsoft.com/en-us/library/ff730941.aspx