<commit_before>﻿Function Show-SqlServerFileSystem
{
<#
.SYNOPSIS
Shows file system on remote SQL Server and returns the directory name of the directory you select.
	
.DESCRIPTION
Similar to the remote file system popup you see when browsing a remote SQL Server in SQL Server Management Studio, this command allows you to traverse the remote SQL Server's file structure.

Show-SqlServerFileSystem uses SQL Management Objects to browse the directories and what you see is limited to the permissions of the account running the command.
	
.PARAMETER SqlServer
The SQL Server instance.
	
.PARAMETER SqlCredential
Allows you to login to servers using SQL Logins as opposed to Windows Auth/Integrated/Trusted. To use:

$scred = Get-Credential, then pass $scred object to the -SqlCredential parameter. 

Windows Authentication will be used if SqlCredential is not specified. SQL Server does not accept Windows credentials being passed as credentials. To connect as a different Windows user, run PowerShell as that user.

.NOTES 
dbatools PowerShell module (https://dbatools.io, <EMAIL>)
Copyright (C) 2016 <NAME>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

.LINK
https://dbatools.io/Show-SqlServerFileSystem

.EXAMPLE
Show-SqlServerFileSystem -SqlServer sqlserver2014a

Shows a GUI and uses Windows Authentication to log into the SQL Server. Returns a string of the path you selected.
	
.EXAMPLE   
Show-SqlServerFileSystem -Source sqlserver2014a -Destination sqlcluster -SqlCredential $cred

Shows a GUI and uses SQL credentials to log into the SQL Server. Returns a string of the path you selected.
	
#>
	[CmdletBinding()]
	Param (
		[parameter(Mandatory = $true, ValueFromPipeline = $true)]
		[Alias("ServerInstance", "SqlInstance")]
		[object]$SqlServer,
		[object]$SqlCredential
	)
	
	BEGIN
	{
		try { Add-Type -AssemblyName PresentationFramework }
		catch { throw "Windows Presentation Framework required but not installed" }
		
		Function Add-TreeItem
		{
			Param (
				[string]$name,
				[object]$parent,
				[string]$tag
			)
			
			$childitem = New-Object System.Windows.Controls.TreeViewItem
			
			$textblock = New-Object System.Windows.Controls.TextBlock
			$textblock.Margin = "5,0"
			
			$stackpanel = New-Object System.Windows.Controls.StackPanel
			$stackpanel.Orientation = "Horizontal"
			
			$image = New-Object System.Windows.Controls.Image
			$image.Height = 20
			$image.Width = 20
			$image.Stretch = "Fill"
			
			if ($name.length -eq 1)
			{
				$image.Source = $diskicon
				$textblock.Text = "$name`:"
				$childitem.Tag = "$name`:"
				
			}
			else
			{
				$image.Source = $foldericon
				$textblock.Text = $name
				$childitem.Tag = "$tag\$name"
			}
			
			[void]$stackpanel.Children.Add($image)
			[void]$stackpanel.Children.Add($textblock)
			
			$childitem.Header = $stackpanel
			
			[void]$childitem.Items.Add("*")
			[void]$parent.Items.Add($childitem)
		}
		
		Function Get-SubDirectory
		{
			Param (
				[string]$nameSpace,
				[object]$treeviewItem
			)
			
			$textbox.Text = $nameSpace
			try { $dirs = $sourceserver.EnumDirectories($nameSpace) }
			catch { return }
			$subdirs = $dirs.Name
			
			foreach ($subdir in $subdirs)
			{
				if (!$subdir.StartsWith("$") -and $subdir -ne 'System Volume Information')
				{
					Add-TreeItem -Name $subdir -Parent $treeviewItem -Tag $nameSpace
				}
			}
		}
		
		Function Convert-b64toimg
		{
			param ($base64)
			
			$bitmap = New-Object System.Windows.Media.Imaging.BitmapImage
			$bitmap.BeginInit()
			$bitmap.StreamSource = [System.IO.MemoryStream][System.Convert]::FromBase64String($base64)
			$bitmap.EndInit()
			$bitmap.Freeze()
			return $bitmap
		}
		
		$diskicon = Convert-b64toimg "<KEY>YPE<KEY>
		$foldericon = Convert-b64toimg "<KEY>
		$dbatoolsicon = Convert-b64toimg "<KEY>
		
		$sourceserver = Connect-SqlServer -SqlServer $SqlServer -SqlCredential $SourceSqlCredential
	}
	
	PROCESS
	{		
		# Create XAML form in Visual Studio, ensuring the ListView looks chromeless 
		[xml]$xaml = '<Window 
		xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
		xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        Title="Locate Folder" Height="620" Width="440" Background="#F0F0F0"
		WindowStartupLocation="CenterScreen">
    <Grid>
        <TreeView Name="treeview" Height="462" Width="391" Background="#FFFFFF" BorderBrush="#FFFFFF" Foreground="#FFFFFF" Margin="11,36,11,79"/>
        <Label x:Name="label" Content="Select the folder:" HorizontalAlignment="Left" Margin="15,4,0,0" VerticalAlignment="Top"/>
        <Label x:Name="path" Content="Selected Path" HorizontalAlignment="Left" Margin="15,502,0,0" VerticalAlignment="Top"/>
        <TextBox Name="textbox" HorizontalAlignment="Left" Height="23" Margin="111,504,0,0" TextWrapping="Wrap" Text="C:\blah" VerticalAlignment="Top" Width="292"/>
        <Button Name="okbutton" Content="OK" HorizontalAlignment="Left" Margin="241,540,0,0" VerticalAlignment="Top" Width="75"/>
        <Button Name="cancelbutton" Content="Cancel" HorizontalAlignment="Left" Margin="328.766,540,0,0" VerticalAlignment="Top" Width="75"/><commit_msg>fix some typos<commit_after>﻿Function Show-SqlServerFileSystem
{
<#
.SYNOPSIS
Shows file system on remote SQL Server and returns the directory name of the directory you select.
	
.DESCRIPTION
Similar to the remote file system popup you see when browsing a remote SQL Server in SQL Server Management Studio, this command allows you to traverse the remote SQL Server's file structure.

Show-SqlServerFileSystem uses SQL Management Objects to browse the directories and what you see is limited to the permissions of the account running the command.
	
.PARAMETER SqlServer
The SQL Server instance.
	
.PARAMETER SqlCredential
Allows you to login to servers using SQL Logins as opposed to Windows Auth/Integrated/Trusted. To use:

$scred = Get-Credential, then pass $scred object to the -SqlCredential parameter. 

Windows Authentication will be used if SqlCredential is not specified. SQL Server does not accept Windows credentials being passed as credentials. To connect as a different Windows user, run PowerShell as that user.

.NOTES 
dbatools PowerShell module (https://dbatools.io, <EMAIL>)
Copyright (C) 2016 <NAME>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

.LINK
https://dbatools.io/Show-SqlServerFileSystem

.EXAMPLE
Show-SqlServerFileSystem -SqlServer sqlserver2014a

Shows a GUI and uses Windows Authentication to log into the SQL Server. Returns a string of the path you selected.
	
.EXAMPLE   
Show-SqlServerFileSystem -Source sqlserver2014a -SqlCredential $cred

Shows a GUI and uses SQL credentials to log into the SQL Server. Returns a string of the path you selected.
	
#>
	[CmdletBinding()]
	Param (
		[parameter(Mandatory = $true, ValueFromPipeline = $true)]
		[Alias("ServerInstance", "SqlInstance")]
		[object]$SqlServer,
		[object]$SqlCredential
	)
	
	BEGIN
	{
		try { Add-Type -AssemblyName PresentationFramework }
		catch { throw "Windows Presentation Framework required but not installed" }
		
		Function Add-TreeItem
		{
			Param (
				[string]$name,
				[object]$parent,
				[string]$tag
			)
			
			$childitem = New-Object System.Windows.Controls.TreeViewItem
			
			$textblock = New-Object System.Windows.Controls.TextBlock
			$textblock.Margin = "5,0"
			
			$stackpanel = New-Object System.Windows.Controls.StackPanel
			$stackpanel.Orientation = "Horizontal"
			
			$image = New-Object System.Windows.Controls.Image
			$image.Height = 20
			$image.Width = 20
			$image.Stretch = "Fill"
			
			if ($name.length -eq 1)
			{
				$image.Source = $diskicon
				$textblock.Text = "$name`:"
				$childitem.Tag = "$name`:"
				
			}
			else
			{
				$image.Source = $foldericon
				$textblock.Text = $name
				$childitem.Tag = "$tag\$name"
			}
			
			[void]$stackpanel.Children.Add($image)
			[void]$stackpanel.Children.Add($textblock)
			
			$childitem.Header = $stackpanel
			
			[void]$childitem.Items.Add("*")
			[void]$parent.Items.Add($childitem)
		}
		
		Function Get-SubDirectory
		{
			Param (
				[string]$nameSpace,
				[object]$treeviewItem
			)
			
			$textbox.Text = $nameSpace
			try { $dirs = $sourceserver.EnumDirectories($nameSpace) }
			catch { return }
			$subdirs = $dirs.Name
			
			foreach ($subdir in $subdirs)
			{
				if (!$subdir.StartsWith("$") -and $subdir -ne 'System Volume Information')
				{
					Add-TreeItem -Name $subdir -Parent $treeviewItem -Tag $nameSpace
				}
			}
		}
		
		Function Convert-b64toimg
		{
			param ($base64)
			
			$bitmap = New-Object System.Windows.Media.Imaging.BitmapImage
			$bitmap.BeginInit()
			$bitmap.StreamSource = [System.IO.MemoryStream][System.Convert]::FromBase64String($base64)
			$bitmap.EndInit()
			$bitmap.Freeze()
			return $bitmap
		}
		
		$diskicon = Convert-b64toimg "<KEY>
		$foldericon = Convert-b64toimg "<KEY>
		$dbatoolsicon = Convert-b64toimg "<KEY>
		
		$sourceserver = Connect-SqlServer -SqlServer $SqlServer -SqlCredential $SourceSqlCredential
	}
	
	PROCESS
	{		
		# Create XAML form in Visual Studio, ensuring the ListView looks chromeless 
		[xml]$xaml = '<Window 
		xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
		xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        Title="Locate Folder" Height="620" Width="440" Background="#F0F0F0"
		WindowStartupLocation="CenterScreen">
    <Grid>
        <TreeView Name="treeview" Height="462" Width="391" Background="#FFFFFF" BorderBrush="#FFFFFF" Foreground="#FFFFFF" Margin="11,36,11,79"/>
        <Label x:Name="label" Content="Select the folder:" HorizontalAlignment="Left" Margin="15,4,0,0" VerticalAlignment="Top"/>
        <Label x:Name="path" Content="Selected Path" HorizontalAlignment="Left" Margin="15,502,0,0" VerticalAlignment="Top"/>
        <TextBox Name="textbox" HorizontalAlignment="Left" Height="23" Margin="111,504,0,0" TextWrapping="Wrap" Text="C:\" VerticalAlignment="Top" Width="292"/>
        <Button Name="okbutton" Content="OK" HorizontalAlignment="Left" Margin="241,540,0,0" VerticalAlignment="Top" Width="75"/>
        <Button Name="cancelbutton" Content="Cancel" HorizontalAlignment="Left" Margin="328.766,540,0,0" VerticalAlignment="Top" Width="75"/>
{'fraction_non_alphanumeric': 0.10869372954806757, 'fraction_numerical': 0.019987618289555142, 'mean_word_length': 4.05272564789991, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 34, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3615027', 'n_tokens_mistral': 3972, 'n_tokens_neox': 3356, 'n_words': 1029}