Powershell 提示用户输入驱动器号 [英] Powershell Prompt user for drive letter

查看:70
本文介绍了Powershell 提示用户输入驱动器号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本来映射文件以运行 VHD.但是,当您将它从一台机器运行到另一台机器时,驱动器号可能会发生变化.如何提示用户哪个驱动器盘符包含该文件夹?或确定哪个驱动器号有 \Program Files\Microsoft Learning\20414\Drives\?

I have a script that maps files in order to run VHDs. But the drive letter can change when you run it from one machine to another. How can I prompt the user which drive letter has the folder? or determine which drive letter has \Program Files\Microsoft Learning\20414\Drives\?

实际脚本如下:

Set-VHD -Path "D:\Program Files\Microsoft Learning\20414\Drives\20414B-LON-DC1\Virtual Hard Disks\20414B-LON-DC1.vhd" -ParentPath "D:\Program Files\Microsoft Learning\Base\Drives\MT12-WS12-LON-DC1-TMP.vhd" 

推荐答案

使用 Read-Host 提示用户输入.像这样,

Use Read-Host to prompt for user input. Like so,

$vhdLocation = read-host "输入 VHD 文件的路径"

您可以列出所有驱动器并检查目录是否存在,而不是提示用户,Get-PSDriveTest-PathJoin-Path.像这样,

Instead of prompting user, you could list all the drives and check if the directory exists with Get-PSDrive, Test-Path and Join-Path. Like so,

get-psdrive | ? {
$_.root -match "[c-z]:\\" -and (test-path $(join-path $_.root "Program Files\Microsoft Learning\20414\Drives\"))
}

$_.root -match "[c-z]:\\" 将匹配驱动器号 C: 到 Z:.

$_.root -match "[c-z]:\\" will match drive letters C: to Z:.

$(join-path $_.root "Program Files\Microsoft Learning\20414\Drives\") 将为路径创建有效的语法.也就是说,它将自动管理分隔符.

$(join-path $_.root "Program Files\Microsoft Learning\20414\Drives\") will create a valid syntax for path. That is, it will manage delimiters automatically.

test-path 将返回 true.

这篇关于Powershell 提示用户输入驱动器号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆