Tag: Forms

PS (Unfinished): O365 User Exporter For MaaS360

PS (Unfinished): O365 User Exporter For MaaS360

Image result for its broken meme

#############################################
## Global Varibles
#############################################
$script:SelectedPath = $null
$script:PrimaryDomain = $null
$script:PulledUserData = $null
$Script:date = Get-Date -Format ddmmyyhhmm
#############################################
## Functions
#############################################
Function EnvConfiguration () {
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -ErrorAction Stop
Add-Type -AssemblyName PresentationFramework
Add-Type -assemblyName PresentationCore
Add-Type -assemblyName WindowsBase
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
}
Function WelcomeDialouge () {
# Dialouge Box Params
$ButtonType = [Windows.MessageBoxButton]::OKCancel
$MessageIcon = [Windows.MessageBoxImage]::Information
$MessageTitle = "ASB -- O365 CSV Exported For IBM MaaS360 -- Daisy Stevens"
$MessageBody = "Welcome to the O365 Exporter Tool for`nMass User Importation into MaaS360`n `nScript Developed By: Daisy Olivia Stevens`nhttps://adminscriptbank.wordpress.com`nhttps://www.linkedin.com/in/theheroic"
# Dialouge Call
$Result = [Windows.MessageBox]::Show($MessageBody,$MessageTitle,$ButtonType,$MessageIcon)
if ($Result -eq 'OK'){ConnectO365}
}
Function ConnectO365 (){
# Obtain Creds from User input and then connect to Office 365
Connect-MsolService #-ErrorAction stop
# Move to the next function
Set-DestinationPath
}
function Set-DestinationPath {
$Message = "Please select a directory for your exported data"
$FolderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog -Property @{SelectedPath = 'C:\'}
$FolderBrowser.Description = $Message
[void]$FolderBrowser.ShowDialog()
$FolderBrowser.SelectedPath
if ($FolderBrowser.SelectedPath -eq $null){
# Dialouge Box Params
$ButtonType = [Windows.MessageBoxButton]::OKCancel
$MessageIcon = [Windows.MessageBoxImage]::Error
$MessageTitle = "ASB -- O365 CSV Exporter For IBM MaaS360 -- Daisy Stevens"
$MessageBody = "No Path Was Selected!`nDo you wish to try again?"
# Dialouge Call
$Result = [Windows.MessageBox]::Show($MessageBody,$MessageTitle,$ButtonType,$MessageIcon)
if ($Result -eq 'OK'){Set-DestinationPath} else {exit}
}
$script:SelectedPath = $FolderBrowser.SelectedPath
PrimaryDomainSelector
}
Function PrimaryDomainSelector {
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "ASB -- O365 CSV Exported For IBM MaaS360 -- Daisy Stevens"
$objForm.Size = New-Object System.Drawing.Size(800,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()}})
$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();PullUserData;})
$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 Select Your Primary Domain:"
$objForm.Controls.Add($objLabel)
$objListBox = New-Object System.Windows.Forms.ListBox
$objListBox.Location = New-Object System.Drawing.Size(10,40)
$objListBox.Size = New-Object System.Drawing.Size(755,20)
$objListBox.Height = 80
Get-MsolDomain | Select-Object name | ForEach-Object {$_.Name | Add-Content -Path .\o365Domains.tmp}
ForEach($i in (Get-Content .\o365Domains.tmp))
{[void] $objListBox.Items.Add($i)}
$objForm.Controls.Add($objListBox)
$objForm.Topmost = $True
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
$script:PrimaryDomain = $objListBox.SelectedItem
$FileName = ".\o365Domains.tmp"
if (Test-Path $FileName)
{
Remove-Item $FileName
}
}
function PullUserData {
$script:PulledUserData = Get-MsolUser -All | Select-Object *
ComposeCSV
}
function CustomInputBox([string] $title, [string] $message, [string] $defaultText) {
[void] [Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$userForm = New-Object System.Windows.Forms.Form
$userForm.Text = "$title"
$userForm.Size = New-Object System.Drawing.Size(800,200)
$userForm.StartPosition = "CenterScreen"
$userForm.AutoSize = $False
$userForm.MinimizeBox = $False
$userForm.MaximizeBox = $False
$userForm.SizeGripStyle= "Hide"
$userForm.WindowState = "Normal"
$userForm.FormBorderStyle="Fixed3D"
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(115,80)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({$value=$objTextBox.Text;$userForm.Close()})
$userForm.Controls.Add($OKButton)
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(195,80)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$userForm.Close()})
$userForm.Controls.Add($CancelButton)
$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 = "$message"
$userForm.Controls.Add($userLabel)
$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)
$objTextBox.Text="$defaultText"
$userForm.Controls.Add($objTextBox)
$userForm.Topmost = $True
$userForm.Opacity = 1.0
$userForm.ShowIcon = $False
$userForm.Add_Shown({$userForm.Activate()})
[void] $userForm.ShowDialog()
$value=$objTextBox.Text
return $value
}
function ComposeCSV {
$UserInputTitle = "ASB -- O365 CSV Exporter For IBM MaaS360 -- Daisy Stevens"
$userInputText = "Please enter your international country code: "
$UserInputDText = "+44"
$userInput = CustomInputBox $UserInputTitle $userInputText $UserInputDText
if ($userInput -eq $null){
$userInput = CustomInputBox $UserInputTitle $userInputText $UserInputDText
}
$Data = New-Object System.Object
$Data | Add-Member -MemberType Property -Name "Full Name" -Value $script:PulledUserData.DisplayName
$Data | Add-member -MemberType Property -Name "User Name" -Value $script:PulledUserData.Signinname
$Data | Add-Member -MemberType Property -Name "Domain" -Value $Domain
$Data | Add-Member -MemberType Property -Name "Email" -Value $script:PulledUserData.signinname
$Data | Add-Member -MemberType Property -Name "Phone Number Country" -Value $userInput
$Data | Add-Member -MemberType Property -Name "Phone Number" -Value $script:PulledUserData.PhoneNumber
$Data | Add-Member -MemberType Property
-Name "Location" -Value "London"
$Data | add-member -MemberType Property -Name "Auth Type" -Value "Local"
$Data | select * | Export-Csv -Path "$script:SelectedPath\Maas360_Export_$script:date.csv" -NoTypeInformation
CompleteMSG
}
function CompleteMSG {
# Dialouge Box Params
$ButtonType = [Windows.MessageBoxButton]::OK
$MessageIcon = [Windows.MessageBoxImage]::Information
$MessageTitle = "ASB -- O365 CSV Exported For IBM MaaS360 -- Daisy Stevens"
$MessageBody = "The Script has completed successfully"
# Dialouge Call
$Result = [Windows.MessageBox]::Show($MessageBody,$MessageTitle,$ButtonType,$MessageIcon)
if ($Result -eq 'OK'){exit}
}
#############################################
## Function Calls
#############################################
EnvConfiguration
WelcomeDialouge
Advertisements
PS: GUI Building Made Easy

PS: GUI Building Made Easy

Okay so when it comes to doing form building in raw PowerShell the formatting can be a complete pain in the ass. So I have been searching online that’s pretty dangerous for me I never know what I might find but on this occasion I have found an awesome tool from Sapien Technologies called Primal Forms CE the only issue I wasn’t able to find a download link for the application and it looks like they have discontinued it for free use but don’t worry I managed to find it sitting on an old mirror and I have left a copy of it on my OneDrive for all of you to download.

So what does it do?

In essence it is a simple drag and drop environment for designing your GUI that is then exportable into raw PowerShell code. Saving you the time and the headache of trying to format it in code

Screenshot:

c8353444d582ed3d820731582abf994d

Download Link:

https://1drv.ms/u/s!Amaaew76M7RY1Wo78izJXXDhhSjh

 

PS: Using Forms to Return PC Hostname

PS: Using Forms to Return PC Hostname

The following script is a short demo to show you how to build a form and display the PC host name in a text box on the button click action

#Generated Form Function
function GenerateForm {
#region Import the Assemblies
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
#endregion

#region Generated Form Objects
$form1 = New-Object System.Windows.Forms.Form
$textBox1 = New-Object System.Windows.Forms.TextBox
$button1 = New-Object System.Windows.Forms.Button
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#endregion Generated Form Objects

#----------------------------------------------
#Generated Event Script Blocks
#----------------------------------------------
#Provide Custom Code for events specified in PrimalForms.
$button1_OnClick=
{
#TODO: Place custom script here
$textBox1.Text = $env:COMPUTERNAME

}

$handler_form1_Load=
{
#TODO: Place custom script here

}

$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
$form1.WindowState = $InitialFormWindowState
}

#----------------------------------------------
#region Generated Form Code
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 261
$System_Drawing_Size.Width = 284
$form1.ClientSize = $System_Drawing_Size
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$form1.Name = "form1"
$form1.Text = "GET PC Name"
$form1.add_Load($handler_form1_Load)

$textBox1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 73
$System_Drawing_Point.Y = 120
$textBox1.Location = $System_Drawing_Point
$textBox1.Name = "textBox1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 175
$textBox1.Size = $System_Drawing_Size
$textBox1.TabIndex = 1

$form1.Controls.Add($textBox1)

$button1.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 105
$System_Drawing_Point.Y = 91
$button1.Location = $System_Drawing_Point
$button1.Name = "button1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 75
$button1.Size = $System_Drawing_Size
$button1.TabIndex = 0
$button1.Text = "GO!"
$button1.UseVisualStyleBackColor = $True
$button1.add_Click($button1_OnClick)

$form1.Controls.Add($button1)

#endregion Generated Form Code

#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$form1.ShowDialog()| Out-Null

} #End Function

#Call the Function
GenerateForm