Skip to content

Commit

Permalink
Merge pull request #8 from muratiakos/master
Browse files Browse the repository at this point in the history
Release 1.6
  • Loading branch information
muratiakos authored Jul 15, 2016
2 parents 42f5a86 + 09989a9 commit ad1fd7e
Show file tree
Hide file tree
Showing 15 changed files with 164 additions and 54 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,9 @@
## 1.5.2 (Feb 28, 2016)
- Refactor Pester tests and Fixtures
- Reorganize functions into subfolders

## 1.6.0 (Jul 15, 2016)
- Enable remote executions with resolving RunspaceIds without specifying -Session parameter
- Add password-masking to Test-PSConnectionString
- Test-PsWebConfig now support -IncludeAppsettings parameters
- Better detailed -Verbose info from all cmdlets
3 changes: 3 additions & 0 deletions Functions/PSAppSetting/Get-PSAppSetting.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ function Get-PSAppSetting {
)

process {
Write-Verbose "Executing Get-PSAppSetting"
foreach ($config in $ConfigXml) {
Write-Verbose "Processing configuration '$($config.ComputerName + " " + $config.File)'"
if ($config -is [string]) { $config = [xml]$config }

if ($config | Get-Member -Name configuration) {
if ($config.configuration.appSettings.EncryptedData) {
Write-Warning "appSettings section is encrypted. You may not see all relevant entries."
Write-Warning "Execute this command as an administrator for automatic decryption."
}

foreach ($appSetting in $config.configuration.appSettings.add) {
Expand Down
9 changes: 8 additions & 1 deletion Functions/PSConnectionString/Get-PSConnectionString.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@ function Get-PSConnectionString {
)

process {
Write-Verbose "Executing Get-PSConnectionString"
foreach ($config in $ConfigXml) {
if ($config -is [string]) { $config = [xml]$config }

if ($config | Get-Member -Name configuration) {
Write-Verbose "Processing configuration '$($config.ComputerName + " " + $config.File)'"
if ($config.configuration.connectionStrings.EncryptedData) {
Write-Warning "ConnectionStrings section is encrypted. You may not see all relevant entries."
Write-Warning "Execute this command as an administrator for automatic decryption."
}

foreach ($connectionString in $config.configuration.connectionStrings.add) {
Expand All @@ -46,8 +49,12 @@ function Get-PSConnectionString {
Set_Type -TypeName "PSWebConfig.ConnectionString"
}

if (-Not $IncludeAppSettings) { continue }
if (-Not $IncludeAppSettings) {
Write-Verbose "Appsettings are not included in ConnectionString processing."
continue
}

Write-Verbose "Looking for ConnectionStrings in appsettings.."
$config | Get-PSAppSetting | ForEach-Object {
if ($_.value -match 'data source=') {
$_ |
Expand Down
18 changes: 15 additions & 3 deletions Functions/PSConnectionString/Test-PSConnectionString.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
If -ReplaceRules hashtable is specified it will replace hash-keys with it's
values in the ConnectionString to be tested.
Passwords in the ConnectionString are masked by default, use -ShowPassword
switch if you need to show passwords in test-results.
.PARAMETER InputObject
Mandatory - Pipeline input of PsConnectionString
Expand All @@ -24,6 +27,9 @@
Optional - Hashtable that replaces hash-keys with it's values in the
ConnectionString to be tested.
.PARAMETER ShowPassword
Optional - Switch to disable password masking for the test result.
.PARAMETER Session
Optional - PSSession to execute the test-against it.
Expand All @@ -42,10 +48,13 @@ function Test-PSConnectionString {
[string]$ConnectionString,

[hashtable]$ReplaceRules,
[System.Management.Automation.Runspaces.PSSession[]]$Session
[System.Management.Automation.Runspaces.PSSession[]]$Session,

[switch]$ShowPassword
)

process {
Write-Verbose "Executing Test-PSConnectionString"
if ($ConnectionString) {
$InputObject = New-Object -TypeName PsObject -Property @{
ConnectionString = $ConnectionString
Expand All @@ -59,16 +68,19 @@ function Test-PSConnectionString {
$EntrySession = $entry.Session
if ($Session) { $EntrySession = $Session }

$ArgumentList = $entry.ConnectionString,$ReplaceRules,$ShowPassword
if ($EntrySession) {
Write-Verbose "Remote Test execution from '$($EntrySession.ComputerName)'"
Invoke-Command `
-Session $EntrySession `
-ArgumentList $entry.ConnectionString,$ReplaceRules `
-ArgumentList $ArgumentList `
-ScriptBlock $Function:Test_ConnectionString |
Add-Member -NotePropertyName Session -NotePropertyValue $EntrySession -Force -PassThru |
Set_Type -TypeName 'PSWebConfig.TestResult'
} else {
Write-Verbose "Local Test execution"
Invoke-Command `
-ArgumentList $entry.ConnectionString,$ReplaceRules `
-ArgumentList $ArgumentList `
-ScriptBlock $Function:Test_ConnectionString |
Set_Type -TypeName 'PSWebConfig.TestResult'
}
Expand Down
13 changes: 12 additions & 1 deletion Functions/PSConnectionString/Test_ConnectionString.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ function Test_ConnectionString {
[string]$ConnectionString,

[Parameter(Position=1)]
[hashtable]$ReplaceRules
[hashtable]$ReplaceRules,

[Parameter(Position=2)]
[bool]$ShowPassword=$false
)

$result = New-Object PsObject -Property @{
Expand Down Expand Up @@ -55,5 +58,13 @@ function Test_ConnectionString {
} finally {
if ($SqlConnection) { $SqlConnection.Close() }
}

if (!$ShowPassword) {
Write-Verbose "Masking password due to ShowPassword:false .."
$maskRule = ';\s*password=(\S+)\s*(;|$)',';password=***;'
$result.ConnectionString = $result.ConnectionString -ireplace $maskRule
$result.Test = $result.Test -ireplace $maskRule
}

return $result
}
4 changes: 4 additions & 0 deletions Functions/PSUri/Get-PSEndpoint.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ function Get-PSEndpoint {
)

process {
Write-Verbose "Executing Get-PSEndpoint"
foreach ($config in $ConfigXml) {
Write-Verbose "Processing configuration '$($config.ComputerName + " " + $config.File)'"
if ($config | Get-Member -Name configuration) {
foreach ($endpoint in $config.configuration.'system.serviceModel'.client.endpoint) {
$endpoint |
Expand All @@ -32,6 +34,8 @@ function Get-PSEndpoint {
Add-Member -MemberType AliasProperty -Name Uri -Value address -Force -PassThru |
Set_Type -TypeName 'PSWebConfig.Uri'
}
} else {
Write-Warning "Skipping invalid configuration: $config"
}
}
}
Expand Down
28 changes: 19 additions & 9 deletions Functions/PSUri/Get-PSUri.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
.PARAMETER ConfigXml
Mandatory - Pipeline input for Configuration XML
.PARAMETER IncludeAppSettings
Optional - Switch to include http/s URIs from appsettings
.EXAMPLE
Get-PSWebConfig -Path 'C:\inetpub\wwwroot\myapp' | Get-PSUri
Expand All @@ -19,19 +21,27 @@ function Get-PSUri {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true,ValueFromPipeLine=$true)]
[psobject[]]$ConfigXml
[psobject[]]$ConfigXml,
[switch]$IncludeAppSettings
)

process {
# Return all service endpoint addresses
Write-Verbose "Executing Get-PSUri"

Write-Verbose "Looking for service-endpoint addresses..."
Get-PSEndpoint -ConfigXml $configXml

# Return any URL from appSettings as an Address
Get-PSAppSetting -ConfigXml $configXml |
Where-Object value -imatch '^http[s]*:' |
Add-Member -MemberType AliasProperty -Name name -Value key -Force -PassThru |
Add-Member -MemberType AliasProperty -Name address -Value value -Force -PassThru |
Add-Member -MemberType AliasProperty -Name Uri -Value value -Force -PassThru |
Set_Type -TypeName 'PSWebConfig.Uri'
if (-Not $IncludeAppSettings) {
Write-Verbose "Appsettings are not included in URI processing."
return
} else {
Write-Verbose "Looking for any http URLs from appSettings..."
Get-PSAppSetting -ConfigXml $configXml |
Where-Object value -imatch '^http[s]*:' |
Add-Member -MemberType AliasProperty -Name name -Value key -Force -PassThru |
Add-Member -MemberType AliasProperty -Name address -Value value -Force -PassThru |
Add-Member -MemberType AliasProperty -Name Uri -Value value -Force -PassThru |
Set_Type -TypeName 'PSWebConfig.Uri'
}
}
}
2 changes: 2 additions & 0 deletions Functions/PSUri/Test-PSUri.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ function Test-PSUri {
)

process {
Write-Verbose "Executing Test-PSUri"

if ($Uri) {
$InputObject = $Uri | Foreach-Object {
New-Object -TypeName PsObject -Property @{
Expand Down
30 changes: 23 additions & 7 deletions Functions/PSWebConfig/Get-PSWebConfig.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
It accepts either Path or an InputObject to discover the configuration files
and if -Recurse is specified it discovers all sub-configuration too.
Remote configurations can be fetched by setting -Session parameter.
If the input object is received from a PSSession instance, it will try to
use the session's InstanceId to fetch the configuration remotely.
.PARAMETER InputObject
Mandatory - Parameter to pass the Application or WebSite from pipeline
.PARAMETER Path
Expand Down Expand Up @@ -67,7 +72,11 @@ function Get-PSWebConfig {
[System.Management.Automation.Runspaces.PSSession]$Session
)
process {
if (!$AsText -and !$AsFileInfo) { $AsXml = $true }
Write-Verbose "Executing Get-PSWebConfig"
if (!$AsText -and !$AsFileInfo) {
Write-Verbose "Defaulting output-format to XML object"
$AsXml = $true
}

if ($Path) {
Write-Verbose "Processing by Path"
Expand All @@ -80,25 +89,32 @@ function Get-PSWebConfig {
if ($InputObject) {
Write-Verbose "Processing by InputObject"
foreach ($entry in $InputObject) {
# Setting Remote Session
$EntrySession = $entry.Session
if ($Session) {
Write-Verbose "Overriding session from -Session Parameter"
$EntrySession = $Session
}
elseif ($entry | Get-Member -Name RunspaceId) {
Write-Verbose "Getting Session from RunspaceId '$($entry.RunspaceId)'"
$EntrySession = Get-PSSession -InstanceId $entry.RunspaceId
}

if ($entry -is [System.IO.FileInfo]) {
if ($entry -is [System.IO.FileInfo] -or $entry.psobject.TypeNames -icontains 'Deserialized.System.IO.FileInfo') {
Write-Verbose "Adding physicalPath alias for [System.IO.FileInfo] FullName"
$entry = $entry | Add-Member -MemberType AliasProperty -Name physicalPath -Value FullName -PassThru
}

if ($entry | Get-Member -Name physicalPath) {
$EntrySession = $entry.Session
if ($Session) { $EntrySession = $Session }

if ($EntrySession) {
Write-Verbose "Remote Invoke-Command to '$($EntrySession.ComputerName)'"
Write-Verbose "Remote configuration fetch from '$($EntrySession.ComputerName + " " + $entry.physicalPath)'"
$response = Invoke-Command `
-Session $EntrySession `
-ArgumentList @($entry.physicalPath, $AsFileInfo, $AsText, $Recurse) `
-ScriptBlock ${function:Get_ConfigFile} |
Add-Member -NotePropertyName Session -NotePropertyValue $EntrySession -Force -PassThru
} else {
Write-Verbose "Local Invoke-Command"
Write-Verbose "Local configuration fetch from '$($entry.physicalPath)'"
$response = Invoke-Command `
-ArgumentList @($entry.physicalPath, $AsFileInfo, $AsText, $Recurse) `
-ScriptBlock ${function:Get_ConfigFile}
Expand Down
23 changes: 17 additions & 6 deletions Functions/PSWebConfig/Test-PSWebConfig.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@
Tests all URI and ConnectionStrings from web or application configuration.
.DESCRIPTION
The cmdlet fetches all ConnectionString and URIs for a configuration file
and executes a test against them on a local or remote machine.
The cmdlet fetches all ConnectionString and service endpoint URIs
from a configuration xml and executes a test against them on a
local or remote machine.
If -IncludeAppSettings switch is defined, it will include any URI or
ConnectionStrings to the tests.
.PARAMETER InputObject
Mandatory - Parameter to pass a PSWebConfig object
.PARAMETER Session
Optional - PSSession to execute configuration test
.PARAMETER IncludeAppSettings
Optional - Switch to include URIs and ConnectionStrings from appSettings
sections
.EXAMPLE
Get-PSWebConfig -Path 'c:\intepub\wwwroot\testapp\' | Test-PSWebConfig
Expand All @@ -25,13 +33,16 @@ function Test-PSWebConfig {
[Parameter(ValueFromPipeLine=$true)]
[psobject[]]$ConfigXml,

[switch]$IncludeAppSettings,
[System.Management.Automation.Runspaces.PSSession]$Session
)
process {
Get-PSEndpoint -ConfigXml $ConfigXml |
Test-PSUri -Session $Session
Write-Verbose "Executing Test-PSWebConfig"

Get-PSUri -ConfigXml $ConfigXml -IncludeAppSettings:$IncludeAppSettings |
Test-PSUri -Session $Session

Get-PSConnectionString -ConfigXml $ConfigXml |
Test-PSConnectionString -Session $Session
Get-PSConnectionString -ConfigXml $ConfigXml -IncludeAppSettings:$IncludeAppSettings |
Test-PSConnectionString -Session $Session
}
}
2 changes: 1 addition & 1 deletion PSWebConfig.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
RootModule = 'PSWebConfig.psm1'

# Version number of this module.
ModuleVersion = '1.5.2.0'
ModuleVersion = '1.6.0.0'

# ID used to uniquely identify this module
GUID = '37abef2c-d883-46be-ce1a-53d16477d01d'
Expand Down
Loading

0 comments on commit ad1fd7e

Please sign in to comment.