在这个简单的脚本中,if
当输入文件存在时,语句可以正常工作,但如果输入文件不存在,它会给我这个错误并完成:
Get-Content : Cannot find path 'C:\scripts\importfile.txt' because it does not exist.
At C:\Scripts\CLI_Localadmins.ps1:18 char:36
+ If (!($FileExists)) {$Computers = Get-Content -Path 'c:\scripts\importfile.txt'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\scripts\importfile.txt:String) [Get-Content], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand*
这是我正在使用的代码:
#Check if import file exists.
$ChkFile = "c:\scripts\importfile.txt"
$ValidPath = Test-Path $ChkFile -IsValid
If ($ValidPath -eq $True) {$Computers = Get-Content -Path 'c:\scripts\importfile.txt'
}
Else {$Computers = Get-QADComputer -SizeLimit 0 | select name -ExpandProperty name
}
# Give feedback that something is actually going on