[英]powershell script and subst command
I have the following powershell 2.0 script: 我有以下powershell 2.0脚本:
function getFreeDrive
{
[char[]]$driveLetters = @([char]'E'..[char]'Z')
foreach ($d in $driveLetters) {
if(!(Test-Path -Path "$d`:" -IsValid)) {
return $d
}
}
}
$drive = getFreeDrive
subst "$drive`:" T:\temp
ls "$drive`:\" # just a dummy command
subst "$drive`:" /D
I want the script to 我想要脚本
The script works fine, when I run it the first time. 当我第一次运行它时,脚本运行正常。 If I run the script a second time in the same shell, I get an error from the ls command saying that the drive can not be found.
如果我在同一个shell中第二次运行该脚本,我会从ls命令收到错误,指出无法找到该驱动器。 If I open a new shell and run the script, it runs fine again.
如果我打开一个新shell并运行该脚本,它会再次正常运行。
What is the problem with my script and how can I get the it to run multiple times in the same powershell instance? 我的脚本有什么问题,如何让它在同一个PowerShell实例中多次运行?
Or maybe there is an alternative to the subst command? 或者也许替代subst命令? I tried using a powershell drive, but it doesn't work with other windows programs (eg devenv.exe).
我尝试使用powershell驱动器,但它不能与其他Windows程序(例如devenv.exe)一起使用。
An alternative is using PSProviders and more accuratly PSDrives (have a look to get-help about_providers ) : 另一种方法是使用PSProviders和更准确的PSDrives(看看get-help about_providers ):
PS > New-PSDrive -Name "tr" -PSProvider filesystem -Root "c:\temp"
Name Used (GB) Free (GB) Provider Root
---- --------- --------- -------- ----
tr 28,15 FileSystem C:\temp
PS > ls tr:*.c
Répertoire : C:\temp
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 01/08/2012 05:28 994 test.c
PS > Remove-PSDrive -Name "tr"
The trouble is that these drives can't be used with the shell explorer.exe. 问题是这些驱动器不能与shell explorer.exe一起使用。
I was having the exact same issue, and my workaround - note I am using version 3 - is to call Get-PSDrive
before going into newly mapped drive: 我遇到了完全相同的问题,我的解决方法 - 注意我使用的是版本3 - 是在进入新映射的驱动器之前调用
Get-PSDrive
:
$drive = getFreeDrive
subst "$drive`:" T:\temp
Get-PSDrive | Out-Null
ls "$drive`:\" # just a dummy command
subst "$drive`:" /D
I can replicate the exact same behaviour as you, even to the point where in one Powershell window I can't even cd to the drive it's created, but if I open a brand new window I can cd to it just fine. 我可以复制与你完全相同的行为,即使在一个Powershell窗口中我甚至无法cd到它创建的驱动器,但如果我打开一个全新的窗口,我可以cd到它就好了。
Behaviour seems like that outlined here: 行为似乎如下所示:
http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/2e414f3c-98dd-4d8b-a3a3-88cfa0e7594c/ http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/2e414f3c-98dd-4d8b-a3a3-88cfa0e7594c/
Workaround is probably to use PSDrives as mentioned above, or just don't map, then unmap, then try to remap the same drive in the same session. 解决方法可能是使用如上所述的PSDrives,或者只是不映射,然后取消映射,然后尝试在同一会话中重新映射相同的驱动器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.