I have a pretty complex script that does quite a bit, However I made a WPF form for a user interface that lays on the top of it. However when it gets to a specific spot in the script the entire script and WPF interface freezes. I have tested the code in just powershell without the WPF form and it works like a charm. Can someone please explain or assist me in figuring this out? The script freezes when it gets to the PlatformFileCopy function of the code, the ClientFolderTicketFolderAndPlatform function work just fine. Any and all help would be much appreciated. Thank you! Powershell code and WPF code below. NOte: This WPF form was made in Visual Studio. Please note: I have tried to just combine the two functions in to one, which on their own in just powershell work but it still freezes on the File Copy portion of the code even if its just running one function.
**Powershell SCRIPT:**
#Assembly and Sharepoint Connection Code
Add-Type -AssemblyName PresentationFramework
Connect-PnPOnline -Url "URL to sharepoint site" -UseWebLogin
#USER INITIALS OF THE TEAM MEMBER RUNNING THE SCRIPT
$UserName = $env:username.ToCharArray() | Select -First 2
$UserInitials = $UserName -Join ""
#XAML GUI FILE PATH
$xamlFile = "File Path to xaml file"
#GUI Creation Code
$inputXML = Get-Content $xamlFile -Raw
$inputXML = $inputXML -replace 'mc:Ignorable="d"', '' -replace "x:N", 'N' -replace '^<Win.*', '<Window'
[XML]$XAML = $inputXML
#Read XAML
$reader = (New-Object System.Xml.XmlNodeReader $xaml)
try
{
$window = [Windows.Markup.XamlReader]::Load($reader)
}
catch
{
Write-Warning $_.Exception
throw
}
#Create variables based on form control names
$xaml.SelectNodes("//*[@Name]") | ForEach-Object {
try
{
Set-Variable -Name "var_$($_.Name)" -Value $window.FindName($_.Name) -ErrorAction Stop
}
catch
{
throw
}
}
#GUI Variable Code Functions
$PlatformFiles = $var_Platform_Selection.Text
$TicketNumber = $var_Ticket_Number.Text + "_" + $UserInitials
$FolderName = $var_Client_Name.Text
if( ($FolderName[0] -ge "A") -and ($FolderName[0] -le "F") -or ($FolderName[0] -ge "a") -and ($FolderName[0] -le "f") )
{
$ClientFolder = "A-F"
}
elseif( ($FolderName[0] -ge "G") -and ($FolderName[0] -le "L") -or ($FolderName[0] -ge "g") -and ($FolderName[0] -le "l") )
{
$ClientFolder = "G-L"
}
elseif( ($FolderName[0] -ge "M") -and ($FolderName[0] -le "R") -or ($FolderName[0] -ge "m") -and ($FolderName[0] -le "r") )
{
$ClientFolder = "M-R"
}
elseif( ($FolderName[0] -ge "S") -and ($FolderName[0] -le "Z") -or ($FolderName[0] -ge "s") -and ($FolderName[0] -le "z") )
{
$ClientFolder = "S-Z"
}
$var_Checkbox_1.Add_Click( {
if($Checkbox_1.IsChecked -eq $true)
{
$Checkbox_2.IsEnabled = $false;
$Checkbox_2.IsChecked = $false;
$Checkbox_3.IsEnabled = $false;
$Checkbox_3.IsChecked = $false;
$Checkbox_4.IsEnabled = $false;
$Checkbox_4.IsChecked = $false;
$Checkbox_5.IsEnabled = $false;
$Checkbox_5.IsChecked = $false;
}
else
{
$Checkbox_2.IsEnabled = $true;
$Checkbox_3.IsEnabled = $true;
$Checkbox_4.IsEnabled = $true;
$Checkbox_5.IsEnabled = $true;
}
} )
$var_Submit_Button.Add_Click( {
ClientFolderTicketFolderAndPlatform -FolderName $var_Client_Name.Text -TicketNumber $var_Ticket_Number.Text -PlatformFiles $var_Platform_Selection.Text
PlatformFileCopy -PlatformFiles $PlatformFiles
} )
#Function Code for XAML GUI
Function ClientFolderTicketFolderAndPlatform {
[CmdletBinding()]
param (
[Parameter(Mandatory)]
$FolderName,
$TicketNumber,
$PlatformFiles
)
if( ($FolderName[0] -ge "A") -and ($FolderName[0] -le "F") -or ($FolderName[0] -ge "a") -and ($FolderName[0] -le "f") )
{
$ClientFolder = "A-F"
}
elseif( ($FolderName[0] -ge "G") -and ($FolderName[0] -le "L") -or ($FolderName[0] -ge "g") -and ($FolderName[0] -le "l") )
{
$ClientFolder = "G-L"
}
elseif( ($FolderName[0] -ge "M") -and ($FolderName[0] -le "R") -or ($FolderName[0] -ge "m") -and ($FolderName[0] -le "r") )
{
$ClientFolder = "M-R"
}
elseif( ($FolderName[0] -ge "S") -and ($FolderName[0] -le "Z") -or ($FolderName[0] -ge "s") -and ($FolderName[0] -le "z") )
{
$ClientFolder = "S-Z"
}
$ClientFolderExists = Get-PnPFolder -Url "Sharepoint URL for folder check"
if($ClientFolderExists -eq $null)
{
$var_Results_Output.Text = $var_Results_Output.Text + "Client folder for $FolderName does not exits. Creating new client folder.`n"
$RootFolder = "Root sharepoint folder where new folder will live"
Add-PnPFolder -Name $FolderName -Folder $RootFolder
$var_Results_Output.Text = $var_Results_Output.Text + "Client folder has been created for $FolderName.`n"
}
elseif($ClientFolderExists -ne $null)
{
$var_Results_Output.Text = $var_Results_Output.Text + "Client folder already exists for $FolderName.`n"
}
$TicketNumber = $TicketNumber + "_" + $UserInitials
$TicketFolderExists = Get-PnPFolder -Url "Sharepoint URL of ticket folder"
if($TicketFolderExists -eq $null)
{
$var_Results_Output.Text = $var_Results_Output.Text + "Ticket folder does not exist. Creating new ticket folder for $TicketNumber.`n"
$TicketFolderLocation = "URL of ticket folder location"
Add-PnPFolder -Name $TicketNumber -Folder $TicketFolderLocation
$var_Results_Output.Text = $var_Results_Output.Text + "Ticket folder has been created for $TicketNumber.`n"
}
elseif($TicketFolderExists -ne $null)
{
$var_Results_Output.Text = $var_Results_Output.Text + "Ticket folder already exists for $TicketNumber.`n"
}
}
Function PlatformFileCopy {
[CmdletBinding()]
param (
[Parameter(Mandatory)]
$PlatformFiles
)
$var_Results_Output.Text = $var_Results_Output.Text + "Your selected platform is $PlatformFiles.`n"
$var_Results_Output.Text = $var_Results_Output.Text + "System will now copy files for $PlatformFiles to the ticket folder.`n"
if($PlatformFiles -eq "Platform 1" -and $var_Checkbox_1.IsChecked -eq $true)
{
Copy-PnPFile -SourceUrl "Source URL of target file" -TargetUrl "URL path to the folder to that the coppied file is going to be in"
$var_Results_Output.Text = $var_Results_Output.Text + "File copying has been completed.`n"
}
$Null = $window.ShowDialog()
**End of Powershell script code**
**WPF code .xaml file:**
<Window x:Class="TestWPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="Test WFP UI" Height="450" Width="800">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBox x:Name="Clien_Name" HorizontalAlignment="Left" Margin="28,23,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<TextBox x:Name="Ticket_Number" HorizontalAlignment="Left" Margin="217,23,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<ComboBox x:Name="Platform_Selection" HorizontalAlignment="Left" Margin="26,79,0,0" VerticalAlignment="Top" Width="120">
<ListViewItem Content="Platform 1"/>
<ListViewItem Content="Platform 2"/>
<ListViewItem Content="Platform 3"/>
<ListViewItem Content="Platform 4"/>
<ListViewItem Content="Platform 5"/>
<ListViewItem Content="Platform 6"/>
</ComboBox>
<Label Content="Client Name" HorizontalAlignment="Left" Margin="49,0,0,0" VerticalAlignment="Top"/>
<Label Content="Ticket Number" HorizontalAlignment="Left" Margin="205,0,0,0" VerticalAlignment="Top"/>
<CheckBox x:Name="Checkbox_1" Content="Checkbox_1" HorizontalAlignment="Left" Margin="433,82,0,0" VerticalAlignment="Top"/>
<CheckBox x:Name="Checkbox_2" Content="Checkbox_2" HorizontalAlignment="Left" Margin="203,122,0,0" VerticalAlignment="Top"/>
<CheckBox x:Name="Checkbox_3" Content="Checkbox_3" HorizontalAlignment="Left" Margin="309,82,0,0" VerticalAlignment="Top"/>
<CheckBox x:Name="Checkbox_4" Content="Checkbox_4" HorizontalAlignment="Left" Margin="203,82,0,0" VerticalAlignment="Top"/>
<CheckBox x:Name="Checkbox_5" Content="Checkbox_5" HorizontalAlignment="Left" Margin="309,102,0,0" VerticalAlignment="Top"/>
<CheckBox x:Name="Checkbox_6" Content="Checkbox_6" HorizontalAlignment="Left" Margin="433,102,0,0" VerticalAlignment="Top"/>
<CheckBox x:Name="Checkbox_7" Content="Checkbox_7" HorizontalAlignment="Left" Margin="203,102,0,0" VerticalAlignment="Top"/>
<Label Content="Platform Selection" HorizontalAlignment="Left" Margin="33,48,0,0" VerticalAlignment="Top"/>
<Button x:Name="Submit_Button" Content="Submit" HorizontalAlignment="Left" Margin="668,379,0,0" VerticalAlignment="Top" Width="51"/>
<Button x:Name="Cancel_Button" Content="Cancel" HorizontalAlignment="Left" Margin="724,379,0,0" VerticalAlignment="Top" IsCancel="True" Width="50"/>
<TextBox x:Name="Results_Output" HorizontalAlignment="Center" Margin="0,142,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="747" Height="232" IsReadOnlyCaretVisible="True"/>
</Grid>
</Window>
**End of .xaml file code**
**xaml.cs file:**
using System.Windows;
namespace PartnerSecurity_Automation
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}