PermaChips
PermaChips

Reputation: 51

PShell script works in ISE, but when running .ps1

I wrote a script to map a Network Drive (NAS) if not mapped, or unmap it if mapped.

# NAS.ps1
If (!(Test-Path N:)) {
    $User = "User"
    # Pwd in script will be removed later
    $Pwd = ConvertTo-SecureString -String "Password" -AsPlainText -Force
    
    $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $Pwd

    New-PSDrive -Name "N" -PSProvider "FileSystem" -Root "\\192.168.1.100\NAS" -Persist -Credential $Credential
} else {
    Remove-PSDrive -Name "N"
}

I edit it using the Windows Powershell ISE. When I run it, I see the drive appearing or disappearing in my Windows Explorer window. That's perfect.

Run with Powershell

However, when I right click NAS.ps1 > "Run with Powershell", the PS window quickly appear/disappear and the drive is neither mapped/unmapped (nothing changes).

Run Powershell, then ./NAS.ps1

If I try to run Powershell, cd to the folder with my scripts, and run it manually, it keeps mounting the drive:

PS C:\Users\User\Desktop> .\NAS.ps1

Name           Used (GB)     Free (GB) Provider      Root                                               CurrentLocation
----           ---------     --------- --------      ----                                               ---------------
N                1150.44       3201.81 FileSystem    \\192.168.1.100\NAS

PS C:\Users\User\Desktop> .\NAS.ps1

Name           Used (GB)     Free (GB) Provider      Root                                               CurrentLocation
----           ---------     --------- --------      ----                                               ---------------
N                1150.44       3201.81 FileSystem    \\192.168.1.100\NAS

Run Powershell as Admin., then ./NAS.ps1

If I launch Powershell as an administrator, and run my scripts, nothing is shown:

PS C:\Windows\system32> cd C:\Users\User\Desktop
PS C:\Users\User\Desktop> .\NAS.ps1
PS C:\Users\User\Desktop> .\NAS.ps1
PS C:\Users\User\Desktop> .\NAS.ps1

Using Get-PSDrive it appear as mounted, I can cd into it, but from the Win. Explorer window it does not appear

Upvotes: 2

Views: 459

Answers (2)

Puzo
Puzo

Reputation: 113

Ok, the First question if you set the execution policy?

By default, execution of scripts is not allowed.

You can do it with this cmdlet.

# To configure execution policy
Set-ExecutionPolicy -ExecutionPolicy Unrestricted

# To check execution policy
Get-ExecutionPolicy -List

The second thing that looks bad is $pwd $pwd, a system default variable that defines the current path. Better user $Pass or $Pword or $Pswd etc.

And the final question, what for you are doing this New-PSDrive ?

Why I'm asking. Because as I know, PSDrive is mapping only for the current PowerShell session. This will not show you drive like a mapped drive. You can use it to copy files or doing an action. But when you close this PowerShell session, the mapping will be removed.

This is not the same as "net use" You actually can do it like

$net = new-object -ComObject WScript.Network
$net.MapNetworkDrive("n:", "\\192.168.1.100\NAS", $false, $User, $password)

Or the old fashion way:

$script = 'net use N: \\192.168.1.100\NAS /persistent:yes /u:' + $User + ' ' + $Password
Invoke-Expression $script

Upvotes: 1

PermaChips
PermaChips

Reputation: 51

The solution was to use -Scope Global when mapping the drive, and to use net use to un map it.

Working code:

# NAS
Set-StrictMode -Version 2.0
If (!(Test-Path N:)) {

    $User = "user"
    $Pswd = ConvertTo-SecureString -String "Password" -AsPlainText -Force

    $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $Pswd

     New-PSDrive -Name "N" -Root "\\192.168.1.100\NAS" -Credential $Credential -PSProvider "FileSystem" -Persist -Scope Global
} else {
    Net Use "N:" /delete
}

Upvotes: 3

Related Questions