この単純なスクリプトでは、入力ファイルが存在する場合、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