To make it easier for new employees to connect to Exchange via powershell im writting a quick little tools using powershell:winforms.
The tool will just contain a couple of buttons, text field, en informative text so users can manage the Exchange envoirement from one UI.
#1 Set Credentials Button
$Set_Credential = New-Object System.Windows.Forms.Button
$Set_Credential.Top = "5"
$Set_Credential.Left = "5"
$Set_Credential.Anchor = "Left,Top"
$Set_Credential.Text = 'Set Credentials'
$Set_Credential.add_Click({
$UserCredential = Get-Credential
})
$Exchange_Manager.Controls.Add($Set_Credential)
#3 Connect to Exchange Online
$Exchange_Connect = New-Object System.Windows.Forms.Button
$Exchange_Connect.Top = "5"
$Exchange_Connect.Left = "150"
$Exchange_Connect.Anchor ="Left,Top"
$Exchange_Connect.Text = 'Connect'
$Exchange_Connect.add_Click({
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential "$UserCredential" -Authentication Basic -AllowRedirection
Import-PSSession $Session
})
$Exchange_Manager.Controls.Add($Exchange_Connect)
As you can see the first 'Set Credentials' button allows for users to enter credentials via the default get-credentials.
The Second button 'Connect to Exchange' starts a new PSSession using the $UserCredentials to login.
The issue i'm facing is that when pressing the connect button i get an error that says the -credential value is Null.
Querying the $UserCredential also gives an empty line.
It seems that altrough the credentials get entered they dont get stored because they are called trough an button function.
I tried looking it up but most of the anwsers are quite vague.
Ful code:
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$Exchange_Manager = New-Object Windows.Forms.Form
#Session Initation 1
#1 Set Credentials Button
$Set_Credential = New-Object System.Windows.Forms.Button
$Set_Credential.Top = "5"
$Set_Credential.Left = "5"
$Set_Credential.Anchor = "Left,Top"
$Set_Credential.Text = 'Set Credentials'
$Set_Credential.add_Click({
$UserCredential = Get-Credential
})
$Exchange_Manager.Controls.Add($Set_Credential)
#2 Current Credentials
#3 Connect to Exchange Online
$Exchange_Connect = New-Object System.Windows.Forms.Button
$Exchange_Connect.Top = "5"
$Exchange_Connect.Left = "150"
$Exchange_Connect.Anchor ="Left,Top"
$Exchange_Connect.Text = 'Connect'
$Exchange_Connect.add_Click({
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential "$UserCredential" -Authentication Basic -AllowRedirection
Import-PSSession $Session
})
$Exchange_Manager.Controls.Add($Exchange_Connect)
#Information
#1 Set Credentials Button
$Mailboxes = New-Object System.Windows.Forms.Button
$Mailboxes.Top = "50"
$Mailboxes.Left = "5"
$Mailboxes.Anchor = "Left,Top"
$Mailboxes.Text = 'Get All Mailboxes'
$Mailboxes.add_Click({
Get-Mailbox |Select-Object DisplayName, Alias | Sort-Object -Property DisplayName | Out-gridview
})
$Exchange_Manager.Controls.Add($Mailboxes)
$Exchange_Manager.ShowDialog()
You need to make $UserCredential a global.
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$Exchange_Manager = New-Object Windows.Forms.Form
$Exchange_Manager.TopMost = $true
# create a global empty credential object
$Global:UserCredential = [System.Management.Automation.PSCredential]::Empty
#Session Initation 1
#1 Set Credentials Button
$Set_Credential = New-Object System.Windows.Forms.Button
$Set_Credential.Top = "5"
$Set_Credential.Left = "5"
$Set_Credential.Anchor = "Left,Top"
$Set_Credential.Text = 'Set Credentials'
$Set_Credential.add_Click({
$Global:UserCredential = Get-Credential
})
$Exchange_Manager.Controls.Add($Set_Credential)
#2 Current Credentials
#3 Connect to Exchange Online
$Exchange_Connect = New-Object System.Windows.Forms.Button
$Exchange_Connect.Top = "5"
$Exchange_Connect.Left = "150"
$Exchange_Connect.Anchor ="Left,Top"
$Exchange_Connect.Text = 'Connect'
$Exchange_Connect.add_Click({
if ($Global:UserCredential -ne [System.Management.Automation.PSCredential]::Empty) {
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential "$Global:UserCredential" -Authentication Basic -AllowRedirection
Import-PSSession $Session
}
})
$Exchange_Manager.Controls.Add($Exchange_Connect)
#4 Show the dialog and dispose of it afterwards
$Exchange_Manager.ShowDialog()
$Exchange_Manager.Dispose()
Related
function CalendarShare {
Add-MailboxFolderPermission -Identity ${FromUser.Text} -AccessRights Editor -User ${ToUser.Text}
}
When the program is running, it works until it processes the share calendar button. It states that it
cannot bind the argument parameter identity because it is null.
I have no idea what can cause this
Full code:
Add-Type -AssemblyNAme System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
#Declare Functions
function login {
$LiveCred = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic -AllowRedirection
Import-PSSession $Session
$btnlogin.Visible = $False
}
function quitprogram {
$Form.Close()
Exit-PSSession []
}
function CalendarShare {
Add-MailboxFolderPermission -Identity ${FromUser.Text} -AccessRights Editor -User ${ToUser.Text}
}
#Creates base form
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '400,400'
$Form.Text = 'Powershell GUI'
$Form.TopMost = $False
#Creates Login button
$btnlogin = New-Object System.windows.Forms.button
$btnlogin.text = 'Login'
$btnlogin.width = 80
$btnlogin.height = 40
$btnlogin.location = New-Object System.Drawing.Point(150,5)
#Add_Click defines what to do on click
$btnlogin.Add_Click( {
login #function previously defined. Line 6
})
#Creates label for From user
$FromUserLabel = New-Object system.Windows.Forms.Label
$FromUserLabel.text = 'Share Calendar From User'
$FromUserLabel.AutoSize = $true
$FromUserLabel.width = 25
$FromUserLabel.height = 10
$FromUserLabel.location = New-Object System.Drawing.Point(130,100) #Define where it appears on the map
#Create first input. From User
$FromUser = New-Object system.Windows.Forms.TextBox
$FromUser.multiline = $False
$FromUser.width - 200
$FromUser.height = 20
$FromUser.location = New-Object System.Drawing.Point(135,125)
#Create label for To user
$ToUserLabel = New-Object system.Windows.Forms.Label
$ToUserLabel.text = 'Share Calendar To User'
$ToUserLabel.AutoSize = $True
$ToUserLabel.width = 25
$ToUserLabel.height = 10
$ToUserLabel.location = New-Object System.Drawing.Point(135,175)
#Create second input. To User
$ToUser = New-Object system.Windows.Forms.TextBox
$ToUser.multiline = $False
$ToUser.width - 200
$ToUser.height = 20
$ToUser.location = New-Object System.Drawing.Point(135,200)
#Create Share Button Calendar
$btnsharecalendar = New-Object System.windows.Forms.button
$btnsharecalendar.text = 'Share Calendar'
$btnsharecalendar.width = 165
$btnsharecalendar.height = 30
$btnsharecalendar.location = New-Object System.Drawing.Point(105,275)
#Define Add_Click below
$btnsharecalendar.Add_Click({
CalendarShare
})
#Create button to close program
$btnquit = New-Object System.windows.Forms.button
$btnquit.text = 'Quit'
$btnquit.width = 80
$btnquit.height = 40
$btnquit.location = New-Object System.Drawing.Point(150,325)
#Add_Click defines what to do on click
$btnquit.Add_Click( {
quitprogram #function previously defined. Line 12
})
#Allows form to work
$Form.Controls.AddRange(#($btnlogin,$FromUserLabel,$FromUser,$ToUserLabel,$ToUser,$btnsharecalendar,$btnquit))
$Form.ShowDialog()
Note: This is assuming that both $FromUser and $ToUser are textboxes
You aren't using your variable correctly for the identity parameter, should be:
function CalendarShare {
Add-MailboxFolderPermission -Identity $FromUser.Text -AccessRights Editor -User $ToUser.Text}
}
I don't want to run script for each terminated user, I assume I need to use do while loop for this so the it will continually loop over and over again until the ESC option.
I have been trying to do this in powershell, but I am new to it and am still learning. Do you have any idea?
$wshell = New-Object -ComObject Wscript.Shell
$wshell.Popup("Please enter in your Domain Admin credentials. Please remember it should be in the form of DOMAIN\username.",0,"Credentials Needed!",0x0)
$creds = Get-Credential
$PSDefaultParameterValues = #{"*-AD*:Credential"=$creds}
#Here we create the connection to the exchange server. Edit with your mailserver info
$ExchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://**editmemithyourwebmailservername**/PowerShell
Import-PSSession $ExchangeSession
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Windows.Forms.Application]::EnableVisualStyles()
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Terminated Employee Process Form"
$objForm.Size = New-Object System.Drawing.Size(500,400)
$objForm.StartPosition = "CenterScreen"
$objForm.MaximizeBox = $False
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
{$userinput=$UserTextBox.Text;$forwardemail=$ForwardingTextBox.Text;$ticketnumber=$TicketTextBox.Text;$disableuser=$DisableUserCheckbox.Checked;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
{$objForm.Close()}})
$Font = New-Object System.Drawing.Font("Verdana",8,[System.Drawing.FontStyle]::Bold)
#$objForm.Font = $Font
#VERSION NUMBER
$VersionLabel = New-Object System.Windows.Forms.Label
$VersionLabel.Location = New-Object System.Drawing.Size(450,10)
$VersionLabel.Size = New-Object System.Drawing.Size(120,20)
$VersionLabel.Font = $Font
$VersionLabel.Text = "V1"
$objForm.Controls.Add($VersionLabel)
#OK AND CANCEL BUTTONS
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,320)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({$userinput=$UserTextBox.Text;$ticketnumber=$TicketTextBox.Text;$forwardemail=$ForwardingTextBox.Text;$disableuser=$DisableUserCheckbox.Checked;$objForm.Close()})
$objForm.Controls.Add($OKButton)
#USERNAME LABEL
$UserLabel = New-Object System.Windows.Forms.Label
$UserLabel.Location = New-Object System.Drawing.Size(10,20)
$UserLabel.Size = New-Object System.Drawing.Size(280,20)
$UserLabel.Text = "Username of Terminated Employee"
$objForm.Controls.Add($UserLabel)
#USERNAME TEXT BOX
$UserTextBox = New-Object System.Windows.Forms.TextBox
$UserTextBox.Location = New-Object System.Drawing.Size(10,40)
$UserTextBox.Size = New-Object System.Drawing.Size(180,20)
$objForm.Controls.Add($UserTextBox)
#DISABLE USER CHECKBOX CONTROL
$DisableUserCheckbox = New-Object System.Windows.Forms.Checkbox
$DisableUserCheckbox.Location = New-Object System.Drawing.Size(220,30)
$DisableUserCheckbox.Size = New-Object System.Drawing.Size(120,40)
$DisableUserCheckbox.Text = "Disable The User?"
$objForm.Controls.Add($DisableUserCheckbox)
#FORWARD EMAIL LABEL
$FowardEmailLabel = New-Object System.Windows.Forms.Label
$FowardEmailLabel.Location = New-Object System.Drawing.Size(10,80)
$FowardEmailLabel.Size = New-Object System.Drawing.Size(280,20)
$FowardEmailLabel.Text = "Forward Email to Manager? If Yes, Type In Email Address"
$objForm.Controls.Add($FowardEmailLabel)
#FORWARD EMAIL TEXT BOX
$ForwardingTextBox = New-Object System.Windows.Forms.TextBox
$ForwardingTextBox.Location = New-Object System.Drawing.Size(10,100)
$ForwardingTextBox.Size = New-Object System.Drawing.Size(180,40)
$objForm.Controls.Add($ForwardingTextBox)
#ENTER TICKET NUMBER TEXT LABEL
$TicketLabel = New-Object System.Windows.Forms.Label
$TicketLabel.Location = New-Object System.Drawing.Size(10,150)
$TicketLabel.Size = New-Object System.Drawing.Size(80,20)
$TicketLabel.Text = "Issue Number"
$objForm.Controls.Add($TicketLabel)
$TicketTextBox = New-Object System.Windows.Forms.TextBox
$TicketTextBox.Location = New-Object System.Drawing.Size(10,170)
$TicketTextBox.Size = New-Object System.Drawing.Size(40,250)
$objForm.Controls.Add($TicketTextBox)
#CANCEL BUTTONS
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(350,320)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close(); $cancel = $true})
$objForm.Controls.Add($CancelButton)
$objForm.Topmost = $True
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
if ($cancel) {return}
#$OKButton.Add_Click({$userinput=$UserTextBox.Text;$ticketnumber=$TicketTextBox.Text;$forwardemail=$ForwardingTextBox.Text;$disableuser=$DisableUserCheckbox.Checked;$objForm.Close()})
#$CancelButton.Add_Click({$objForm.Close()})
#COMMON GLOBAL VARIABLES
$disableusercheckbox=$DisableUserCheckbox.Checked
$userinput=$UserTextBox.Text
$forwardemail=$ForwardingTextBox.Text
$ticketnumber=$TicketTextBox.Text
$Month = Get-Date -format MM
$Day = Get-Date -format dd
$Year = Get-Date -format yyyy
If ($OKButton.Add_Click) {
########
#ACTIVE DIRECTORY ACTIONS
#########
#DISABLE THE USER
If ($disableusercheckbox -eq $true)
{
Disable-ADAccount -Identity $userinput
$disabled = $userinput + " has been disabled"
} else {
$notdisabled = $userinput + " has not been disabled at this time"
}
#GETS ALL GROUPS USER WAS PART OF BEFORE BLOWING THEM OUT
$User = $userinput
$List=#()
$Groups = Get-ADUser -Identity $User -Properties * | select -ExpandProperty memberof
foreach($i in $Groups){
$i = ($i -split ',')[0]
$List += "`r`n" + ($i -creplace 'CN=|}','')
}
#BLOW OUT GROUPS OF USER EXCEPT DOMAIN USERS
(get-aduser $userinput -properties memberof).memberof|remove-adgroupmember -member $userinput -Confirm:$False
#SETS THE USERS TITLE,COMPANY/MANAGER TO DISABLED
set-aduser -identity $userinput -title "CompanyName - Disabled $Month/$Day/$Year"
set-aduser -identity $userinput -company $null
set-aduser -identity $userinput -manager $null
set-aduser -identity $userinput -department $null
set-aduser -identity $userinput -description "CompanyName - Disabled $Month/$Day/$Year per Issue# $ticketnumber"
#CHANGES THE USERS PASSWORD
$newpwd = ConvertTo-SecureString -String "G00dBye#1234" -AsPlainText –Force
Set-ADAccountPassword $userinput –NewPassword $newpwd -Reset
#MOVES THE USER TO DISABLED USERS
Get-ADUser -Filter { samAccountName -like $userinput } | Move-ADObject –TargetPath "OU=Disabled Users,OU=User Accounts,DC=domain,DC=com"
#HIDES USER FROM GLOBAL ADDRESS BOOK and configures forwarding
Set-Mailbox -Identity $userinput -ForwardingAddress $forwardemail -HiddenFromAddressListsEnabled $true
#REMOVES THE SESSION
Remove-PSsession $ExchangeSession
Start-Sleep -s 2
}
Form text boxes are going to be tricky to handle multiple users. So instead, use a CSV file as input file with the username and ticketnumber as headers.
$CSVList = Import-csv C:\Temp\InputFile.csv
Use a Foreach loop to loop through each line in the CSV.
Foreach ($Line.Username in $CSVList)
{
#your termination code goes here
#if at any point you need to use the ticket number for this user
$TicketNo = $Line.TicketNumber
#This is the Header you used in the csv file.
}
For Display, create an object table (much like a csv file) and show it using a Datagridview or the more simpler Out-Gridview instead of labels so you can support displaying multiple users. (Out-Gridview will create its own window though)
I am trying to create a powershell Script with a Gui that gives the user a drop list of available drives and then when the user selects the drive and clicks ok the script maps that drive as M:\
But I cant work out how to get the selected item in the combo box to be passed into the variable $MapDrive
#List of Drives To Map
$DriveList = #("0. DGL_Data","1. P1","2. DGLFSG3","3. p3 (TPC)","4. p4","6. p6","7. p7","8. p8","9. p9","10. p10","11. p11","12. p12")
#Displays With Drive to Map
$MapDrive = convert.ToString($Dropdown.SelectedItem)
Function MapDrive {
If ($MapDrive -eq $DriveList[0]){
Write-Output Sucess > "C:\Users\andy.burton\Desktop\Practice Selector\Success.txt"}
ElseIf ($MapDrive -eq $DriveList[1]){
Write-Output BooYah > "C:\Users\andy.burton\Desktop\Practice Selector\Yes.txt"
}
Else {
Write-Output Failure > "C:\Users\andy.burton\Desktop\Practice Selector\Failed.txt"}
}
#test Function
Function test {
Write-Output $MapDrive > "C:\Users\andy.burton\Desktop\Practice Selector\Success.txt"
}
#Function to Create Form
Function GenerateForm {
#Define Drive Selector Main Form
Add-Type -AssemblyName System.Windows.Forms
$DGL = New-Object system.Windows.Forms.Form
$DGL.Text = "DGL Practice Manager"
$DGL.TopMost = $true
$DGL.BackgroundImage = [system.drawing.image]::FromFile("C:\Users\andy.burton\Desktop\Practice Selector\Images\medical.jpg")
$DGL.Icon = New-Object system.drawing.icon("C:\Users\andy.burton\Desktop\Practice Selector\Images\medical2.ico")
$DGL.Width = 600
$DGL.Height = 265
$DGL.MinimizeBox = $False
$DGL.MaximizeBox = $False
#Label to Display Instuctions
$label2 = New-Object system.windows.Forms.Label
$label2.Text = "Select which drive"
$label2.BackColor = "#e4f3fa"
$label2.AutoSize = $true
$label2.Width = 25
$label2.Height = 10
$label2.location = new-object system.drawing.point(20,28)
$label2.Font = "Microsoft Sans Serif,10"
$DGL.controls.Add($label2)
#Dropdown Box For Selecting Practice
$Dropdown = New-Object system.windows.Forms.ComboBox
$Dropdown.BackColor = "#e4f3fa"
$DropDown.DropDownStyle = [System.Windows.Forms.ComboBoxStyle]::DropDownList
$Dropdown.Width = 243
$Dropdown.Height = 20
$Dropdown.location = new-object system.drawing.point(21,73)
$Dropdown.Font = "Microsoft Sans Serif,10"
$DropDown.items.addrange($DriveList)
$DGL.controls.Add($Dropdown)
#Cancel Button to Cancel drive Selection
$Cancelbutton = New-Object system.windows.Forms.Button
$Cancelbutton.Text = "Cancel"
$Cancelbutton.Width = 60
$Cancelbutton.Height = 30
$Cancelbutton.location = new-object system.drawing.point(210,120)
$Cancelbutton.Font = "Microsoft Sans Serif,10"
$DGL.CancelButton = $Cancelbutton
$CancelButton.Add_Click({ $DGL.close();[System.Windows.Forms.Application]::Exit($null)})
$DGL.controls.Add($Cancelbutton)
#OK Button to Select Drive
$OKbutton = New-Object system.windows.Forms.Button
$OKbutton.Text = "OK"
$OKbutton.Width = 60
$OKbutton.Height = 30
$OKbutton.location = new-object system.drawing.point(140,120)
$OKbutton.Font = "Microsoft Sans Serif,10"
$DGL.AcceptButton = $OKbutton
$MapDrive = convert.ToString($Dropdown.SelectedItem)
#On click call PracticeSelectedCallBack to launch the application
$OKbutton.Add_Click({test ; $DGL.close()})
$DGL.controls.Add($OKbutton)
#Display the Form
$DGL.Add_Shown({$DGL.Activate()})
$DGL.ShowDialog()
}
GenerateForm
I also want to hide the powershell window but not the gui I have tried -window hidden but that hid everything
The ComboBox has several events that you can tie into that will do various things. One of the events is a SelectedIndexChanged. You can add that event to your ComboBox object and update $MapDrive
This code $MapDrive = convert.ToString($Dropdown.SelectedItem) will only fire once during the initial compile. You have to use events to trigger code changes after compile during runtime.
Also, in Powershell you can use the following command [System.Windows.Forms.ComboBox] | gm to get a list of the methods, and properties of the object. You can use [System.Windows.Forms.checkedlistbox].GetEvents() to get a list of events of an object.
Goal:
I am attempting to create a button that would save a file to desktop. The incoming file is fetched with an Invoke-WebRequest using the GET method. I want the save button to be in my pop-up window.
Here is an example:
Side note:
This code is sitting in a switch with a variable split three ways.
switch (...) {
p {
if ($second -match 'RegexMatch') {
$resource = $second
$fileResult = Invoke-WebRequest -Uri https://url.com/$resource/file -WebSession $currentsession
# End API Call
Write-Host
Write-Host '------------' -ForegroundColor Green
Write-Host 'FILE Results' -ForegroundColor Green
Write-Host '------------' -ForegroundColor Green
# Create Window
Add-Type -AssemblyName System.Windows.Forms
$form = New-Object -TypeName System.Windows.Forms.Form
$form.StartPosition = 'CenterScreen'
$form.KeyPreview = $true
$form.Add_KeyDown {
if ($_.Control -and $_.KeyCode -eq 'F') {
Add-Type -AssemblyName Microsoft.VisualBasic
$stringToFind = [Microsoft.VisualBasic.Interaction]::InputBox('Please enter your search terms', 'Find')
$pos = $textBox.Text.IndexOf($stringToFind)
if ($pos -ne -1) {
$textBox.SelectionStart = $pos
$textBox.SelectionLength = $stringToFind.Length
}
}
}
# Textbox
$textBox = New-Object -TypeName System.Windows.Forms.TextBox
$textBox.Dock = [Windows.Forms.DockStyle]::Fill
$textBox.ReadOnly =$true
$textBox.Multiline = $true
$textBox.ScrollBars = 'Vertical'
$textBox.Font = New-Object -TypeName System.Drawing.Font -ArgumentList ('Arial',12)
$textBox.ForeColor = 'White'
$textBox.Text = $fileResult
$textBox.BackColor = 'Black'
$textBox.ShortcutsEnabled = $true
$Form.Controls.Add($textBox)
# Button
$btn = New-Object -TypeName System.Windows.Forms.Button
$btn.Text = 'Finish'
$btn.DialogResult = 'Ok'
$btn.Dock = 'bottom'
$form.Controls.Add($btn)
if ($form.ShowDialog() -eq 'Ok') {
$tb.lines
}
} else {
Write-Host
Write-Warning -Message 'Please enter a valid FILE ID'
Write-Host
}
break
}
...
}
Purpose:
I want to add an option for the user to download the file for a closer look in a different application.
Question:
How would I begin to create a button utilizing winforms in powershell to save this file to disk?
Here is what I have tried:
$BtnSave=New-Object -TypeName System.Windows.Forms.Button
$BtnSave.Text='Save'
$BtnSave.Dock='bottom'
$btnSave.DialogResult='Ok'
$form.Controls.Add($BtnSave)
$BtnSave.Add_Click({
$SaveFileDialog = New-Object 'System.Windows.Forms.SaveFileDialog'
if ($SaveFileDialog.ShowDialog() -eq 'Ok')
{
$textBox.Text = $SaveFileDialog.FileName
Write-Information 'File Saved'
}
})
New Problem:
File is not saving to disk still, but the save file dialog does show up on click. In addition, using Switch -OutFile with my Invoke-WebRequest is shooting me an error.
Error:
Invoke-WebRequest : Missing an argument for parameter 'OutFile'. Specify a parameter of type 'System.String' and try again.
After adding a button where you want in the form, the Add_Click() method will allow you to handle its click event and run any scriptblock you want (when button is clicked).
At this point, the -OutFile argument for Invoke-WebRequest will help with saving the downloaded file to disk (pass it the desired path to the file).
i am unable to figure out the mistake that i am doing.The following script will call a bunch of batch files and while the batch files are doing there job, the progress will be shown in a progress bar along with the name of the script. What i want to achieve is to display a custom message during the installation process. I am not able to find out the mistake, any help is deeply appreciated.
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") | Out-Null
Set-Location $PSScriptRoot
#Call scripts for installation
$ScriptsHome = Get-Item '.\test\forPS\*'
#Define Form
# Init Form
$Form = New-Object System.Windows.Forms.Form
$Form.width = 1000
$Form.height = 200
$Form.Text = "** Installation in Progress-PLEASE DO NOT CLOSE THIS WINDOW**"
$Form.Font = New-Object System.Drawing.Font("Times New Roman" ,12, [System.Drawing.FontStyle]::Regular)
$Form.MinimizeBox = $False
$Form.MaximizeBox = $False
$Form.WindowState = "Normal"
$Form.StartPosition = "CenterScreen"
$Form.Opacity = .8
$Form.BackColor = "Gray"
#Define ICON for Form
$Icon = New-Object System.Drawing.Graphics (".\ICON.jpg")
$Form.Icon = $Icon
# Init ProgressBar
$ProgressBar = New-Object System.Windows.Forms.ProgressBar
$ProgressBar.Maximum = $ScriptsHome.Count
$ProgressBar.Minimum = 0
$ProgressBar.Location = new-object System.Drawing.Size(10,70)
$ProgressBar.size = new-object System.Drawing.Size(967,10)
$Form.Controls.Add($ProgressBar)
$Form.Controls.Add($Messages)
#Running Script Name
$Label = New-Object System.Windows.Forms.Label
$Label.AutoSize = $true
$Label.Location = New-Object System.Drawing.Point(10,50)
$Form.Controls.Add($Label)
#Define Array messages
#Array
$Messages = #("Preparing to install patch set..Preparing to stop all related processes",
"Upgrading the application",
"Copying the missing folder",
"Applying the patch",
"Starting all previously stopped Services",
"Checkcing healthyness of the system after the installation this is may take up to half an hour...",
"Rebooting Server"
)
$Messages = New-Object System.Windows.Forms.Label
$Messages.AutoSize = $true
$Messages.Location = New-Object System.Drawing.Point(10,50)
$Form.Controls.Add($Messages)
# Add_Shown action
$ShownFormAction = {
$Form.Activate()
foreach ($script in $ScriptsHome) {
$ProgressBar.Increment(1)
#$Messages.Text = $Messages[$Messages]
$Label.Text = "$($script.Name)"
Start-Process $script.FullName -Wait -WindowStyle Hidden
}
$Form.Dispose()
}
$Form.Add_Shown($ShownFormAction)
# Show Form
$Form.ShowDialog()
Thanks in advance.
You're reusing the same variable name for the list of messages and the label showing the message itself:
$Messages = #("Preparing to install patch set..Preparing to stop all related processes",
"Upgrading the application",
"Copying the missing folder",
"Applying the patch",
"Starting all previously stopped Services",
"Checkcing healthyness of the system after the installation this is may take up to half an hour...",
"Rebooting Server"
)
$Messages = New-Object System.Windows.Forms.Label
$Messages.AutoSize = $true
$Messages.Location = New-Object System.Drawing.Point(10,50)
Rename one of them (ie. use $MessageLabel for the label):
$MessageLabel = New-Object System.Windows.Forms.Label
$MessageLabel.AutoSize = $true
$MessageLabel.Location = New-Object System.Drawing.Point(10,50)
Since you increment the ProgressBar by 1 every step, you can reuse the progress bar value to index into the $Messages array:
foreach ($script in $ScriptsHome) {
$ProgressBar.Increment(1)
$MessageLabel.Text = $Messages[$ProgressBar.Value - 1]
$Label.Text = "$($script.Name)"
Start-Process $script.FullName -Wait -WindowStyle Hidden
}