Why is my if statement always false?

user1123644

I run this script:

if (Copy-Item .\test.ps1 $env:SystemRoot\System32)
{
    Write-Host "Done."
    exit 0
}
else
{
    Write-Host "Not done."
    Write-Host "You must be root."
    exit 1
}

When I run this script as a normal user I got the message in else statement, because I am not root. And this is okay.

But I run this script as root I also got the message in else statement! But file copy operation is succeded. I can't get the message in if statement. Why?

I also check the error code and its always False.

SimonS

An if statement does not evaluate whether the command inside its condition ran successfully. It will only check the value (in your case the return of your command) and cast it to a bool.

Copy-Item does not return anything by default, and that's why your if statement is always false, because [bool]$null is $false.

You have three options here:

Add the -PassThru parameter to get some form of return:

if (Copy-Item .\test.ps1 $env:SystemRoot\System32 -PassThru)

Use the $? variable to see if your previous command was successful:

Copy-Item .\test.ps1 $env:SystemRoot\System32
if ($?) {
    Write-Host "Done."
    exit 0
}
else {
    Write-Host "Not done."
    Write-Host "You must be root."
    exit 1
}

However, the most reliable way would be to wrap it in Try {} Catch {} and add -ErrorAction Stop

Try {
    Copy-Item .\test.ps1 $env:SystemRoot\System32 -ErrorAction Stop
    Write-Host "Done."
    exit 0
}
Catch {
    Write-Host "Not done."
    Write-Host "You must be root."
    exit 1
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why is my if statement always evaluating to false within a switch statement?

Why is my condition always false?

Why is the first condition of "else if" statement always false?

Why is my if-statement condition: "if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE)" always false?

Why is my statement always false? if (isset($_GET['action']) && $_GET['action']=='IPN_Handler')

Using Google Sheets scripts, why does my if statement always return false when comparing cell values?

Why is my struct method always returning false?

PHP Why my 'if' statement always gives TRUE?

Why is my boolean statement always evaluating to true?

Why is my elif statement always executing in Godot?

Why is my if statement in Python evaluating to False?

Why is my if statement not evaluating false when it should be?

Why is my if...else statement not returning false?

Why always return "None" string, like my if statment was always false?

Function returning always false when using a Boolean on my statement

Java if () statement always false?

if statement always returning false

Why is this Java code always giving me "False" statement?

Why does my getColor function always return false?

I want to know why my 'login logic' always false condition?

Why is my UserControl Visible property always returning false?

Why is my Receiver for charging status always returning false?

Why if i disable my textBox to false, is always equal to true?

Why is my if statement always giving the same result in React?

Why the if condition is always false?

If always false, why?

Append always returning false statement

always false conditional statement - sonarqube

python if statement always returns False