Importing Module

Tagged: 

This topic contains 4 replies, has 2 voices, and was last updated by Profile photo of Michael Orellana Michael Orellana 2 years, 2 months ago.

  • Author
    Posts
  • #18947
    Profile photo of Michael Orellana
    Michael Orellana
    Participant

    Im finally coming here for help, I understand the basics of importing a module. But in my current situation I need to import a module from a shared network drive, from a computer that is not on the domain, It will be eventually but not at this step.

    I can browse to the shared drive by just pasting the location into windows explorer and I get prompted for credentials and then, boom, gives me access to the drive from the computer that is not on the domain. Is there a way to replicate this behavior in powershell for the import process? If not is there a way you can recommend? At the very least just be able to copy the module over and import it locally. Any help would be appreciate, and I can answer any questions if you need more background on the matter.

  • #18948
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    You can map a drive from a script in a few different ways, but you're also going to need to store the username and password used to map the drive, in some way. There are various methods to accomplish this, ranging from storing the password in plain text (least secure) to using encryption in such a way that only an authorized user can decrypt it (most secure, but may depend on something like public-key certificates, or may be limited to only a single user / computer.)

    Leaving the security aside for a moment (both of these examples will use plain-text passwords), here are a couple of simple ways to authenticate to a UNC share in Windows:

    # using the old "net use" command:
    net use \\server\share /user:Domain\UserName password
    
    # using PowerShell's New-PSDrive cmdlet:
    $secureString = "password" | ConvertTo-SecureString -AsPlainText -Force
    $cred = New-Object pscredential("Domain\UserName", $secureString)
    
    New-PSDrive -Name SomeName -PSProvider FileSystem -Root \\server\share -Credential $cred
    

    After you execute either the "net use" or New-PSDrive commands successfully, you should be able to import the module.

  • #19000
    Profile photo of Michael Orellana
    Michael Orellana
    Participant

    I get two errors when I try to run the New-PSdrive command

    New-object: Cannot Find type [pscredential]: make sure the assembly containing this type is loaded.

    The provider does not support the use credentials. Perform this operation again without specifying credentials.

  • #19004
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Ah, you're running PowerShell 2.0, then. Both of those errors would be resolved by updating to at least PowerShell 3.0. If you need to use v2, stick to the "net use" command for this task.

  • #19026
    Profile photo of Michael Orellana
    Michael Orellana
    Participant

    Thanks for the info! A little background on the script, I am building a script that will automate the setup process after a machine has been imaged, so currently the reason I have to go the route that I am is because the image that is being put on these computers, comes with PS 2.0. I have found a work around to my problem and it is as follows:

    $AdminUser = Read-Host "Please enter user name including domain. 'usa\Ajohnson'"
            
    $SecureString = Read-Host -AsSecureString "Please Enter your password"
    $AdminPass = [runtime.interopServices.Marshal]::PtrToStringAuto([runtime.InteropServices.Marshal]::SecureStringToBSTR($SecureString))
        
    $net = New-Object -ComObject WScript.Network
    $net.MapNetworkDrive("u:","\\Network\share", $false , $AdminUser, $AdminPass)
    

    I appreciate all the help and pointing me in the right directions.

You must be logged in to reply to this topic.