@@ -46,12 +46,7 @@ function New-MockBehavior {
4646}
4747
4848function EscapeSingleQuotedStringContent ($Content ) {
49- if ($global :PSVersionTable.PSVersion.Major -ge 5 ) {
50- [System.Management.Automation.Language.CodeGeneration ]::EscapeSingleQuotedStringContent($Content )
51- }
52- else {
53- $Content -replace " ['‘’‚‛]" , ' $&$&'
54- }
49+ [System.Management.Automation.Language.CodeGeneration ]::EscapeSingleQuotedStringContent($Content )
5550}
5651
5752function Create-MockHook ($contextInfo , $InvokeMockCallback ) {
@@ -83,7 +78,7 @@ function Create-MockHook ($contextInfo, $InvokeMockCallback) {
8378 }
8479 }
8580 $cmdletBinding = [Management.Automation.ProxyCommand ]::GetCmdletBindingAttribute($metadata )
86- if ($global :PSVersionTable .PSVersion.Major -ge 3 -and $ contextInfo.Command.CommandType -eq ' Cmdlet' ) {
81+ if ($contextInfo.Command.CommandType -eq ' Cmdlet' ) {
8782 if ($cmdletBinding -ne ' [CmdletBinding()]' ) {
8883 $cmdletBinding = $cmdletBinding.Insert ($cmdletBinding.Length - 2 , ' ,' )
8984 }
@@ -1499,8 +1494,7 @@ function Get-DynamicParametersForCmdlet {
14991494 [string ] $CmdletName ,
15001495
15011496 [ValidateScript ( {
1502- if ($PSVersionTable.PSVersion.Major -ge 3 -and
1503- $null -ne $_ -and
1497+ if ($null -ne $_ -and
15041498 $_.GetType ().FullName -ne ' System.Management.Automation.PSBoundParametersDictionary' ) {
15051499 throw ' The -Parameters argument must be a PSBoundParametersDictionary object ($PSBoundParameters).'
15061500 }
@@ -1525,77 +1519,29 @@ function Get-DynamicParametersForCmdlet {
15251519 return
15261520 }
15271521
1528- if (' 5.0.10586.122' -lt $PSVersionTable.PSVersion ) {
1529- # Older version of PS required Reflection to do this. It has run into problems on occasion with certain cmdlets,
1530- # such as ActiveDirectory and AzureRM, so we'll take advantage of the newer PSv5 engine features if at all possible.
1531-
1532- if ($null -eq $Parameters ) {
1533- $paramsArg = @ ()
1534- }
1535- else {
1536- $paramsArg = @ ($Parameters )
1537- }
1538-
1539- $command = $ExecutionContext.InvokeCommand.GetCommand ($CmdletName , [System.Management.Automation.CommandTypes ]::Cmdlet, $paramsArg )
1540- $paramDictionary = [System.Management.Automation.RuntimeDefinedParameterDictionary ]::new()
1541-
1542- foreach ($param in $command.Parameters.Values ) {
1543- if (-not $param.IsDynamic ) {
1544- continue
1545- }
1546- if ($Parameters.ContainsKey ($param.Name )) {
1547- continue
1548- }
1549-
1550- $dynParam = [System.Management.Automation.RuntimeDefinedParameter ]::new($param.Name , $param.ParameterType , $param.Attributes )
1551- $paramDictionary.Add ($param.Name , $dynParam )
1552- }
1553-
1554- return $paramDictionary
1522+ if ($null -eq $Parameters ) {
1523+ $paramsArg = @ ()
15551524 }
15561525 else {
1557- if ($null -eq $Parameters ) {
1558- $Parameters = @ { }
1559- }
1560-
1561- $cmdlet = ($command.ImplementingType )::new()
1562-
1563- $flags = [System.Reflection.BindingFlags ]' Instance, Nonpublic'
1564- $context = $ExecutionContext.GetType ().GetField(' _context' , $flags ).GetValue($ExecutionContext )
1565- [System.Management.Automation.Cmdlet ].GetProperty(' Context' , $flags ).SetValue($cmdlet , $context , $null )
1566-
1567- foreach ($keyValuePair in $Parameters.GetEnumerator ()) {
1568- $property = $cmdlet.GetType ().GetProperty($keyValuePair.Key )
1569- if ($null -eq $property -or -not $property.CanWrite ) {
1570- continue
1571- }
1572-
1573- $isParameter = [bool ]($property.GetCustomAttributes ([System.Management.Automation.ParameterAttribute ], $true ))
1574- if (-not $isParameter ) {
1575- continue
1576- }
1577-
1578- $property.SetValue ($cmdlet , $keyValuePair.Value , $null )
1579- }
1580-
1581- try {
1582- # This unary comma is important in some cases. On Windows 7 systems, the ActiveDirectory module cmdlets
1583- # return objects from this method which implement IEnumerable for some reason, and even cause PowerShell
1584- # to throw an exception when it tries to cast the object to that interface.
1585-
1586- # We avoid that problem by wrapping the result of GetDynamicParameters() in a one-element array with the
1587- # unary comma. PowerShell enumerates that array instead of trying to enumerate the goofy object, and
1588- # everyone's happy.
1526+ $paramsArg = @ ($Parameters )
1527+ }
15891528
1590- # Love the comma. Don't delete it. We don't have a test for this yet, unless we can get the AD module
1591- # on a Server 2008 R2 build server, or until we write some C# code to reproduce its goofy behavior.
1529+ $command = $ExecutionContext .InvokeCommand.GetCommand ( $CmdletName , [ System.Management.Automation.CommandTypes ]::Cmdlet , $paramsArg )
1530+ $paramDictionary = [ System.Management.Automation.RuntimeDefinedParameterDictionary ]::new()
15921531
1593- , $cmdlet.GetDynamicParameters ()
1532+ foreach ($param in $command.Parameters.Values ) {
1533+ if (-not $param.IsDynamic ) {
1534+ continue
15941535 }
1595- catch [ System.NotImplementedException ] {
1596- # Some cmdlets implement IDynamicParameters but then throw a NotImplementedException. I have no idea why. Ignore them.
1536+ if ( $Parameters .ContainsKey ( $param .Name )) {
1537+ continue
15971538 }
1539+
1540+ $dynParam = [System.Management.Automation.RuntimeDefinedParameter ]::new($param.Name , $param.ParameterType , $param.Attributes )
1541+ $paramDictionary.Add ($param.Name , $dynParam )
15981542 }
1543+
1544+ return $paramDictionary
15991545}
16001546
16011547function Get-DynamicParametersForMockedFunction {
@@ -1852,31 +1798,29 @@ function New-BlockWithoutParameterAliases {
18521798 $Block
18531799 )
18541800 try {
1855- if ($PSVersionTable.PSVersion.Major -ge 3 ) {
1856- $params = $Metadata.Parameters.Values
1857- $ast = Get-ScriptBlockAST $Block
1858- $blockText = $ast.Extent.Text
1859- $variables = [array ]($Ast.FindAll ( { param ($ast ) $ast -is [System.Management.Automation.Language.VariableExpressionAst ] }, $true ))
1860- [array ]::Reverse($variables )
1801+ $params = $Metadata.Parameters.Values
1802+ $ast = Get-ScriptBlockAST $Block
1803+ $blockText = $ast.Extent.Text
1804+ $variables = [array ]($Ast.FindAll ( { param ($ast ) $ast -is [System.Management.Automation.Language.VariableExpressionAst ] }, $true ))
1805+ [array ]::Reverse($variables )
18611806
1862- foreach ($var in $variables ) {
1863- $varName = $var.VariablePath.UserPath
1864- $length = $varName.Length
1807+ foreach ($var in $variables ) {
1808+ $varName = $var.VariablePath.UserPath
1809+ $length = $varName.Length
18651810
1866- foreach ($param in $params ) {
1867- if ($param.Aliases -contains $varName ) {
1868- $startIndex = $var.Extent.StartOffset - $ast.Extent.StartOffset + 1 # move one position after the dollar sign
1811+ foreach ($param in $params ) {
1812+ if ($param.Aliases -contains $varName ) {
1813+ $startIndex = $var.Extent.StartOffset - $ast.Extent.StartOffset + 1 # move one position after the dollar sign
18691814
1870- $blockText = $blockText.Remove ($startIndex , $length ).Insert($startIndex , $param.Name )
1815+ $blockText = $blockText.Remove ($startIndex , $length ).Insert($startIndex , $param.Name )
18711816
1872- break # It is safe to stop checking for further params here, since aliases cannot be shared by parameters
1873- }
1817+ break # It is safe to stop checking for further params here, since aliases cannot be shared by parameters
18741818 }
18751819 }
1876-
1877- $Block = [scriptblock ]::Create($blockText )
18781820 }
18791821
1822+ $Block = [scriptblock ]::Create($blockText )
1823+
18801824 $Block
18811825 }
18821826 catch {
0 commit comments