question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Apostrophe in filepath breaks Import-Module when the module has a class in it

See original GitHub issue

When there is an apostrophe in the filepath to a module Import-Module will work with modules that only have functions in however will fail if there is a class in the module. The exact same code will work when the module is located on a different path.

This situation was encountered due to the default OneDrive for Business path including an apostrophe. When using the user windowspowershell module with folder redirection all use modules now have an apostrophe in the path.

This affects both Version 5.1 and Version 6beta3

Steps to reproduce

Create Folder C:\Te’mp Save the below code to C:\Te’mp\TypeName.psm1

# Define a class
class TypeName
{
   # Property with validate set
   [ValidateSet("val1", "Val2")]
   [string] $P1

   # Constructor
   TypeName ([string] $s)
   {
       $this.P1 = $s       
   }
}

Import-Module 'C:\te''mp\TypeName.psm1'

Expected behavior

Module Loaded Successfully / No Output

Actual behavior

Import-Module : The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)
At line:1 char:1
+ Import-Module 'C:\Te''mp\TypeName.psm1'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Import-Module], FileLoadException
    + FullyQualifiedErrorId : System.IO.FileLoadException,Microsoft.PowerShell.Commands.ImportModuleCommand

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      6.0.0-beta
PSEdition                      Core
GitCommitId                    v6.0.0-beta.3
OS                             Microsoft Windows 10.0.14393
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Name                           Value                                                                                                        
----                           -----                                                                                                        
PSVersion                      5.1.15063.413                                                                                                
PSEdition                      Desktop                                                                                                      
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                      
BuildVersion                   10.0.15063.413                                                                                               
CLRVersion                     4.0.30319.42000                                                                                              
WSManStackVersion              3.0                                                                                                          
PSRemotingProtocolVersion      2.3                                                                                                          
SerializationVersion           1.1.0.1

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
rkeithhillcommented, Jul 14, 2017

And don’t forget that you can now use the Unicode esc sequence in dq strings e.g. "`u{2019}". 😃

1reaction
ld0614commented, Jul 7, 2017

I’ve written a (very quick and VERY dirty) script which loops through all visible ASCII chars and outputs the ones that fail. In version 5.1 the following fail: 39 ('), 44 (,) and 61 (=). Interestingly enough testing with version 6.0 Beta 3 the same 3 fail as well as 126 (~)

$InvalidDirectoryChars = 34,42,47,58,60,62,63,92,124
$FailedChars = @()

for ($i = 32; $i -lt 255; $i++)
{
    $PathName = "C:\Temp\$($i)Te$([Char]$i)mp"
    if ($InvalidDirectoryChars -notcontains $i)
    {
        $Directory = New-Item -ItemType Directory -Path $PathName -Force -ErrorAction SilentlyContinue
        if ($Directory -eq $null)
        {
            Write-Output "Char $([Char]$i) is not a valid folder name number is: $i"
        }
        else
        {
            Copy-Item C:\Temp\Sample\* $PathName -Force
        }
        if ($i -ne 91)
        {
            Import-Module $PathName\FunctionModule.psm1 -ErrorAction SilentlyContinue -Force
        }
        else
        {
            Import-Module 'C:\Temp\91Te`[mp\FunctionModule.psm1' -Force
        }
        $FunctionModule = Get-Module FunctionModule
        if ($FunctionModule.Path -eq "$PathName\FunctionModule.psm1")
        {
            Write-Host "Char $i Loaded FunctionModule Correctly" -ForegroundColor Green
        }
        else
        {
            Write-Host "Char $i failed to load FunctionModule" -ForegroundColor Red
        }

        if ($i -ne 91)
        {
            Import-Module $PathName\ClassModule.psm1 -ErrorAction SilentlyContinue
        }
        else
        {
            Import-Module 'C:\Temp\91Te`[mp\ClassModule.psm1' -Force
        }
        $ClassModule = Get-Module ClassModule
        if ($ClassModule.Path -eq "$PathName\ClassModule.psm1")
        {
            Write-Host "Char $i Loaded ClassModule Correctly" -ForegroundColor Green
        }
        else
        {
            Write-Host "Char $i failed to load ClassModule" -ForegroundColor Red
            $FailedChars += $i
        }
    }
}

Write-Output "Failed Chars are:"
$FailedChars
Read more comments on GitHub >

github_iconTop Results From Across the Web

python - How do I read a XLSX if the filepath or filename ...
1 Answer. The problem isn't with the apostrophe, it's with the backslash ( \ ) character. It is being used simultaneously as a...
Read more >
Bug in PowerShell classes when script is in a folder ...
My client's PowerShell scripts aren't working for a new user who happens to have a single-quote in their name - which means their...
Read more >
Swift Error Module Importing Failed Invalid Pathname
I was playing around with Swift on Ubuntu and got the same message just launching the swift REPL. ... Apostrophe in filepath breaks...
Read more >
What's New in Windows PowerShell 5.0
These release notes describe the new features and changes in Windows PowerShell 5.x.
Read more >
Swift Error Module Importing Failed Invalid Pathname
Importing locally developed module fails with Import. Apostrophe in filepath breaks Import-Module when the. ImportError: DLL load failed: The specified ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found