Skip to content

Commit

Permalink
Merge pull request #3 from Akaizoku/develop
Browse files Browse the repository at this point in the history
Improvements
  • Loading branch information
Akaizoku authored Jan 24, 2020
2 parents 2d058a4 + 329a01c commit 2a591ce
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 51 deletions.
1 change: 1 addition & 0 deletions PSTK.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ FunctionsToExport = @(
"Complete-RelativePath",
"Confirm-Prompt",
"Convert-FileEncoding",
"ConvertTo-JavaProperty",
"ConvertTo-NaturalSort",
"ConvertTo-PDF",
"Copy-OrderedHashtable",
Expand Down
70 changes: 70 additions & 0 deletions Public/ConvertTo-JavaProperty.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
function ConvertTo-JavaProperty {
<#
.SYNOPSIS
Convert to Java property
.DESCRIPTION
Format property key-value pair as Java property
.PARAMETER Property
The property parameter corresponds to the property key-value pair to format.
.PARAMETER Prefix
The optional prefix parameter corresponds to the prefix of the expected Java property. The default value is "D".
.INPUTS
System.Collections.Specialized.OrderedDictionary. You can pipe the property key-value pair to ConvertTo-JavaProperty.
.OUTPUTS
System.String. ConvertTo-JavaProperty returns the formatted Java property.
.NOTES
File name: ConvertTo-JavaProperty.ps1
Author: Florian Carrier
Creation date: 15/10/2019
Last modified: 16/01/2020
#>
[CmdletBinding ()]
Param(
[Parameter (
Position = 1,
Mandatory = $true,
HelpMessage = "Property key-value pair",
ValueFromPipeline = $true
)]
[ValidateNotNullOrEmpty()]
[Alias ("Properties")]
[System.Collections.Specialized.OrderedDictionary]
$Property,
[Parameter (
Position = 2,
Mandatory = $false,
HelpMessage = "Java property prefix"
)]
[String]
$Prefix = "D"
)
Begin {
# Get global preference variables
Get-CallerPreference -Cmdlet $PSCmdlet -SessionState $ExecutionContext.SessionState
}
Process {
if ($Property.Count -gt 0) {
$JavaString = New-Object -TypeName "System.String" -ArgumentList ""
# Loop through properties (if multiple have been provided)
foreach ($SingleProperty in $Property.GetEnumerator()) {
# Add space between consecutive properties
if ($JavaString) {
$JavaString += " "
}
# Format property
$JavaString += "-" + $Prefix + '"' + $SingleProperty.Name + '"' + "=" + '"' + $SingleProperty.Value + '"'
}
# Return Java property string
return $JavaString
} else {
# If property is invalid
return $null
}
}
}
68 changes: 65 additions & 3 deletions Public/Resolve-URI.ps1
Original file line number Diff line number Diff line change
@@ -1,28 +1,90 @@
function Resolve-URI {
<#
.SYNOPSIS
Resolve URI
.DESCRIPTION
Parse a unform resource identifier (URI) and resolve restricted and reserved characters
.PARAMETER URI
The URI parameter corresponds to the uniform resource identifier to resolve.
.INPUTS
System.String. You can pipe the uniform resource identifier to Resolve-URI.DESCRIPTION
.OUTPUTS
System.String. Resolve-URI returns the encoded uniform resource identifier.
File name: Resolve-URI.ps1
Author: Florian Carrier
Creation date: 12/12/2018
Last modified: 16/01/2020
.LINK
https://en.wikipedia.org/wiki/Uniform_Resource_Identifier
.LINK
https://en.wikipedia.org/wiki/Percent-encoding
#>
[CmdletBinding ()]
Param (
[Parameter (
Position = 1,
Mandatory = $true,
HelpMessage = "URI to resolve"
HelpMessage = "URI to resolve",
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true
)]
[String]
$URI
$URI,
[Parameter (
HelpMessage = "Switch to limit parsing to restricted characters"
)]
[Switch]
$RestrictedOnly
)
Begin {
# Get global preference variables
Get-CallerPreference -Cmdlet $PSCmdlet -SessionState $ExecutionContext.SessionState
# List of restricted characters
$RestrictedCharacters = [Ordered]@{
"\" = '/'
"%" = '%25'
}
# List of reserved characters
$ReservedCharacters = [Ordered]@{
" " = '%20'
"," = '%2C'
";" = '%3B'
":" = '%3A'
"!" = '%21'
"?" = '%3F'
"'" = '%27'
"(" = '%28'
")" = '%29'
"[" = '%5B'
"]" = '%5D'
"@" = '%40'
"*" = '%2A'
"/" = '%2F'
"&" = '%26'
"#" = '%23'
"+" = '%2B'
"=" = '%3D'
"$" = '%24'
}
}
Process {
# Encode URI
# Encode restricted characters
foreach ($RestrictedCharacter in $RestrictedCharacters.GetEnumerator()) {
$URI = $URI.Replace($RestrictedCharacter.Key, $RestrictedCharacter.Value)
}
# Encode reserved characters
if (-Not $RestrictedOnly) {
foreach ($ReservedCharacter in $ReservedCharacters.GetEnumerator()) {
$URI = $URI.Replace($ReservedCharacter.Key, $ReservedCharacter.Value)
}
}
# Return encoded URI
return $URI
}
Expand Down
74 changes: 34 additions & 40 deletions Public/Test-SQLConnection.ps1
Original file line number Diff line number Diff line change
@@ -1,71 +1,59 @@
# ------------------------------------------------------------------------------
# Database connection testing function
# ------------------------------------------------------------------------------
function Test-SQLConnection {
<#
.SYNOPSIS
Check a SQL Server database connection
Test SQL Server database connection
.DESCRIPTION
Check that a SQL Server database connection is working
Check that a SQL Server database connection is working.
.PARAMETER Server
[String] The server parameter corresponds to the database server to connect to
The server parameter corresponds to the database server to connect to.
.PARAMETER Database
[String] The database parameter corresponds to the database to be tested
.PARAMETER Security
[DEPRECATED] The security parameter defines if the connection should be made us-
ing the SQL Server Integrated Security (Windows Active Directory) or the
default SQL authentication with username and password.
The database parameter corresponds to the database to be tested.
.PARAMETER Username
[String] The username parameter corresponds to the username of the account
to use in case of SQL authentication.
The username parameter corresponds to the username of the account to use in case of SQL authentication.
.PARAMETER Password
[String] The password parameter corresponds to the password of the account
to use in case of SQL authentication.
The password parameter corresponds to the password of the account to use in case of SQL authentication.
.PARAMETER Credentials
[System.Management.Automation.PSCredential] The credentials parameter corresponds to the credentials of account
to use in case of SQL authentication.
The credentials parameter corresponds to the credentials of accoun to use in case of SQL authentication.
.PARAMETER TimeOut
The optional time-out parameter corresponds to the time in seconds before the connection is deemed unresponsive. The default value is 3 seconds.
.PARAMETER Security
[DEPRECATED] The security parameter defines if the connection should be made using the SQL Server Integrated Security (Windows Active Directory) or the default SQL authentication with username and password.
.INPUTS
None. You cannot pipe objects to Test-SQLConnection.
.OUTPUTS
[Boolean] Test-SQLConnection returns a boolean depending on the result of the
connection attempt.
Boolean. Test-SQLConnection returns a boolean depending on the result of the connection attempt.
.EXAMPLE
Test-SQLConnection -Server "localhost" -Database "MSSQLServer"
In this example, Test-SQLConnection will try to connect to the MSSQLServer
database on the local server using the current Windows user.
In this example, Test-SQLConnection will try to connect to the database "MSSQLServer" on the server "localhost" using the current Windows user.
.EXAMPLE
Test-SQLConnection -Server "localhost" -Database "MSSQLServer" -Username "user" -Password "password"
In this example, Test-SQLConnection will try to connect to the MSSQLServer
database on the local server using the credentials of the user "user" with
the "password" password.
In this example, Test-SQLConnection will try to connect to the database "MSSQLServer" on the server "localhost" using the credentials of the user "user" with the password "password".
.EXAMPLE
$SecurePassword = ConvertTo-SecureString -String "password" -AsPlainText -Force
$Credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList ("user", $SecurePassword)
$Credentials = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList ("user", (ConvertTo-SecureString -String "password" -AsPlainText -Force))
Test-SQLConnection -Server "localhost" -Database "MSSQLServer" -Credentials $Credentials
In this example, Test-SQLConnection will try to connect to the MSSQLServer
database on the local server using the credentials of the user "user" with
the "password" password.
In this example, Test-SQLConnection will try to connect to the database "MSSQLServer" on the server "localhost" using the credentials of the user "user" with the password "password".
.NOTES
File name: Test-SQLConnection.ps1
Author: Florian Carrier
Creation date: 15/10/2018
Last modified: 02/12/2019
Last modified: 15/01/2020
Dependencies: Test-SQLConnection requires the SQLServer module
.LINK
Expand Down Expand Up @@ -117,6 +105,14 @@ function Test-SQLConnection {
)]
[System.Management.Automation.PSCredential]
$Credentials,
[Parameter (
Position = 7,
Mandatory = $false,
HelpMessage = "Connection timeout (in seconds)"
)]
[ValidateNotNullOrEmpty ()]
[Int]
$TimeOut = 3,
[Parameter (
HelpMessage = "[DEPRECATED] Use of specific credentials instead of integrated security"
)]
Expand All @@ -129,29 +125,27 @@ function Test-SQLConnection {
}
Process {
# Define connection string
$ConnectionString = "Server=$Server; Database=$Database; Connect Timeout=3;"
$ConnectionString = "Server=$Server;Database=$Database;Connect Timeout=$TimeOut;"
# Check authentication mode
if ($PSBoundParameters.ContainsKey("Credentials")) {
# If "secured" credentials are provided
$FullConnectionString = $ConnectionString + "Integrated Security=False; User ID=$($Credentials.Username); Password=$($Credentials.GetNetworkCredential().Password);"
$Obfuscate = $Credentials.GetNetworkCredential().Password
$FullConnectionString = $ConnectionString + "Integrated Security=False;User ID=$($Credentials.Username);Password=$($Credentials.GetNetworkCredential().Password);"
$SensitiveData = $Credentials.GetNetworkCredential().Password
} elseif ($PSBoundParameters.ContainsKey("Username") -And $PSBoundParameters.ContainsKey("Password")) {
# If plain text credentials are provided
if ($Username) {
$FullConnectionString = $ConnectionString + "Integrated Security=False; User ID=$Username; Password=$Password;"
$Obfuscate = $Password
$FullConnectionString = $ConnectionString + "Integrated Security=False;User ID=$Username;Password=$Password;"
$SensitiveData = $Password
} else {
Write-Log -Type "ERROR" -Message "Please provide a valid username"
Write-Log -Type "DEBUG" -Message "$Username"
Stop-Script 1
Write-Log -Type "ERROR" -Message "Invalid username ""$Username""" -ExitCode 1
}
} else {
# Else default to integrated security (Windows authentication)
Write-Log -Type "DEBUG" -Message "Integrated Security"
$FullConnectionString = $ConnectionString + "Integrated Security=True;"
}
# Create connection object
Write-Log -Type "DEBUG" -Object $FullConnectionString -Obfuscate $Obfuscate
Write-Log -Type "DEBUG" -Object $FullConnectionString -Obfuscate $SensitiveData
$Connection = New-Object -TypeName "System.Data.SqlClient.SqlConnection" -ArgumentList $FullConnectionString
# Try to open the connection
try {
Expand Down
28 changes: 20 additions & 8 deletions Public/Write-Checksum.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@ function Write-Checksum {
- SHA512 : Secure Hash Algorithm 2 (512)
- MD5 : MD5 message-digest algorithm
.PARAMETER Filter
The filter parameter corresponds to the pattern to use to filter files if the path provided is a directory.
If not specified, by default all files will be selected.
.PARAMETER OutputDirectory
The output directory parameter corresponds to the directory in which to generate the file(s) containing the checksum value(s).
If not specified, the default output location is the same as the file(s) analyses.
.PARAMETER Filter
The optional filter parameter corresponds to the pattern to use to filter files if the path provided is a directory.
If not specified, by default all files will be selected.
.PARAMETER Exclude
The optional exclude parameter corresponds to the pattern to use to exclude files if the path provided is a directory.
If not specified, by default all files will be selected.
.EXAMPLE
Write-Checksum -Path "C:\Files" -Algorithm "MD5" -Filter "*.zip"
Expand Down Expand Up @@ -80,19 +84,27 @@ function Write-Checksum {
[Parameter (
Position = 3,
Mandatory = $false,
HelpMessage = "Directory in which to save generate checksum files"
)]
[ValidateNotNullOrEmpty ()]
[String]
$OutputDirectory,
[Parameter (
Position = 4,
Mandatory = $false,
HelpMessage = "Filter to apply in case of directory"
)]
[ValidateNotNullOrEmpty ()]
[String]
$Filter = "*",
[Parameter (
Position = 4,
Position = 5,
Mandatory = $false,
HelpMessage = "Directory in which to save generate checksum files"
HelpMessage = "Pattern for exclusion"
)]
[ValidateNotNullOrEmpty ()]
[String]
$OutputDirectory
$Exclude = ""
)
Begin {
# Get global preference vrariables
Expand Down Expand Up @@ -121,7 +133,7 @@ function Write-Checksum {
}
}
Process {
$Files = Get-ChildItem -Path $Path -Filter $Filter
$Files = Get-ChildItem -Path $Path -Filter $Filter -Exclude $Exclude
foreach ($File in $Files) {
# Get file hash
$FileHash = Get-FileHash -Path $File.FullName -Algorithm $Algorithm
Expand Down

0 comments on commit 2a591ce

Please sign in to comment.