0

In this simple script, the if statement works fine when the input file is present, but if the input file is not there it gives me this error and completes:

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*

this is the code I'm using:

#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 
3
  • 1
    What is $env: in the variable assignment supposed to do? Commented Aug 14, 2013 at 17:48
  • Can you update the error message for the latest attempt? Commented Aug 15, 2013 at 14:05
  • Taylor Tvrdy Updated Error Message: Get-Content : Cannot find path 'C:\scripts\importfile.txt' because it does not exist. At C:\Scripts\CLI_Localadmins.ps1:17 char:42 + If ($ValidPath -eq $True) {$Computers = Get-Content -Path 'c:\scripts\importfil ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (C:\scripts\importfile.txt:String) [Get-Content], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand Processing ... Script completed for online systems Commented Aug 15, 2013 at 20:11

3 Answers 3

3

The problem is in the IF statement as described by the error statement.. Try removing the exclamation point

Sign up to request clarification or add additional context in comments.

7 Comments

ok now the else side works but the if side does not! if the file exists it is not getting the input from it. It still pulls data from the else side of the statement.
Is the value of $FileExists different in each case?
Then the problem is in the way you are testing if the file exists
Changed the input checking to: #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 Still works with the input file but does not go to the else statement
@user2654059 Please don't post code in comments. As you can see it's practically unreadable. Update your question instead.
|
0

The problem with your condition is that Test-Path $ChkFile -IsValid checks only if $ChkFile is a valid path, not if it actually exists. If you want to test for existence you need to remove -IsValid. Also, I'd recommend using -LiteralPath, because by default Test-Path treats the path as a regular expression, which causes problems when a path contains special characters like square brackets.

#Check if import file exists.
$ChkFile = "c:\scripts\importfile.txt" 
if (Test-Path -LiteralPath $ChkFile) {
  $Computers = Get-Content $ChkFile
} else {
  $Computers = Get-QADComputer -SizeLimit 0 | select -Expand name
}

Comments

0

I found this website that may help. Here is a quote from the article, "An important warning about using the -isValid switch...since there’s nothing syntactically wrong with the path. So Test-Path -isValid $profile will always return true."I believe the -isValid switch is just checking the syntax of the path and making sure it is correct, it doesn't actually check if the path is there.

Try using split-path instead of -isValid like this

$ValidPath = Test-Path (split-path $ChkFile)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.