165 lines
11 KiB
PowerShell
165 lines
11 KiB
PowerShell
Function Remove-SpecialCharacter-withLogging {
|
||
|
||
<#
|
||
.SYNOPSIS
|
||
Function will replace SpecialCharacters in an input String.
|
||
|
||
.DESCRIPTION
|
||
Function will try to identify if the input String is a Path or File or Path+File.
|
||
In case of its an File, this characters will be replaced: \ / : * ? " < > |
|
||
In case of its an Path or Path+File, this characters will be replaced: / : * ? " < > |
|
||
|
||
.REQUIREMENT General
|
||
PowerShell V3
|
||
|
||
.REQUIREMENT Assembly
|
||
<NONE>
|
||
|
||
.REQUIREMENT Variables
|
||
StringWithSpecialCharacter, SpecialCharacters4Files, SpecialCharacters4Folders, ReplaceCharacter, FunctionName, SpecialCharacters4Folder, ArrayWithSpecialCharacter
|
||
|
||
.REQUIREMENT Variables preSet
|
||
<NONE>
|
||
|
||
.REQUIREMENT Functions
|
||
Write-LogFile
|
||
|
||
.VERSION
|
||
Number: 1.0.0.0 / Date: 17.03.2019
|
||
|
||
.PARAMETER StringWithSpecialCharacter
|
||
Give the String which contains SpecialCharacters (One or more).
|
||
|
||
.PARAMETER SpecialCharacters4Files
|
||
Give a array of not allowed Characters in Filenames (Default: \,/,:,*,?,",<,>,|))
|
||
|
||
.PARAMETER SpecialCharacters4Folders
|
||
Give a array of not allowed Characters in Paths (Default: /,:,*,?,",<,>,|))
|
||
|
||
.PARAMETER ReplaceCharacter
|
||
Give the Character which will be used to Replace the Special Characters (Default: Simply nothing)
|
||
|
||
.EXAMPLE
|
||
Remove-SpecialCharacter-withLogging -StringWithSpecialCharacter "\FileWith*SpecialCharacter/.Extension"
|
||
|
||
.EXAMPLE
|
||
Remove-SpecialCharacter-withLogging -StringWithSpecialCharacter "C:\PathWith\/SpecialCharacter"
|
||
|
||
#>
|
||
|
||
[cmdletbinding()]
|
||
|
||
Param (
|
||
|
||
[Parameter(Position=0,Mandatory=$True,HelpMessage='Give the String which contains SpecialCharacters (One or more).')]
|
||
[ValidateNotNullOrEmpty()]
|
||
[STRING]$StringWithSpecialCharacter,
|
||
|
||
[Parameter(Position=1,Mandatory=$False,HelpMessage='Give a array of not allowed Characters in Filenames (Default: \,/,:,*,?,",<,>,|))')]
|
||
[ValidateNotNullOrEmpty()]
|
||
[ARRAY]$SpecialCharacters4Files=@('\','/',':','*','?','"','<','>','|'),
|
||
|
||
[Parameter(Position=2,Mandatory=$False,HelpMessage='Give a array of not allowed Characters in Paths (Default: /,:,*,?,",<,>,|))')]
|
||
[ValidateNotNullOrEmpty()]
|
||
[ARRAY]$SpecialCharacters4Folders=@('/',':','*','?','"','<','>','|'),
|
||
|
||
[Parameter(Position=3,Mandatory=$False,HelpMessage='Give the Character which will be used to Replace the Special Characters (Default: Simply nothing)')]
|
||
[ValidateNotNullOrEmpty()]
|
||
[ARRAY]$ReplaceCharacter=""
|
||
|
||
) #end param
|
||
|
||
BEGIN {
|
||
|
||
#Clear Error Variable
|
||
$error.clear()
|
||
|
||
$FunctionName = $((($MyInvocation.MyCommand.Name) -split "\.")[0].ToString())
|
||
Write-Host "DEBUG Info - $($FunctionName): Begin Function."
|
||
|
||
#Checking if "Write-LogFile" Module was loaded
|
||
IF (Get-Module -All -Name "Write-LogFile") {
|
||
|
||
Write-Host "DEBUG Info - $($FunctionName): Write-LogFile - Module exists."
|
||
|
||
} #end if
|
||
|
||
ELSE {
|
||
|
||
Write-Host ""
|
||
Write-Host "DEBUG Info - $($FunctionName): Write-LogFile - Module does not exist!"
|
||
Write-Host "DEBUG Info - $($FunctionName): Please load the Module and try again, running this Function/Module!"
|
||
Write-Host "DEBUG Info - $($FunctionName): Exiting, because of this Issue."
|
||
EXIT
|
||
|
||
} #end else
|
||
|
||
} #end begin
|
||
|
||
PROCESS {
|
||
|
||
IF (($StringWithSpecialCharacter.get_Chars(0) -match "[\\]" ) -and ($StringWithSpecialCharacter.get_Chars(1) -match "[\\]")) {
|
||
|
||
Write-Host "DEBUG Info - $($FunctionName): Input String seems to be a UNC Network Path"
|
||
FOREACH ($SpecialCharacters4Folder in $SpecialCharacters4Folders) {
|
||
|
||
IF ($StringWithSpecialCharacter -match "\$SpecialCharacters4Folder") {
|
||
|
||
Write-Host "DEBUG Info - $($FunctionName): Found: $($SpecialCharacters4Folder)"
|
||
$StringWithSpecialCharacter = $StringWithSpecialCharacter -Replace("\$SpecialCharacters4Folder",$ReplaceCharacter)
|
||
Write-Host "DEBUG Info - $($FunctionName): Replaced it!"
|
||
|
||
} #end if
|
||
|
||
} #end foreach
|
||
|
||
} #end if
|
||
|
||
ELSEIF (($StringWithSpecialCharacter.get_Chars(0) -match "[a-zA-Z]" ) -and ($StringWithSpecialCharacter.get_Chars(1) -match "[\:]")) {
|
||
|
||
Write-Host "DEBUG Info - $($FunctionName): Input String seems to be a local Drive"
|
||
FOREACH ($SpecialCharacters4Folder in $SpecialCharacters4Folders) {
|
||
|
||
IF ($StringWithSpecialCharacter -match "\$SpecialCharacters4Folder") {
|
||
|
||
Write-Host "DEBUG Info - $($FunctionName): Found: $($SpecialCharacters4Folder)"
|
||
$ArrayWithSpecialCharacter = $StringWithSpecialCharacter -Split(":")
|
||
$StringWithSpecialCharacter = $ArrayWithSpecialCharacter[0]+":"+($ArrayWithSpecialCharacter[1] -Replace("\$SpecialCharacters4Folder",$ReplaceCharacter))
|
||
Write-Host "DEBUG Info - $($FunctionName): Replaced it!"
|
||
|
||
} #end if
|
||
|
||
} #end foreach
|
||
|
||
} #end elseif
|
||
|
||
ELSE {
|
||
|
||
Write-Host "DEBUG Info - $($FunctionName): Input String must be a Filename"
|
||
FOREACH ($SpecialCharacters4File in $SpecialCharacters4Files) {
|
||
|
||
IF ($StringWithSpecialCharacter -match "\$SpecialCharacters4File") {
|
||
|
||
Write-Host "DEBUG Info - $($FunctionName): Found: $($StandardSpecialCharacter)"
|
||
$StringWithSpecialCharacter = $StringWithSpecialCharacter -Replace("\$SpecialCharacters4File",$ReplaceCharacter)
|
||
Write-Host "DEBUG Info - $($FunctionName): Replaced it!"
|
||
|
||
} #end if
|
||
|
||
} #end foreach
|
||
|
||
} #end else
|
||
|
||
Write-Logfile -LogLine "Removed SpecialCharacters in: $StringWithSpecialCharacter"
|
||
|
||
RETURN [STRING]$StringWithSpecialCharacter
|
||
|
||
} #end process
|
||
|
||
END {
|
||
|
||
Write-Host "DEBUG Info - $($FunctionName): End Function."
|
||
|
||
} #end end
|
||
|
||
} #end function |