Skip to content

Commit 45dff90

Browse files
author
Robert Holt
committed
Make script compatible with PSv3
1 parent c3ed792 commit 45dff90

File tree

1 file changed

+16
-26
lines changed

1 file changed

+16
-26
lines changed

CrossCompatibility/CrossCompatibility.psm1

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ else
2828
[System.Reflection.BindingFlags]$script:InstanceBindingFlags = [System.Reflection.BindingFlags]::Public -bor [System.Reflection.BindingFlags]::Instance -bor [System.Reflection.BindingFlags]::FlattenHierarchy
2929

3030
# Common/ubiquitous cmdlet parameters which we don't want to repeat over and over
31-
[string[]]$script:CommonParams = @(
31+
[System.Collections.Generic.HashSet[string]]$script:CommonParameters = New-Object 'System.Collections.Generic.HashSet[string]' ([System.StringComparer]::OrdinalIgnoreCase)
32+
33+
$commonParams = @(
3234
'Verbose'
3335
'Debug'
3436
'ErrorAction'
@@ -42,38 +44,24 @@ else
4244
'PipelineVariable'
4345
)
4446

45-
# The file name for the any-platform reference generated from the union of all other platforms
46-
[string]$script:AnyPlatformReferenceProfileFilePath = [System.IO.Path]::Combine($script:CompatibilityProfileDir, 'anyplatform_union.json')
47-
48-
<#
49-
.SYNOPSIS
50-
Turn the common parameters into a hashset for faster matching.
51-
#>
52-
function New-CommonParameterSet
47+
foreach ($p in $commonParams)
5348
{
54-
$set = New-Object 'System.Collections.Generic.HashSet[string]' ([System.StringComparer]::OrdinalIgnoreCase)
55-
56-
foreach ($p in $script:commonParams)
57-
{
58-
$set.Add($p)
59-
}
60-
61-
return $set
49+
$null = $script:CommonParameters.Add($p)
6250
}
6351

64-
# Set of the common cmdlet parameters to exclude from cmdlet data
65-
[System.Collections.Generic.HashSet[string]]$script:CommonParameters = New-CommonParameterSet
66-
67-
# User module path location
68-
[string]$script:UserModulePath = [System.Management.Automation.ModuleIntrinsics].GetMethod('GetPersonalModulePath', [System.Reflection.BindingFlags]'static,nonpublic').Invoke($null, @())
52+
# The file name for the any-platform reference generated from the union of all other platforms
53+
[string]$script:AnyPlatformReferenceProfileFilePath = [System.IO.Path]::Combine($script:CompatibilityProfileDir, 'anyplatform_union.json')
6954

70-
# Shared module path location
55+
# User and Shared module path locations
7156
if ($PSVersionTable.PSVersion.Major -ge 6)
7257
{
58+
[string]$script:UserModulePath = [System.Management.Automation.ModuleIntrinsics].GetMethod('GetPersonalModulePath', [System.Reflection.BindingFlags]'static,nonpublic').Invoke($null, @())
7359
[string]$script:SharedModulePath = [System.Management.Automation.ModuleIntrinsics].GetMethod('GetSharedModulePath', [System.Reflection.BindingFlags]'static,nonpublic').Invoke($null, @())
7460
}
7561
else
7662
{
63+
$documentsFolder = [System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::Personal)
64+
[string]$script:UserModulePath = "$documentsFolder\PowerShell\Modules"
7765
[string]$script:SharedModulePath = "$env:ProgramFiles\WindowsPowerShell\Modules"
7866
}
7967

@@ -210,8 +198,10 @@ function New-PowerShellCompatibilityProfile
210198
$OutFile = Join-Path $script:CompatibilityProfileDir "$platformNameStr.json"
211199
}
212200

213-
$json = ConvertTo-CompatibilityJson -Item $reportData -NoWhitespace:(-not $Readable)
214-
return New-Item -Path $OutFile -Value $json -Force
201+
ConvertTo-CompatibilityJson -Item $reportData -NoWhitespace:(-not $Readable) `
202+
| Out-File -Force -LiteralPath $OutFile -Encoding Utf8
203+
204+
return Get-Item -LiteralPath $OutFile
215205
}
216206

217207
function New-AllPlatformReferenceProfile
@@ -1107,7 +1097,7 @@ function New-AvailableTypeData
11071097
function Get-FullTypeName
11081098
{
11091099
param(
1110-
[Parameter(ValueFromPipeline=$true)]
1100+
[Parameter(Position=0,ValueFromPipeline=$true)]
11111101
[type]
11121102
$Type
11131103
)

0 commit comments

Comments
 (0)