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?
1 Answer
Reset to default 0use $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 names - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736588880a1945047.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论