admin管理员组

文章数量:1123702

Consider the following contrived PowerShell function (which happens to be part of a module, but that may not be relevant).

function Get-Thing {
    [CmdletBinding()]
    param(
        [string]$Profile
    )

    # do things with $Profile
}

PSScriptAnalyzer correctly produces a PSAvoidAssignmentToAutomaticVariable warning regarding the conflict with the $Profile automatic variable. So within the context of the function (or is it the whole module?), access to the "real" version is no longer accessible unless you explicitly get it with something like $global:Profile.

But assuming I have no need for the real version in this function or I'm okay with using the global prefix if I should need it, are there any other issues I need to be aware of if I simply suppress the warning like this?

function Get-Thing {
    [CmdletBinding()]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidAssignmentToAutomaticVariable','')]
    param(
        [string]$Profile
    )

    # do things with $Profile
}

I briefly considered changing the parameter name to prevent the conflict and adding [Alias('Profile')]. But I don't like the fact that the help view only shows the changed parameter name. Is there any way to still specify the parameter name as -Profile for users but have the internal variable name be different?

Consider the following contrived PowerShell function (which happens to be part of a module, but that may not be relevant).

function Get-Thing {
    [CmdletBinding()]
    param(
        [string]$Profile
    )

    # do things with $Profile
}

PSScriptAnalyzer correctly produces a PSAvoidAssignmentToAutomaticVariable warning regarding the conflict with the $Profile automatic variable. So within the context of the function (or is it the whole module?), access to the "real" version is no longer accessible unless you explicitly get it with something like $global:Profile.

But assuming I have no need for the real version in this function or I'm okay with using the global prefix if I should need it, are there any other issues I need to be aware of if I simply suppress the warning like this?

function Get-Thing {
    [CmdletBinding()]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidAssignmentToAutomaticVariable','')]
    param(
        [string]$Profile
    )

    # do things with $Profile
}

I briefly considered changing the parameter name to prevent the conflict and adding [Alias('Profile')]. But I don't like the fact that the help view only shows the changed parameter name. Is there any way to still specify the parameter name as -Profile for users but have the internal variable name be different?

Share Improve this question asked yesterday Ryan BolgerRyan Bolger 1,2958 silver badges20 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

use $Local:Profile

The prefix specifics the Scope of the variable, in this case Local(as in: the function).
You can then assign it to a different-named variable.

function Get-Thing {
  [CmdletBinding()]
  param(
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidAssignmentToAutomaticVariable', "I'm making sure tu assign it to a local variable in the script")]
    [string]$Profile
  )

  $LocalProfile = $Local:Profile

  $LocalProfile
  $Global:PROFILE

}

which wil result into:

PS > Get-Thing -Profile TT
TT
C:\Users\[REDACTED]\Documents\PowerShell\Microsoft.Powershell_profile.ps1   

本文标签: How to deal with PowerShell function parameters that conflict with automatic variable namesStack Overflow