【发布时间】:2021-03-27 19:14:47
【问题描述】:
如果我使用不等于“Dynamique”或“Statique”的 TypeAdressage 调用函数,我想抛出错误,但是当我使用“Dynamique”调用它时会抛出错误
function Changer-Type {
param(
[string]$Identifiant,
[string]$TypeAdressage,
[string]$Path
)
if ($TypeAdressage -ne "Dynamique"-or ($TypeAdressage -ne "Statique")){
Write-Error "Type Adressage impossible"
}
else{
if(-not(Test-Path $Path -PathType Leaf) -or [IO.Path]::GetExtension($Path) -ne '.csv') {
throw 'File does not exist or is not a Csv...'
}
$computers = Import-Csv -Path $Path -Delimiter ';'
$computers | Where-Object { $_.Identifiant -eq $Identifiant } |
ForEach-Object { $_.TypeAdressage = $TypeAdressage }
# write the updated $computers object array back to disk
$computers | Export-Csv -Path $Path -Delimiter ';' -NoTypeInformation
}
}
$csvPath = 'C:\Temp\Peripherique.csv'
Changer-Type -Identifiant "Q00032" -TypeAdressage "Dynamique" -Path $csvPath
Changer-Type -Identifiant "PWIN10" -TypeAdressage "test" -Path $csvPath
【问题讨论】:
-
将
-or更改为-and -
我会将您的条件更改为:
if (-not ('Dynamique', 'Statique', 'orAnyOtherType' -eq $TypeAdressage)) { ...,另请参阅:If all values in 'foreach' are true -
Mathias 是对的,当您想测试两个事物的不等式时,-and 是正确的连词。与随意交谈相反。
标签: powershell conditional-statements