我试图将一个If语句中建立的变量传递给另一个If语句。这是我的测试代码:
if (test-path -path "C:\temp\watcher.txt"){
$script:NewName = rename-item "C:\temp\watcher.txt" -NewName "C:\temp\date_watcher.txt"
}
else
{
Write-host "nothin here boss"
}
if (test-path -path $script:newname){
$NewName2 = rename-item $script:newname -NewName "C:\temp\dateSECOND_watcher.txt"
}
else
{
Write-host "nothin here boss"
}
第一个语句可以正常工作,但是无法识别第二个语句中的变量,并显示以下错误:
“ Test-Path:由于参数'Path'为空,因此无法将其绑定到参数'
任何帮助都将不胜感激。
答案 0 :(得分:3)
您的变量未初始化,因为默认情况下Rename-Item不会返回对象。
尝试向其中添加-PassThru
,以使其返回您期望的对象。