PowerShell / PSScriptAnalyzer

Download ScriptAnalyzer from PowerShellGallery
https://www.powershellgallery.com/packages/PSScriptAnalyzer/
MIT License
1.86k stars 376 forks source link

The rule 'PSUseCmdletCorrectly' reference a wrong command #755

Closed LaurentDardenne closed 11 months ago

LaurentDardenne commented 7 years ago

The analysis of this code seems to reference a command that is not in the current scope :


#Requires -Modules PSCX

$code=@'
function Convert-Object{
    [CmdletBinding()]
    [OutputType([String])]
    Param
    (
        [Parameter(Mandatory=$true)]
        $InputObject
    )

 function Get-TypeName {
  param()
   $InputObject.pstypenames[0] -Replace '^Selected\.',''
 }

 function MethodHeader {
  param()
   $type=Get-TypeName
   return $type
 }
 MethodHeader
}#Convert-Object
'@

Invoke-ScriptAnalyzer -ScriptDefinition $code
# RuleName                            Severity     ScriptName Line  Message
# --------                            --------     ---------- ----  -------
# PSUseCmdletCorrectly                Warning                 18    Cmdlet 'Get-TypeName' may be used incorrectly.
#                                                                   Please check that all mandatory parameters are
#                                                                   supplied.

gcm Get-TypeName
# CommandType     Name                                               Version    Source
# -----------     ----                                               -------    ------
# Cmdlet          Get-TypeName                                       3.2.0.0    Pscx

Remove-module pscx
Invoke-ScriptAnalyzer -ScriptDefinition $code
#same warning

#close the PS session
#move 'C:\Program Files (x86)\PowerShell Community Extensions' to 'C:\temp'

#new PS Session -NoProfile
$code=@'
function Convert-Object{
    [CmdletBinding()]
    [OutputType([String])]
    Param
    (
        [Parameter(Mandatory=$true)]
        $InputObject
    )

 function Get-TypeName {
  param()
   $InputObject.pstypenames[0] -Replace '^Selected\.',''
 }

 function MethodHeader {
  param()
   $type=Get-TypeName
   return $type
 }
 MethodHeader
}#Convert-Object
'@
Invoke-ScriptAnalyzer -ScriptDefinition $code
#Ok, no PSSA Warning

$Env:Path -split ';'
# %SystemRoot%\system32\WindowsPowerShell\v1.0\
# C:\ProgramData\Oracle\Java\javapath
# C:\Program Files\Common Files\Microsoft Shared\Windows Live
# C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live
# C:\Windows\system32
# C:\Windows
# C:\Windows\System32\Wbem
# C:\Program Files (x86)\WinMerge
# c:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\
# c:\Program Files\Microsoft SQL Server\100\Tools\Binn\
# c:\Program Files\Microsoft SQL Server\100\DTS\Binn\
# C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static
# C:\Program Files (x86)\Microsoft Team Foundation Server 2012 Power Tools\Best Practices Analyzer\
# C:\Program Files (x86)\Microsoft Team Foundation Server 2012 Power Tools\
# C:\Program Files (x86)\Windows Live\Shared
# C:\Program Files\Microsoft\Web Platform Installer\
# C:\Program Files\Microsoft SQL Server\110\Tools\Binn\
# C:\Program Files\Windows Fabric\bin\Fabric\Fabric.Code.1.0
# C:\Program Files\SourceGear\Common\DiffMerge\
# C:\Program Files (x86)\Disconnect\Disconnect Desktop\openvpn\bin
# C:\Program Files (x86)\Git\cmd
# C:\Windows\SysWOW64\WindowsPowerShell\v1.0\Modules\TShell\TShell\
# C:\Windows\System32\WindowsPowerShell\v1.0\
# C:\Program Files\Git\cmd
# C:\ProgramData\chocolatey\bin
# C:\Program Files\WindowsPowerShell\Scripts
# C:\Windows\System32\WindowsPowerShell\v1.0\
# C:\Program Files\TortoiseGit\bin
# C:\Program Files\dotnet\
# C:\Users\Laurent\AppData\Local\atom\bin
# C:\Program Files (x86)\Microsoft VS Code\bin
# C:\Dev\NuGet
# C:\Users\Laurent\Documents\WindowsPowerShell\Scripts
# C:\Program Files (x86)\PowerShell Community Extensions\Pscx3\Pscx\Apps

$env:PSmodulePath -split ';'
# C:\Users\Laurent\Documents\WindowsPowerShell\Modules\
# C:\Windows\system32\WindowsPowerShell\v1.0\Modules\
# C:\Program Files (x86)\PowerShell Community Extensions\Pscx3\
# C:\Program Files\Service Bus\1.0\
# C:\Program Files\Workflow Manager\1.0\
# C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\ServiceManagement
# C:\Program Files\WindowsPowerShell\Modules

Versions :

$PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.14409.1005
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0, 5.0, 5.1.14409.1005}
BuildVersion                   10.0.14409.1005
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

PSScriptAnalyzer version 1.11.1
Windows Seven x64

kapilmb commented 7 years ago

This is a bug. The rule should looks at the locally defined Get-TypeName function first first before looking at installed functions/cmdlets.