How to prompt for restart based on uptime? - powershell

I am trying to create a powershell script that will prompt the user to restart their computer based on their uptime. Preferably I want it to prompt on day 5 and force a restart on day 7.
This script
(get-date)-([System.Management.ManagementDateTimeconverter]::ToDateTime((Get-WmiObject win32_operatingsystem).lastbootuptime))|select days
Gives me the uptime days and if it returns an amount greater than 5 I want it to run
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Shutdown | Restart"
$objForm.Size = New-Object System.Drawing.Size(300,300)
$objForm.StartPosition = "CenterScreen"
$objForm.FormBorderStyle = "FixedSingle"
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
{$objForm.Close()}})
$SButton = New-Object System.Windows.Forms.Button
$SButton.Location = New-Object System.Drawing.Size(15,220)
$SButton.Size = New-Object System.Drawing.Size(75,25)
$SButton.Text = "Shutdown"
$SButton.Add_Click({Stop-Computer -Force})
$objForm.Controls.Add($SButton)
$RButton = New-Object System.Windows.Forms.Button
$RButton.Location = New-Object System.Drawing.Size(110,220)
$RButton.Size = New-Object System.Drawing.Size(75,25)
$RButton.Text = "Restart"
$RButton.Add_Click({Restart-Computer -Force})
$objForm.Controls.Add($RButton)
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(205,220)
$CancelButton.Size = New-Object System.Drawing.Size(75,25)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)
$outlook = #(Get-Process | ?{ $_.Name -eq "outlook" }).Count
$word = #(Get-Process | ?{ $_.Name -eq "winword" }).Count
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20)
$objLabel.Size = New-Object System.Drawing.Size(280,120)
$objLabel.Text = "Please ensure you have saved your work before proceeding.`n`nYou currently have $outlook Outlook and $word Word windows open."
$objForm.Controls.Add($objLabel)
$objForm.Topmost = $True
$objForm.Add_Shown({$objForm.Activate()})
And if possible I want it to force the restart if it's greater than 7

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 containing multiple ListBoxes

I am trying to modify the code found in the Microsoft Documentation (https://learn.microsoft.com/en-us/powershell/scripting/samples/multiple-selection-list-boxes?view=powershell-7.2) to contain 2 ListBoxes: One single select, and one multiple selected.
This issue is only listBox2 is showing up.
What I have so far:
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Data Entry Form'
$form.Size = New-Object System.Drawing.Size(600,200)
$form.StartPosition = 'CenterScreen'
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(75,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(150,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)
#Single Select Start
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'Select Primary USB from the list below:'
$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(260,20)
#Single Select End
#Multiple Item Select Start
$label2 = New-Object System.Windows.Forms.Label
$label2.Location = New-Object System.Drawing.Point(310,20)
$label2.Size = New-Object System.Drawing.Size(280,20)
$label2.Text = 'Select Secondary USB(s) from the list below:'
$form.Controls.Add($label2)
$listBox2 = New-Object System.Windows.Forms.Listbox
$listBox2.Location = New-Object System.Drawing.Point(310,40)
$listBox2.Size = New-Object System.Drawing.Size(260,20)
$listBox2.SelectionMode = 'MultiExtended'
#Multiple Item Select End
$testArray = gdr
ForEach($n in $testArray){
if(($n.Root.Length -lt 4) -And ($n.Root.Length -gt 0) -And ($n.Root -ne "\") -And ($n.Root -ne "C:\")){
[void] $listBox.Items.Add($n.Root)
[void] $listBox2.Items.Add($n.Root)
}
}
$listBox.Height = 70
$listBox2.Height = 70
$form.Controls.Add($listBox1)
$form.Controls.Add($listBox2)
$form.Topmost = $true
$result = $form.ShowDialog()
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
$x = $listBox1.SelectedItem
$y = $listBox2.SelectedItems
Write-Host "Single Item Selected" $x
Write-Host "Multiple Items Selected" $y
}
P.S. Can someone please make a new tag for PowerShell-GUI
istbox1 doesn't exist....change:
$form.Controls.Add($listBox1)
to
$form.Controls.Add($listBox)
Or rename the object to $listbox1

Add an existing Progressbar into an GUI

I have writen an simple GUI for my command to display. When i press a button my script searches for matches in a Log File and while doing that it displays a Progressbar, but it only displays it in the ISE Window not in the GUI itself. How can i Display it in the GUI.
I found New-Object System.Windows.Forms.ProgressBar when searching for a way.
But in the examples i only found how they do new Bars not add existings that only exist inside of a Button.
This is my Script
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
#Dropdown/Serverauswahl
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Select a Computer'
$form.Size = New-Object System.Drawing.Size(600,400)
$form.StartPosition = 'CenterScreen'
$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(150,240)
$okButton.Size = New-Object System.Drawing.Size(150,46)
$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(300,240)
$cancelButton.Size = New-Object System.Drawing.Size(150,46)
$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(560,40)
$label.Text = 'Please select a Server:'
$form.Controls.Add($label)
$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Location = New-Object System.Drawing.Point(20,80)
$listBox.Size = New-Object System.Drawing.Size(540,40)
$listBox.Font = "courier New, 13"
$listBox.Height =150
[void] $listBox.Items.Add('LNS5')
[void] $listBox.Items.Add('LNS10')
[void] $listBox.Items.Add('LNS13')
[void] $listBox.Items.Add('LNS14')
[void] $listBox.Items.Add('LNS62')
$form.Controls.Add($listBox)
$form.Topmost = $true
$result = $form.ShowDialog()
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
$x = $listBox.SelectedItem
$path = "C:\temp\SMTPFilter\${x}filter.txt"
}
#zweites Fenster
$objForm = New-Object System.Windows.Forms.Form
$objForm.StartPosition = "CenterScreen"
$objForm.Size = New-Object System.Drawing.Size(1200,800)
$objForm.Text = "Test GUI"
$form = New-Object System.Windows.Forms.Form
$form.Size = New-Object System.Drawing.Size(900,600)
$dataGridView = New-Object System.Windows.Forms.DataGridView
$dataGridView.Size=New-Object System.Drawing.Size(800,400)
#Filtern
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(30,112)
$OKButton.Size = New-Object System.Drawing.Size(300,92)
$OKButton.Text = "Filtern"
$OKButton.Name = "Filter"
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::None
$OKButton.Add_Click({$i= 0
$length = (Get-Content $path).Length
$global:result = Get-Content $path | ForEach-Object {
if($_ -match '(\d{2}\.\d{2}\.\d{4} \d{2}:\d{2}:\d{2}).*\(((?:\d{1,3}\.){3}\d{1,3})\) disconnected\.?\s+(\d+) message\[s\]'){
[PsCustomObject]#{
IP = $matches[2]
Messages = [int]$matches[3]
Date = [datetime]::ParseExact($matches[1], 'dd.MM.yyyy HH:mm:ss', $null)
}}
$i++
if($i % 1000 -eq 0){
Write-Progress -activity "Searching for matches" -status "Scanned: $i of $($length)" -percentComplete (($i / $length) * 100)
}}
Write-Progress -activity "Searching for matches" -status "Scanned: $i of $($length)" -percentComplete (($i / $length) * 100)
#Messages Counted
$global:cumulative = $result | Group-Object -Property IP | ForEach-Object {
try {
$dns = [System.Net.Dns]::GetHostEntry($_.Name).HostName
}
catch {
$dns = 'Not available'
}
[PsCustomObject]#{
IP = $_.Name
Messages = ($_.Group | Measure-Object -Property Messages -Sum).Sum
DNSName = $dns
Date = ($_.Group | Sort-Object Date)[-1].Date
}
}})
$objForm.Controls.Add($OKButton)
#Ergebnis Anzeigen
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(30,214)
$OKButton.Size = New-Object System.Drawing.Size(300,92)
$OKButton.Text = "Ergebnis anzeigen"
$OKButton.Name = "Egebnis Button"
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::None
$OKButton.Add_Click({$objTextBox1 = New-Object System.Windows.Forms.TextBox
$objTextBox1.Multiline = $True;
$objTextBox1.Location = New-Object System.Drawing.Size(360,10)
$objTextBox1.Size = New-Object System.Drawing.Size(800,600)
$objTextBox1.Text = $cumulative | Out-String
$objTextBox1.Font = "courier New, 13"
$objTextBox1.Scrollbars = "Vertical"
$objForm.Controls.Add($objTextBox1)
#outgridview
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(30,316)
$OKButton.Size = New-Object System.Drawing.Size(300,92)
$OKButton.Text = "Ergebnis in GridView"
$OKButton.Name = "GridView"
$OKButton.DialogResult = "OK"
$OKButton.Add_Click({$cumulative | Out-GridView})
$objForm.Controls.Add($OKButton)
#Export CSV
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(30,418)
$OKButton.Size = New-Object System.Drawing.Size(300,92)
$OKButton.Text = "Export CSV (in C:/temp)"
$OKButton.Name = "CSV"
$OKButton.DialogResult = "OK"
$OKButton.Add_Click({$cumulative | Export-Csv -Path 'C:\temp\SMTPresult.Csv'})
$objForm.Controls.Add($OKButton) })
$objForm.Controls.Add($OKButton)
[void] $objForm.ShowDialog()
There is a ProgressBar control available which you can use:
System.Windows.Forms.ProgressBar
See https://mcpmag.com/articles/2014/02/18/progress-bar-to-a-graphical-status-box.aspx for an example
There is also a way of displaying a progressbar inside the taskbar button but you need to deploy a dll from the Microsoft WindowsAPICodePack so that it's available to the person running the script. If you're interested I'll dig out the details

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.