SignatureGenerator.ps1

#-------------------------------------------------------------#
#----Initial Declarations-------------------------------------#
#-------------------------------------------------------------#

Add-Type -AssemblyName PresentationCore, PresentationFramework, System.Windows.Forms

$Xaml= @"
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Width="900" Height="500">
  <Grid>
    <Grid.ColumnDefinitions>
    <ColumnDefinition Width="110"/>
    <ColumnDefinition Width="676*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
      <RowDefinition Height="200"/>
      <RowDefinition Height="263*"/>
    </Grid.RowDefinitions>
    <Border BorderBrush="Black" BorderThickness="1" Grid.Row="0" Grid.Column="0">
      <StackPanel>
        <Label HorizontalAlignment="Left" VerticalAlignment="Top" Content="Full Name" Margin="0,0,0,0"/>
        <Label HorizontalAlignment="Left" VerticalAlignment="Top" Content="Title" Margin="0,0,0,0"/>
        <Label HorizontalAlignment="Left" VerticalAlignment="Top" Content="Role" Margin="0,0,0,0"/>
        <Label HorizontalAlignment="Left" VerticalAlignment="Top" Content="Office Number" Margin="0,0,0,0"/>
        <Label HorizontalAlignment="Left" VerticalAlignment="Top" Content="Cell Number" Margin="0,0,0,0"/>
        <Label HorizontalAlignment="Left" VerticalAlignment="Top" Content="Email Address" Margin="0,0,0,0"/>
        <Label HorizontalAlignment="Left" VerticalAlignment="Top" Content="Street Address" Margin="0,0,0,0"/>
      </StackPanel>
    </Border>
    <Border BorderBrush="Black" BorderThickness="1" Grid.Row="1" Grid.Column="1" Name="WebBorder">
    <WebBrowser Name="WebBrowser" ></WebBrowser>
    </Border>
    <Border BorderBrush="Black" BorderThickness="1" Grid.Row="1" Grid.Column="0">
      <StackPanel>
      <Button Content="Import Template" HorizontalAlignment="Left" VerticalAlignment="Top" Width="95" Margin="5,5,0,0" Name="l6kw1fboq5l4q"/>
      <Button Content="Preview" HorizontalAlignment="Left" VerticalAlignment="Top" Width="95" Margin="5,5,0,0" Name="l6kw1fboo4u5b"/>
      <Button Content="Save" HorizontalAlignment="Left" VerticalAlignment="Top" Width="95" Margin="5,5,0,0" Name="l6kw1fbormfj6"/>
      </StackPanel>
    </Border>
    <Grid Grid.Row="0" Grid.Column="1">
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="144*"/>
        <ColumnDefinition Width="566*"/>
      </Grid.ColumnDefinitions>
      <Border BorderBrush="Black" BorderThickness="1" Grid.Row="0" Grid.Column="0">
        <StackPanel>
        <TextBox HorizontalAlignment="Left" VerticalAlignment="Top" Height="17" Width="140" TextWrapping="Wrap" Margin="5,5,0,0" Text="{Binding Name}" Name="l6kw1fboyydhr"/>
        <TextBox HorizontalAlignment="Left" VerticalAlignment="Top" Height="17" Width="140" TextWrapping="Wrap" Margin="5,10,0,0" Text="{Binding Title}" Name="l6kw1fboi72h2"/>
        <TextBox HorizontalAlignment="Left" VerticalAlignment="Top" Height="17" Width="140" TextWrapping="Wrap" Margin="5,10,0,0" Text="{Binding Role}" Name="TextRole"/>
        <TextBox HorizontalAlignment="Left" VerticalAlignment="Top" Height="17" Width="140" TextWrapping="Wrap" Margin="5,10,0,0" Text="{Binding OfficePhone}" Name="l6kw1fbosfusy"/>
        <TextBox HorizontalAlignment="Left" VerticalAlignment="Top" Height="17" Width="140" TextWrapping="Wrap" Margin="5,10,0,0" Text="{Binding Email}" Name="l6kw1fbo1o7t9"/>
        <TextBox HorizontalAlignment="Left" VerticalAlignment="Top" Height="17" Width="140" TextWrapping="Wrap" Margin="5,10,0,0" Text="{Binding CellPhone}" Name="CellPhone"/>
      <ComboBox HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" Margin="5,10,0,0" Name="l6kw1fbok04cl" ItemsSource="{Binding Locations}"/>
        </StackPanel>
      </Border>
      <Border BorderBrush="Black" BorderThickness="1" Grid.Row="0" Grid.Column="1" Name="ATBorder">
        <StackPanel>
        <Label HorizontalAlignment="Left" VerticalAlignment="Top" Content="Select A Template" Margin="0,5,0,0"/>
        <ComboBox HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" Margin="5,5,0,0" Name="TemplateSelector" ItemsSource="{Binding Templates}"/>
        </StackPanel>
      </Border>
    </Grid>
  </Grid>
</Window>
"@


#-------------------------------------------------------------#
#----Control Event Handlers-----------------------------------#
#-------------------------------------------------------------#


#region Logic
$Locations = @()
$Locations += [PSCustomObject]@{
    Name = "Melbourne HQ"
    Address = "7607 Coral Drive"
    City = "West Melbourne, FL 32904 USA"
}
$Locations += [PSCustomObject]@{
    Name = "Melbourne Accounting"
    Address = "1990 West New Haven Avenue"
    City = "Melbourne Financial Center, Suite 304 <BR> Melbourne, Florida 32904, United States"
}
$Locations += [PSCustomObject]@{
    Name = "Orlando"
    Address = "5025 S. Orange Avenue"
    City = "Orlando, Florida 32809, United States"
}
$LocIndex = $Locations | Group-Object Name -AsHashTable -AsString
#endregion
#region Template
function Get-Template {
  if ($State.SelectedTemplate -eq "Artemis IT") {
    if ($State.Role.Length -le 1)  {
    $State.Template = $Null
    $State.Template = (Invoke-WebRequest -Uri "https://raw.githubusercontent.com/Artemisit/Signatures/main/Template.html" -UseBasicParsing).Content
    }
    if ($State.Role.Length -gt 1){
      $State.Template = $Null
      $State.Template = (Invoke-WebRequest -Uri "https://raw.githubusercontent.com/Artemisit/Signatures/main/Template_Role.html" -UseBasicParsing).Content
    }
  }
  if ($State.SelectedTemplate -eq "ME Construction") {
    if ($State.CellPhone.Length -le 1) {
      $State.Template = $Null
      $State.Template = (Invoke-WebRequest -Uri "https://raw.githubusercontent.com/MEConstruction/Signature/main/Kimberly.htm" -UseBasicParsing).Content
    }
    if ($State.CellPhone.Length -gt 1) {
      $State.Template = $Null
      $State.Template = (Invoke-WebRequest -Uri "https://raw.githubusercontent.com/MEConstruction/Signature/main/MECSignature.htm" -UseBasicParsing).Content
    }
  }
}
function PreviewChanges {
  if ($State.SelectedTemplate -eq "Artemis IT") {
    if ($State.Role.Length -le 1){
    $State.Changes = $null
    $State.Changes = $State.Template.replace("Replace Name", $State.Name).replace("Replace Title", $State.Title)
    }
    if ($State.Role.Length -gt 1){
      $State.Changes = $null
      $State.Changes = $State.Template.replace("Replace Name", $State.Name).replace("Replace Title", $State.Title).replace("Replace Role",$State.Role)
    }
  }
  if ($State.SelectedTemplate -eq "ME Construction") {
    SetLocation
    if ($State.CellPhone.length -le 1) {
      $State.Changes = $null
      $State.Changes = $State.Template.replace("Replace Name", $State.Name).replace("Replace Title", $State.Title).replace("Replace Office Phone", $State.OfficePhone).replace("Replace Email", $State.Email).replace("Replace Street", $State.Street).replace("Replace City", $State.City).replace("KimberlyB@MECFlorida.com", $State.Email)
    }
    if ($State.CellPhone -gt 1) {
      $State.Changes = $null
      $State.Changes = $State.Template.replace("Replace Name", $State.Name).replace("Replace Title", $State.Title).replace("Replace Office Phone", $State.OfficePhone).replace("Replace Email", $State.Email).replace("Replace Street", $State.Street).replace("Replace City", $State.City).replace("KimberlyB@MECFlorida.com", $State.Email).replace("Replace Cell", $State.CellPhone)
    }
  }
  $WebBrowser.NavigateToString($State.Changes)
    
}
function SaveChanges {
  $FileBrowser = [System.Windows.Forms.SaveFileDialog]::new()
  $FileBrowser.InitialDirectory = [Environment]::GetFolderPath('Desktop')
  $FileBrowser.Filter = "HTM Files (*.htm)|*.htm"
  if ($State.SelectedTemplate -eq "Artemis IT"){
    if ($State.Role.Length -le 1){
    $State.Changes = $State.Template.replace("Replace Name",$State.Name).replace("Replace Title",$State.Title)
    }
    if ($State.Role.Length -gt 1){
      $State.Changes = $State.Template.replace("Replace Name",$State.Name).replace("Replace Title",$State.Title).replace("Replace Role",$State.Role)
    }
    $FileBrowser.FileName = "$($State.Name)Signature.htm"
  }
  if ($State.SelectedTemplate -eq "ME Construction"){
    SetLocation
    $State.Changes = $State.Template.replace("Replace Name",$State.Name).replace("Replace Title",$State.Title).replace("Replace Office Phone",$State.OfficePhone).replace("Replace Email",$State.Email).replace("Replace Street",$State.Street).replace("Replace City",$State.City).replace("KimberlyB@MECFlorida.com",$State.Email)
    $FileBrowser.FileName = "MECSignature.htm"
  }
    $FileBrowser.ShowDialog()
    $State.Changes > $FileBrowser.FileName
}
function SetLocation {
    $State.Street = ($LocIndex[$l6kw1fbok04cl.SelectedItem]).Address
    $State.City = ($LocIndex[$l6kw1fbok04cl.SelectedItem]).city
}
function SetTemplate{
  $State.SelectedTemplate = $TemplateSelector.SelectedItem
}
#endregion


#-------------------------------------------------------------#
#----Script Execution-----------------------------------------#
#-------------------------------------------------------------#

$Window = [Windows.Markup.XamlReader]::Parse($Xaml)

[xml]$xml = $Xaml

$xml.SelectNodes("//*[@Name]") | ForEach-Object { Set-Variable -Name $_.Name -Value $Window.FindName($_.Name) }


$TemplateSelector.Add_SelectionChanged({SetTemplate $this $_})
$l6kw1fbok04cl.Add_SelectionChanged({SetLocation $this $_})
$l6kw1fboq5l4q.Add_Click({Get-Template $this $_})
$l6kw1fboo4u5b.Add_Click({PreviewChanges $this $_})
$l6kw1fbormfj6.Add_Click({SaveChanges $this $_})

$State = [PSCustomObject]@{}


Function Set-Binding {
    Param($Target,$Property,$Index,$Name,$UpdateSourceTrigger)
 
    $Binding = New-Object System.Windows.Data.Binding
    $Binding.Path = "["+$Index+"]"
    $Binding.Mode = [System.Windows.Data.BindingMode]::TwoWay
    if($UpdateSourceTrigger -ne $null){$Binding.UpdateSourceTrigger = $UpdateSourceTrigger}


    [void]$Target.SetBinding($Property,$Binding)
}

function FillDataContext($props){

    For ($i=0; $i -lt $props.Length; $i++) {
   
   $prop = $props[$i]
   $DataContext.Add($DataObject."$prop")
   
    $getter = [scriptblock]::Create("Write-Output `$DataContext['$i'] -noenumerate")
    $setter = [scriptblock]::Create("param(`$val) return `$DataContext['$i']=`$val")
    $State | Add-Member -Name $prop -MemberType ScriptProperty -Value  $getter -SecondValue $setter
               
       }
   }



$DataObject =  ConvertFrom-Json @"
 
{
    "Template" : "$Template",
    "Name" : "",
    "Title" : "",
    "OfficePhone" : "",
    "CellPhone" : "",
    "Email" : "",
    "Street" : "",
    "City" : "",
    "Changes": "",
    "Locations": ["Melbourne HQ","Melbourne Accounting","Orlando"],
    "Templates" : ["ME Construction","Artemis IT"],
    "SelectedTemplate" : "",
    "Role" : ""
}
 
"@


$DataContext = New-Object System.Collections.ObjectModel.ObservableCollection[Object]
FillDataContext @("Template","Name","Title","OfficePhone","CellPhone","Email","Street","City","Changes","Locations","Templates","SelectedTemplate","Role") 

$Window.DataContext = $DataContext
Set-Binding -Target $l6kw1fboyydhr -Property $([System.Windows.Controls.TextBox]::TextProperty) -Index 1 -Name "Name"  
Set-Binding -Target $l6kw1fboi72h2 -Property $([System.Windows.Controls.TextBox]::TextProperty) -Index 2 -Name "Title"  
Set-Binding -Target $l6kw1fbosfusy -Property $([System.Windows.Controls.TextBox]::TextProperty) -Index 3 -Name "OfficePhone" 
Set-Binding -Target $CellPhone     -Property $([System.Windows.Controls.TextBox]::TextProperty) -Index 5 -Name "CellPhone"
Set-Binding -Target $l6kw1fbo1o7t9 -Property $([System.Windows.Controls.TextBox]::TextProperty) -Index 4 -Name "Email"  
Set-Binding -Target $l6kw1fbok04cl -Property $([System.Windows.Controls.ComboBox]::ItemsSourceProperty) -Index 9 -Name "Locations"
Set-Binding -Target $TemplateSelector  -Property $([System.Windows.Controls.ComboBox]::ItemsSourceProperty) -Index 10 -Name "Templates"
Set-Binding -Target $TextRole -Property $([System.Windows.Controls.TextBox]::TextProperty) -Index 12 -Name "Role"

$Window.ShowDialog()