将路径作为powershell脚本中的变量传递

时间:2013-04-18 15:12:11

标签: powershell

我想在我的powershell脚本中将路径作为变量(读取主机)传递。

是错误的

找不到路径,因为它不存在

我的代码

$a=read-host
$b=read-host
Copy-item $a $b

2 个答案:

答案 0 :(得分:1)

我们很难真正找出问题,因为我们并不确切知道您输入的是什么。将下面的代码放入脚本中以查看路径是否有效:

$a = Read-Host
$b = Read-Host
'Copying from {0} to {1}' -f $a, $b
Copy-Item $a $b

答案 1 :(得分:0)

如果路径存在,则使用Test-Path返回布尔值

if(Test-Path $a){
     Copy-Item $a $b
}