none
If is not recognized as the name of a cmdlet?

    Question

  • Hello,

    My perpetual newb forays into powershell have bitten me once again.  I am receiving the following error:

    The term 'If' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At C:\Scripts\Powershell\Unused Procs\UnusedProcs.ps1:48 char:115
    +         Select-String -Path $File -include $includeFileTypes -Encoding "Default" -Pattern $ProcName -PassThru | If <<<< ($_ -eq $null) {Add-Content -Path $LogFile $ProcName}`;
        + CategoryInfo          : ObjectNotFound: (If:String) [], CommandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException

    cls;
    try
    {
        if ( (Get-PSSnapin -Name SqlServerCmdletSnapin100 -ErrorAction SilentlyContinue) -eq $null ) 
        {
            Add-PSSnapin SqlServerCmdletSnapin100
        }
    }
    catch{
        Write-host "SQL snapin already present"
    };
    
    $repositoryServer = "CORPTFS01"
    $repositoryDatabase = "Tfs_DefaultCollection"
    $localTFSDirectory = "C:\Source Code\TRUNK\Code\SSIS\DWETL\ACD"
    $includeFileTypes = "*.cpp", "*.cs", "*.aspx", "*.ascx", "*.ashx", "*.asax", ".h", "*.vb", "*.qvs", "*.qvw", "*.dtsx"
    $logFile = "C:\temp\unusedprocs.txt"
    
    $querytext = "
    SELECT DISTINCT ProcName
    FROM (
    	SELECT DISTINCT
    	  ParentPath 
    	 ,ProcName = REPLACE(REPLACE(REPLACE(childitem, '.proc.sql\', ''), 'dbo.', ''), '>', '_') 
    	FROM tbl_Version 
    	WHERE 
    		ChildItem LIKE '%proc.sql\' AND 
    		ChildItem NOT LIKE '%sp_MS%'
    	--ORDER BY 2, 1
    ) x
    ORDER BY 1"
    
    $query = invoke-sqlcmd -query $querytext -ServerInstance $repositoryServer -Database $repositoryDatabase
    $query | %{`
        Set-Variable "ProcName" -Value "$($_.ProcName)";`
        $List = Get-Childitem $localTFSDirectory -Recurse -Include $includeFileTypes;`
        ForEach ($File in $List)`
        {`
            Select-String -Path $File -include $includeFileTypes -Encoding "Default" -Pattern $ProcName -PassThru | If($_ -eq $null) {Add-Content -Path $LogFile $ProcName};`
        };`
    };`

    Please, gurus show me the error of my ways!

    Thanks,

    John_T


    John_T


    • Edited by John_T Monday, June 11, 2012 4:07 PM typo
    Monday, June 11, 2012 4:05 PM

Answers

  • specifically, this section:

    $query = invoke-sqlcmd -query $querytext -ServerInstance $repositoryServer -Database $repositoryDatabase
    $query | %{`
        Set-Variable "ProcName" -Value "$($_.ProcName)";`
        $List = Get-Childitem $localTFSDirectory -Recurse -Include $includeFileTypes;`
        ForEach ($File in $List)`
        {`
            Select-String -Path $File -include $includeFileTypes -Encoding "Default" -Pattern $ProcName -PassThru | If($_ -eq $null) {Add-Content -Path $LogFile $ProcName};`
        };`
    };`
    
    
    #### CHANGE TO:
    
    $query = invoke-sqlcmd -query $querytext -ServerInstance $repositoryServer -Database $repositoryDatabase
    $query | %{`
        Set-Variable "ProcName" -Value "$($_.ProcName)";`
        $List = Get-Childitem $localTFSDirectory -Recurse -Include $includeFileTypes;`
        ForEach ($File in $List)`
        {`
            Select-String -Path $File -include $includeFileTypes -Encoding "Default" -Pattern $ProcName -PassThru | foreach-object { If($_ -eq $null) {Add-Content -Path $LogFile $ProcName};` }
        };`
    };`


    G. Samuel Hays

    • Marked as answer by John_T Monday, June 11, 2012 5:50 PM
    Monday, June 11, 2012 4:22 PM

All replies

  • Your code is like this:
     
    C:...\windowspowershell> 1,2,3 | if ($_ -eq 2) {$_}
    The term 'if' is not recognized as the name of a cmdlet, function, script file,
     or operable program. Check the spelling of the name, or if a path was included
    , verify that the path is correct and try again.
    At line:1 char:11
    + 1,2,3 | if <<<<  ($_ -eq 2) {$_}
        + CategoryInfo          : ObjectNotFound: (if:String) [], CommandNotFoundE
       xception
        + FullyQualifiedErrorId : CommandNotFoundException
     Fixed code is like this (adding a %{} wrapper):
     
    C:...\windowspowershell> 1,2,3 | %{if ($_ -eq 2) {$_}}
    2
     
    • Proposed as answer by Bigteddy Monday, June 11, 2012 4:18 PM
    Monday, June 11, 2012 4:17 PM
  • specifically, this section:

    $query = invoke-sqlcmd -query $querytext -ServerInstance $repositoryServer -Database $repositoryDatabase
    $query | %{`
        Set-Variable "ProcName" -Value "$($_.ProcName)";`
        $List = Get-Childitem $localTFSDirectory -Recurse -Include $includeFileTypes;`
        ForEach ($File in $List)`
        {`
            Select-String -Path $File -include $includeFileTypes -Encoding "Default" -Pattern $ProcName -PassThru | If($_ -eq $null) {Add-Content -Path $LogFile $ProcName};`
        };`
    };`
    
    
    #### CHANGE TO:
    
    $query = invoke-sqlcmd -query $querytext -ServerInstance $repositoryServer -Database $repositoryDatabase
    $query | %{`
        Set-Variable "ProcName" -Value "$($_.ProcName)";`
        $List = Get-Childitem $localTFSDirectory -Recurse -Include $includeFileTypes;`
        ForEach ($File in $List)`
        {`
            Select-String -Path $File -include $includeFileTypes -Encoding "Default" -Pattern $ProcName -PassThru | foreach-object { If($_ -eq $null) {Add-Content -Path $LogFile $ProcName};` }
        };`
    };`


    G. Samuel Hays

    • Marked as answer by John_T Monday, June 11, 2012 5:50 PM
    Monday, June 11, 2012 4:22 PM
  • John,

    you should mark Larry Weiss' comment as the answer, he correctly answered it before I did - I only annotated it to make it easier to read. :) 



    G. Samuel Hays

    Monday, June 11, 2012 5:57 PM
  • Watch out for those back-ticks.
     
    Here is a form of that revised line that preserves the placement of the trailing back-tick.
        Select-String -Path $File -include $includeFileTypes -Encoding"Default"  -Pattern $ProcName
    -PassThru | foreach-object { If($_ -eq $null) {Add-Content -Path $LogFile $ProcName}; }`
     
     
    Monday, June 11, 2012 5:59 PM