Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 即使条件返回false,If语句后的代码也始终运行?_Powershell_Powershell 3.0 - Fatal编程技术网

Powershell 即使条件返回false,If语句后的代码也始终运行?

Powershell 即使条件返回false,If语句后的代码也始终运行?,powershell,powershell-3.0,Powershell,Powershell 3.0,我有一个If语句,用于查看测试路径命令的结果。If语句后面的代码似乎总是在运行,但是,即使测试路径返回False,我也通过将测试路径的结果传递到Write Host命令来证明这一点。我错过了什么?代码如下: If (Test-Path "\\server\students\home\$user") { $user = $_.uname Set-ItemProperty -Path "\\server\students\home\$user"

我有一个If语句,用于查看测试路径命令的结果。If语句后面的代码似乎总是在运行,但是,即使测试路径返回False,我也通过将测试路径的结果传递到Write Host命令来证明这一点。我错过了什么?代码如下:

If (Test-Path  "\\server\students\home\$user") { 
            $user = $_.uname 
            Set-ItemProperty -Path "\\server\students\home\$user" -Name Attributes -Value "Normal"
            $acl = Get-Acl "\\server\students\home\$user"
            $acl.access | where-object {$_.AccessControlType -eq "Deny"} | Foreach-object { $acl.RemoveAccessRule($_) }
            (Get-Item "\\server\students\home\$user").SetAccessControl($acl)
            Get-ChildItem -Path "\\swcportal\students\home\$user\*" -Recurse | Remove-Item -Force -Recurse
            Remove-Item "\\server\students\home\$user" -Recurse
            Write-host "$user's home directory deleted successfully"
            }

您确定不想在IF中使用$\ uname吗。您在IF中使用$user,但随后直接使用不同的值。如果此代码在循环中,则您可能在IF测试中使用$user,从而使用以前的真实结果中的值。如@DeanOC先前所述,您在测试路径中使用$user,如果$User没有值,您的if将测试\\server\students\home\的路径,我认为这总是正确的。您可以尝试将第一个IF封装在第二个IF like IF$user中,该函数仅在$user设置为某个值时运行。