2
\$\begingroup\$

I've written this rather cumbersome PowerShell script that facilitates a business use case. Because I built it off a PowerShell auditing tool I made, it was formatted differently.

What I'd like to do, in the module that launches the WPF window, is have it launch a runspace for the portion that verifies whether the mailbox is on Exchange onprem or on cloud, then launch another runspace to make the change.

However, some guides I've seen suggest that I write a C# to launch the menu, and then functionalize the actual 2-3 PowerShell sections for the C# based menu to call. (I don't know visual studio or C# well enough to do that without major heart palpitations.)

 function proc-input { Param ( [parameter(ValueFromPipeline)] [hashtable]$uuer ) $a = 0 $a = $b = $null [string]$uuss = $uuer.userooo [string]$usdo = $uuer.startdateooo if ($uuer.enddateooo -ne $null){ [string]$uedo = $uuer.enddateooo } else {$uedo = $null } [string]$uimo = $uuer.intmesooo [string]$uemo = $uuer.extmesooo [bool]$glib = $uuer.halt $uuss = $uuss.trim() # update-window "String" # update-window $uuss -AppendContent con-main } function con-main { try { Get-MsolDomain -ErrorAction Stop > $null } catch { if ($cred -eq $null){ update-window "Connecting to server to Validate, requires credentials" $usercredential = Get-Credential #import-module msonline import-module azureadpreview $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection #-SessionOption $proxysettings Import-PSSession $Session -DisableNameChecking -allowclobber connect-msolservice -Credential $userCredential } } eml-valid } function eml-valid { $b = $a = $null try { $a = Get-Mailbox $uuss -erroraction stop } catch { $a = 0 } if ($a -eq 0){ $sam,$max = $uuss.split("{@}") try { $b = get-aduser $sam } catch { $b = 0 } if ($b -eq 0) { update-window "$uuss,Email NOT Found! TRY: First initial last name?" proc-input } else { update-window "Legacy Email; Changing connection. Login first initial last name email : bob smith = [email protected]" con-ex $glib } } else { update-window "Setting mailbox $uuss on cloud" set-ooomessage $uuss $usdo $glib if ($glib -ne $true){ update-window "$uuss Success, user updated in Exchange online updated" } } } function set-ooomessage { #will set out of office message if fed 3 correct strings. Param ( [parameter(Mandatory,ValueFromPipeline)] [string]$user, [parameter(Mandatory,ValueFromPipeline)] [string]$startdate, [parameter(ValueFromPipeline)] [bool]$halt ) if ($halt -ne $true){ if ($uedo -ne $null){ Set-MailboxAutoReplyConfiguration $user -AutoReplyState Scheduled -StartTime $startdate -EndTime $uedo -ExternalMessage $uemo -InternalMessage $uimo add-content $ooolog "$user,$startdate,$uedo,$uemo,$uimo" } else { Set-MailboxAutoReplyConfiguration $user -AutoReplyState Scheduled -StartTime $startdate -InternalMessage $uimo -ExternalMessage $uemo add-content $ooolog "$user,$startdate,$uemo,$uimo" } } else { add-content $ooolog "$user,$startdate,$uemo,$uimo,test" update-window "Test occured $uuss NOT UPDATED" } } function con-ex { #connect exchange on prem $rmtest = get-pssession foreach ($line in $rmtest){ $cnr = $line.computername $cnid = $line.id if ($cnr -like "outlook.office365.com") { remove-pssession $cnid} } $UserC = Get-credential $localEx = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://exchange.companyco.com/PowerShell/ -Authentication Kerberos -Credential $UserC Import-PSSession $localEx -DisableNameChecking set-ooomessage $uuss $usdo $glib if ($glib -ne $true) { update-window "Success: $uuss updated in Exchange On prem"} } Function Update-Window { Param ( $Content, [switch]$AppendContent ) If ($PSBoundParameters['AppendContent']) { $window.results.AppendText($content) } Else { $window.results.Text = $Content } } 

** xaml.. **

$xaml = @' <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" MinWidth="200" Width ="440.172" SizeToContent="Height" Title="New Out of Office Message" Topmost="True" Height="313.505"> <Grid Margin="10,40,10,10"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <TextBlock Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" Margin="5">Please enter user email. If <Bold>BOLD</Bold>, Must be filled out</TextBlock> <TextBlock Grid.Column="0" Grid.Row="1" Margin="5"><Bold>Email</Bold></TextBlock> <TextBlock Grid.Column="0" Grid.Row="2" Margin="5"><Bold>Start Date</Bold></TextBlock> <TextBlock Grid.Column="0" Grid.Row="3" Margin="5">End Date</TextBlock> <TextBlock Grid.Column="0" Grid.Row="4" Margin="5"><Bold>Internal Message</Bold></TextBlock> <TextBlock Grid.Column="0" Grid.Row="5" Margin="5"><Bold>External Message</Bold></TextBlock> <TextBlock Grid.Column="0" Grid.Row="7" Margin="5">Results</TextBlock> <TextBox Name="TxtEmail" Grid.Column="1" Grid.Row="1" Margin="5"></TextBox> <DatePicker x:Name="StartDate" Grid.Column="1" Grid.Row="2" Margin="5" /> <DatePicker x:Name="EndDate" Grid.Column="1" Grid.Row="3" Margin="5" /> <TextBox Name="TxtiMesg" Grid.Column="1" Grid.Row="4" Margin="5"></TextBox> <TextBox Name="TxteMesg" Grid.Column="1" Grid.Row="5" Margin="5"></TextBox> <TextBox x:Name= "results" Height = "80" Grid.ColumnSpan="2" Grid.Row="8" Width = "500"/> <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="0,10,0,0" Grid.Row="6" Grid.ColumnSpan="2"> <CheckBox x:Name="Item2" Content = 'Test'/> <Button Name="ButOk" MinWidth="80" Height="22" Margin="5">OK</Button> <Button Name="ButDone" MinWidth="80" Height="22" Margin="5">Done</Button> <Button Name="ButCancel" MinWidth="80" Height="22" Margin="5">Cancel</Button> </StackPanel> </Grid> </Window> 

 function Convert-XAMLtoWindow { param ( [Parameter(Mandatory=$true)] [string] $XAML ) Add-Type -AssemblyName PresentationFramework $reader = [XML.XMLReader]::Create([IO.StringReader]$XAML) $result = [Windows.Markup.XAMLReader]::Load($reader) $reader.Close() $reader = [XML.XMLReader]::Create([IO.StringReader]$XAML) while ($reader.Read()) { $name=$reader.GetAttribute('Name') if (!$name) { $name=$reader.GetAttribute('x:Name') } if($name) {$result | Add-Member NoteProperty -Name $name -Value $result.FindName($name) -Force} } $reader.Close() $result } function Show-WPFWindow { param ( [Parameter(Mandatory=$true)] [Windows.Window] $Window ) $result = $null $null = $window.Dispatcher.InvokeAsync{ $result = $window.ShowDialog() Set-Variable -Name result -Value $result -Scope 1 }.Wait() $result } #endregion Code Behind function get-userinp { #region Convert XAML to Window $window = Convert-XAMLtoWindow -XAML $xaml #endregion

#region Define Event Handlers # Right-Click XAML Text and choose WPF/Attach Events to # add more handlers $window.ButCancel.add_Click( { $window.DialogResult = $false } ) $window.ButOk.add_Click( { $window.ButOk.isenabled = $false $window.ButDone.isenabled = $false $window.ButCancel.isenabled = $false # $window.DialogResult = $true [hashtable]$return = @{} $return.userooo = $window.TxtEmail.Text $return.startdateooo = $window.StartDate.SelectedDate.Date.ToShortDateString() if($window.EndDate.SelectedDate -ne $null){ $return.enddateooo = $window.EndDate.SelectedDate.Date.ToShortDateString()} $return.extmesooo = $window.TxteMesg.Text $return.intmesooo = $window.TxtiMesg.Text if ($window.item2.ischecked){ $return.halt = $true} proc-input $return $sescleaner = get-pssession foreach ($lin in $sescleaner){ $cnid = $lin.id remove-pssession $cnid } $window.ButDone.isenabled = $true } ) $window.ButDone.add_Click( { $window.DialogResult = $true } ) #endregion Event Handlers #region Manipulate Window Content $window.TxtiMesg.Text = 'User is out of office' $window.TxteMesg.Text = 'User is out of office' $window.TxtEmail.Text = '[email protected]' $null = $window.TxtEmail.Focus() #endregion # Show Window $result = Show-WPFWindow -Window $window } $ooopath = "\ooolog.txt" $ooolog = $env:temp + $ooopath get-userinp #con-main #eml-valid #uncomment to test data capture loop #con-ex

\$\endgroup\$
3
  • 2
    \$\begingroup\$Welcome to Code Review! I am uncertain as to whether your code accomplishes what you want it to, could you please clear that up for me?\$\endgroup\$
    – Malachi
    CommentedSep 20, 2018 at 15:37
  • 2
    \$\begingroup\$my code currently accomplishes what it is intended to do, but not efficiently.\$\endgroup\$
    – pakman
    CommentedSep 20, 2018 at 19:52
  • \$\begingroup\$@PhillKenyon you could try to use jobs and synchronize their results and statuses with timer object. Manage controls and all. Your form will be responsive that way.\$\endgroup\$CommentedSep 29, 2018 at 14:21

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.