none
How do I import a module for script use? RRS feed

  • Question

  • I have a script that uses the AD module for some operations. Here is the code:

    #04/24/2014
    #UsAer Logger
    #Author: Alexander Bazzi
    #Property of AerSale, Inc
    
    Import-Module ActiveDirectory
    
    $username=$env:username
    $computerName=$env:computername
    
    $userProperties = Get-ADUser $username
    $departmentOU = ($userProperties.DistinguishedName -split ',OU=')[1]
    $officeOU=($userProperties.DistinguishedName -split ',OU=')[2]
    $pathOutUser = "C:\Scripts\UsAer_Logger\Output\$officeOU\$departmentOU.txt"
    
    if(Test-Path $pathOutUser)
    {
        $computerName >> $pathOutUser
    }
    
    else {
        New-Item $pathOutUser
        $computerName >> $pathOutUser
    }

    When I try to run it on PowerShell ISE, I get the following exception: 

    Import-Module : The following error occurred while loading the extended type da
    ta file: 
    Microsoft.PowerShell, C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Active
    Directory\ActiveDirectory.Types.ps1xml : File skipped because it was already pr
    esent from "Microsoft.PowerShell".
    Microsoft.PowerShell, C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Active
    Directory\ActiveDirectory.Types.ps1xml : File skipped because it was already pr
    esent from "Microsoft.PowerShell".
    Microsoft.PowerShell, C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Active
    Directory\ActiveDirectory.Types.ps1xml : File skipped because it was already pr
    esent from "Microsoft.PowerShell".
    Microsoft.PowerShell, C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Active
    Directory\ActiveDirectory.Types.ps1xml : File skipped because it was already pr
    esent from "Microsoft.PowerShell".
    Microsoft.PowerShell, C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Active
    Directory\ActiveDirectory.Types.ps1xml : File skipped because it was already pr
    esent from "Microsoft.PowerShell".

    I tried running it without the Import-Module cmdlt, but it didn't recognize the Get-ADUser command. If you could shed some light on this, I'd really appreciate it!

    Many thanks in advance.

    Best,

    Alex


    Wednesday, July 2, 2014 2:43 PM

All replies

  • You have two different copies of Active Directory module installed.

    Get-Module -list Act*

    See if there is a second version.  It is also possible that you are loading something in your profile that is loading those components.  Try start PowerShell with the -NoProfile switch then import AD.  If that works then inspect your profiles - all of them.,


    ¯\_(ツ)_/¯

    Wednesday, July 2, 2014 5:13 PM