PoshCode Archive  Artifact [d62d642822]

Artifact d62d642822f9a0a58d850af92304c8f87a46d363409cea3cd0938026624c2f01:

  • File Check-Modules-path.ps1 — part of check-in [90abcec3aa] at 2018-06-10 13:20:02 on branch trunk — Today I had spent a lot of time troubleshooting one module. After few checks I realized that it was simple typo that prevented PS from seeing my module in correct way. So here is function for you to avoid same problems… ;) (user: Bartek Bielawski size: 2784)

# encoding: ascii
# api: powershell
# title: Check Modules path.
# description: Today I had spent a lot of time troubleshooting one module. After few checks I realized that it was simple typo that prevented PS from seeing my module in correct way. So here is function for you to avoid same problems… ;)
# version: 0.1
# type: function
# author: Bartek Bielawski
# license: CC0
# function: Update-ModulePath
# x-poshcode-id: 3074
# x-archived: 2011-12-04T06:34:30
# x-published: 2011-11-29T05:01:00
#
#
function Update-ModulePath {
<#
    .Synopsis
        Command insures that path and the name of psm1 file are alike.
    .Description
        This function should help to troubleshoot modules. It loooks up path that should contain modules.
        For each .psm1 file found it checks if parent folder containing this file has same name.
        I created this function after I was banging my head for few hours why my module won't show up.
        After several approaches it came out clear that it was simple TYPO in file.psm1 name...
    .Example
        Update-ModulePath -Fix Files
        Will look all files and rename .psm1 files to match parent folder
    .Example
        Update-ModulePath -Fix Folder
        Will look all files and rename parent folder to match file names
    .Parameter Fix
        Switch to decide if we prefer to name folders or files to get all matches.
#>
    [CmdletBinding()]
    param(
    [Parameter(Mandatory=$true)]
    [ValidateSet("Files","Folders")]
    [string]$Fix
    )
    <# Steps to be taken:
        * enumerate all .psm1 files
        * check which one is misconfigured
        * rename file/ folder to fix this issue
    #>
    
    ForEach ($ModuleFile in @(Get-ChildItem -Recurse @($($env:PSModulePath).Split(";")) -filter *.psm1)) {
        if (($file = $ModuleFile.BaseName) -eq ($folder = $ModuleFile.Directory.ToString().Split('\')[-1])) {
            Write-Verbose "$Modulefile.Name is fine"
        } else {
            Write-Verbose "$ModuleFile.Name  is BAD"
            switch ($Fix) {
                "Files" {
                    Write-Verbose "We rename file $file"
                    $OldName = $ModuleFile.FullName
                    $NewName = $OldName -replace "$file.psm1$", "$folder.psm1"
                    
                    
                }
                "Folders" {
                    Write-Verbose "We rename folder $folder"
                    $OldName = $ModuleFile.FullName -replace "\\$file.psm1", ""
                    $NewName = $OldName -replace "$folder$", $file
                }
            }
            Write-Host "Renaming $OldName to $NewName"
            Rename-Item -Force $OldName $NewName
        }
    }
}