Skip to content

Commit

Permalink
Merge pull request #118 from webmd-health-services/bugfix/carbon-fail…
Browse files Browse the repository at this point in the history
…s-as-nested-module

* Fixed: Carbon fails to import multiple times in the same session.
* Fixed: Carbon fails when used as a nested module and Carbon is loaded globally or by nested in another module.
* Fixed: Importing Carbon fails under PowerShell 4.
  • Loading branch information
splatteredbits authored Feb 28, 2022
2 parents fc0884f + 455c109 commit 62c337a
Show file tree
Hide file tree
Showing 64 changed files with 3,645 additions and 6,948 deletions.
190 changes: 184 additions & 6 deletions RELEASE NOTES.txt → CHANGELOG.md

Large diffs are not rendered by default.

12 changes: 9 additions & 3 deletions Carbon.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,14 @@

All functions are idempotent: when run multiple times with the same arguments, your system will be in the same state without failing or producing errors.</description>
<language>en-us</language>
<releaseNotes>$ReleaseNotes$</releaseNotes>
<releaseNotes>https://github.com/webmd-health-services/Carbon/blob/master/CHANGELOG.md</releaseNotes>
<copyright>Aaron Jensen and WebMD Health Services</copyright>
<tags>$Tags$</tags>
<tags>.net, acl, active-directory, certificates, com, compression, computer, credential, cryptography, directory,
dsc, dsc-resources, encryption, environment, file-system, firewall, groups, hosts-file, http, identity, iis, ini,
installers, internet-explorer, ip, junctions, msi, msmq, netsh, networking, ntfs, operating-system, os, path,
performance-counters, powershell, principal, privileges, programs, registry, rsa, scheduled-tasks, security,
service, shares, sid, smb, ssl, text, trusted-host, users, wcf, windows, windows-features, xml, zip, PSModule,
DscResources, setup, automation, admin</tags>
</metadata>
<files>
<file src=".\Carbon\**\*" target="Carbon" exclude="**\*.pdb" />
Expand All @@ -55,6 +60,7 @@ All functions are idempotent: when run multiple times with the same arguments, y
<file src=".\Tools\VERIFICATION.md" target="Tools" />
<file src=".\LICENSE.txt" target="" />
<file src=".\NOTICE.txt" target="" />
<file src=".\RELEASE NOTES.txt" target="" />
<file src=".\CHANGELOG.md" target="" />
<file src=".\README.md" target="" />
</files>
</package>
22 changes: 5 additions & 17 deletions Carbon/Carbon.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
RootModule = 'Carbon.psm1'

# Version number of this module.
ModuleVersion = '2.11.0'
ModuleVersion = '2.11.1'

# ID used to uniquely identify this module
GUID = '075d9444-c01b-48c3-889a-0b3490716fa2'
Expand Down Expand Up @@ -89,13 +89,9 @@ All functions are idempotent: when run multiple times with the same arguments, y
ScriptsToProcess = @()

# Type files (.ps1xml) to be loaded when importing this module
TypesToProcess = @(
'Carbon.types.ps1xml',
'Types\Scheduled.Service.RegisteredTask.types.ps1xml',
'Types\System.IO.DirectoryInfo.types.ps1xml'
'Types\System.IO.FileInfo.types.ps1xml'
'Types\System.ServiceProcess.ServiceController.types.ps1xml'
)
# DO NOT USE! If you import a module twice that has the same type data, you get errors. :( Use Update-TypeData to
# dynamically add extended type data.
# TypesToProcess = @( )

# Format files (.ps1xml) to be loaded when importing this module
FormatsToProcess = @(
Expand Down Expand Up @@ -358,15 +354,7 @@ All functions are idempotent: when run multiple times with the same arguments, y
Prerelease = ''

# ReleaseNotes of this module
ReleaseNotes = @'
# 2.11.0
* Fixed: Resolve-CPathCase fails on PowerShell Core.
* New: 'Grant-Permission', 'Get-Permission', and 'Revoke-Permission' scripts now execute correctly on
non-Windows platforms.
* Fixed: Install-CService now will update services when file permissions or user account privileges have changed.
'@
ReleaseNotes = 'https://github.com/webmd-health-services/Carbon/blob/master/CHANGELOG.md'
} # End of PSData hashtable

} # End of PrivateData hashtable
Expand Down
132 changes: 104 additions & 28 deletions Carbon/Carbon.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,32 @@ if( $IsPSCore )
$carbonAssemblyDir = Join-Path -Path $CarbonBinDir -ChildPath 'coreclr' -Resolve
}

function Add-CAssembly
{
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[String] $Path
)

$numErrors = $Global:Error.Count
try
{
Add-Type -Path $Path
}
catch
{
$numErrorsToRemove = $Global:Error.Count - $numErrors
for( $idx = 0; $idx -lt $numErrorsToRemove; ++$idx )
{
$Global:Error.RemoveAt(0)
}
}
}

Write-Timing ('Loading Carbon assemblies from "{0}".' -f $carbonAssemblyDir)
Get-ChildItem -Path (Join-Path -Path $carbonAssemblyDir -ChildPath '*') -Filter 'Carbon*.dll' -Exclude 'Carbon.Iis.dll' |
ForEach-Object { Add-Type -Path $_.FullName }
ForEach-Object { Add-CAssembly -Path $_.FullName }

# Active Directory

Expand All @@ -71,9 +94,9 @@ if( (Test-Path -Path 'env:SystemRoot') )
if( -not $IsPSCore )
{
Write-Timing ('Adding Microsoft.Web.Administration assembly.')
Add-Type -Path $microsoftWebAdministrationPath
Add-CAssembly -Path $microsoftWebAdministrationPath
Write-Timing ('Adding Carbon.Iis assembly.')
Add-Type -Path (Join-Path -Path $carbonAssemblyDir -ChildPath 'Carbon.Iis.dll' -Resolve)
Add-CAssembly -Path (Join-Path -Path $carbonAssemblyDir -ChildPath 'Carbon.Iis.dll' -Resolve)
}
}
}
Expand All @@ -94,6 +117,84 @@ $TrustedHostsPath = 'WSMan:\localhost\Client\TrustedHosts'
Write-Timing ('Adding System.DirectoryServices.AccountManagement assembly.')
Add-Type -AssemblyName 'System.DirectoryServices.AccountManagement'

function Add-CTypeData
{
[CmdletBinding()]
param(
[Parameter(Mandatory, ParameterSetName='ByType')]
[Type] $Type,

[Parameter(Mandatory, ParameterSetName='ByTypeName')]
[String] $TypeName,

[Parameter(Mandatory)]
[ValidateSet('AliasProperty', 'NoteProperty', 'ScriptProperty', 'ScriptMethod')]
[Management.Automation.PSMemberTypes] $MemberType,

[Parameter(Mandatory)]
[String] $MemberName,

[Parameter(Mandatory)]
[Object] $Value
)

Set-StrictMode -Version 'Latest'

$memberTypeMsg = '{0,-14}' -f $MemberType

if( -not $TypeName )
{
$TypeName = $Type.FullName
}

if( $Type )
{
if( $MemberType -like '*Property' )
{
if( ($Type.GetProperties() | Where-Object Name -EQ $MemberName) )
{
Write-Debug ("Type $($memberTypeMsg) [$($TypeName)] $($MemberName)")
return
}
}
elseif( $MemberType -like '*Method')
{
if( ($Type.GetMethods() | Where-Object Name -EQ $MemberName) )
{
Write-Debug ("Type $($memberTypeMsg) [$($TypeName)] $($MemberName)")
return
}
}
}

$typeData = Get-TypeData -TypeName $TypeName
if( $typeData -and $typeData.Members.ContainsKey($MemberName) )
{
Write-Debug ("TypeData $($memberTypeMsg) [$($TypeName)] $($MemberName)")
return
}

Write-Debug ("TypeData + $($memberTypeMsg) [$($TypeName)] $($MemberName)")
Update-TypeData -TypeName $TypeName -MemberType $MemberType -MemberName $MemberName -Value $Value
}

# Move to Carbon.Core?
Add-CTypeData -Type Diagnostics.Process `
-MemberName 'ParentProcessID' `
-MemberType ScriptProperty `
-Value {
$filter = "ProcessID='{0}'" -f $this.Id
if( (Get-Command -Name 'Get-CimInstance' -ErrorAction Ignore) )
{
$process = Get-CimInstance -ClassName 'Win32_Process' -Filter $filter
}
else
{
$process = Get-WmiObject -Class 'Win32_Process' -Filter $filter
}
return $process.ParentProcessID
}

Write-Timing ('Dot-sourcing functions.')
$functionRoot = Join-Path -Path $PSScriptRoot -ChildPath 'Functions' -Resolve

Expand Down Expand Up @@ -136,29 +237,4 @@ foreach( $developerImport in $developerImports )

Write-Timing ('Dot-sourcing "{0}".' -f $developerImport)
. $developerImport
}

# Allows us to be platform agnostic in our calls of 'GetAccessControl'.
if( -not ([IO.DirectoryInfo]::New([Environment]::CurrentDirectory) | Get-Member -Name 'GetAccessControl') )
{
Update-TypeData -MemberName 'GetAccessControl' -MemberType 'ScriptMethod' -TypeName 'IO.DirectoryInfo' -Value {
[CmdletBinding()]
param(
[Security.AccessControl.AccessControlSections]$IncludeSections = [Security.AccessControl.AccessControlSections]::All
)

return [IO.FileSystemAclExtensions]::GetAccessControl($this, $IncludeSections)
}
}

if( -not ([IO.FileInfo]::New($PSCommandPath) | Get-Member -Name 'GetAccessControl') )
{
Update-TypeData -MemberName 'GetAccessControl' -MemberType 'ScriptMethod' -TypeName 'IO.FileInfo' -Value {
[CmdletBinding()]
param(
[Security.AccessControl.AccessControlSections]$IncludeSections = [Security.AccessControl.AccessControlSections]::All
)

return [IO.FileSystemAclExtensions]::GetAccessControl($this, $IncludeSections)
}
}
Loading

0 comments on commit 62c337a

Please sign in to comment.