所以我制作了一个.ps1 脚本,可以在我想要映射 Samba 共享时随时运行它。
当我运行 PS1 脚本时,它要求我输入密码,但我无法将目录更改为共享文件夹。我试了两次。
但是,当我将 PS1 文件的内容复制并粘贴到 powershell 中并运行它时,我能够将目录更改为 samba 共享。
知道我可能做错了什么吗?
请看一下这个:
PS D:\myscripts> .\map-myfileserver1.ps1
Name Used (GB) Free (GB) Provider Root CurrentLocation
---- --------- --------- -------- ---- ---------------
y 5371.89 682.30 FileSystem \\myfileserver1\smb-storage
PS D:\myscripts> y:
Set-Location : Cannot find drive. A drive with the name 'Y' does not exist.
At line:1 char:1
+ Set-Location $MyInvocation.MyCommand.Name
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Y:String) [Set-Location], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.SetLocationCommand
PS D:\myscripts> .\map-myfileserver1.ps1
Name Used (GB) Free (GB) Provider Root CurrentLocation
---- --------- --------- -------- ---- ---------------
y 5371.89 682.30 FileSystem \\myfileserver1\smb-storage
PS D:\myscripts> y:
Set-Location : Cannot find drive. A drive with the name 'Y' does not exist.
At line:1 char:1
+ Set-Location $MyInvocation.MyCommand.Name
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Y:String) [Set-Location], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.SetLocationCommand
PS D:\myscripts> new-psdrive -name y -psprovider filesystem -root \\myfileserver1\smb-storage -credential my-samba-user-here -persist
Name Used (GB) Free (GB) Provider Root CurrentLocation
---- --------- --------- -------- ---- ---------------
y 5371.89 682.30 FileSystem \\myfileserver1\smb-storage
PS D:\myscripts> y:
PS y:\>
答案1
添加-scope global
到脚本中的 cmdlet 调用。如果没有它,脚本退出时驱动器将取消映射。如果已经使用,这似乎违反直觉-persist
,但这就是它所需要的。
help new-psdrive -details
-Scope <String>
Specifies a scope for the drive. Valid values are "Global", "Local", or "Script", or a
number relative to the current scope (0 through the number of scopes, where 0 is the
current scope and 1 is its parent). "Local" is the default. For more information, see
about_Scopes (http://go.microsoft.com/fwlink/?LinkID=113260).