Skip to content

Commit 10d0244

Browse files
author
Robert Holt
committed
Make script compatible with PSv3
1 parent a3bdcd1 commit 10d0244

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
@@ -24,7 +24,9 @@ else
2424
[System.Reflection.BindingFlags]$script:InstanceBindingFlags = [System.Reflection.BindingFlags]::Public -bor [System.Reflection.BindingFlags]::Instance -bor [System.Reflection.BindingFlags]::FlattenHierarchy
2525

2626
# Common/ubiquitous cmdlet parameters which we don't want to repeat over and over
27-
[string[]]$script:CommonParams = @(
27+
[System.Collections.Generic.HashSet[string]]$script:CommonParameters = New-Object 'System.Collections.Generic.HashSet[string]' ([System.StringComparer]::OrdinalIgnoreCase)
28+
29+
$commonParams = @(
2830
'Verbose'
2931
'Debug'
3032
'ErrorAction'
@@ -38,38 +40,24 @@ else
3840
'PipelineVariable'
3941
)
4042

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

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

66-
# Shared module path location
51+
# User and Shared module path locations
6752
if ($PSVersionTable.PSVersion.Major -ge 6)
6853
{
54+
[string]$script:UserModulePath = [System.Management.Automation.ModuleIntrinsics].GetMethod('GetPersonalModulePath', [System.Reflection.BindingFlags]'static,nonpublic').Invoke($null, @())
6955
[string]$script:SharedModulePath = [System.Management.Automation.ModuleIntrinsics].GetMethod('GetSharedModulePath', [System.Reflection.BindingFlags]'static,nonpublic').Invoke($null, @())
7056
}
7157
else
7258
{
59+
$documentsFolder = [System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::Personal)
60+
[string]$script:UserModulePath = "$documentsFolder\PowerShell\Modules"
7361
[string]$script:SharedModulePath = "$env:ProgramFiles\WindowsPowerShell\Modules"
7462
}
7563

@@ -206,8 +194,10 @@ function New-PowerShellCompatibilityProfile
206194
$OutFile = Join-Path $script:CompatibilityProfileDir "$platformNameStr.json"
207195
}
208196

209-
$json = ConvertTo-CompatibilityJson -Item $reportData -NoWhitespace:(-not $Readable)
210-
return New-Item -Path $OutFile -Value $json -Force
197+
ConvertTo-CompatibilityJson -Item $reportData -NoWhitespace:(-not $Readable) `
198+
| Out-File -Force -LiteralPath $OutFile -Encoding Utf8
199+
200+
return Get-Item -LiteralPath $OutFile
211201
}
212202

213203
function New-AllPlatformReferenceProfile
@@ -1103,7 +1093,7 @@ function New-AvailableTypeData
11031093
function Get-FullTypeName
11041094
{
11051095
param(
1106-
[Parameter(ValueFromPipeline=$true)]
1096+
[Parameter(Position=0,ValueFromPipeline=$true)]
11071097
[type]
11081098
$Type
11091099
)

0 commit comments

Comments
 (0)