mardi 3 février 2015

Powershell - the if statement in a function only uses the first if even though the criteria is not met

I have the following function but no matter what $Server I specify, the first if statement is always used even though the criteria is not met. This causes the wrong credentials to be used. I've tried numerous re-writes "" in different places, with and without wildcards but I always have the same issue. Its probably a simple oversight but I cant spot it (powershell newbie).



$CredsFile = "C:\cred.txt"

Function DriveSpace {

$Server= "Myserver"

if ($Server -contains "application" -or "web") {
$password = get-content $CredsFile | convertto-securestring
$Cred = New-Object System.Management.Automation.PsCredential $Server"\username",$password
$Output = Get-WmiObject win32_logicaldisk -computername $Server -Credential $cred|
Where-Object { $_.DriveType -eq 3 } |
Select SystemName,DeviceID,VolumeName,@{Name="Size(GB)";Expression={"{0:N2}" -f($_.size/1gb)}},@{Name="FreeSpace(GB)";Expression={"{0:N2}" -f($_.freespace/1gb)}} |Out-String
}
Else {
$Output = Get-WmiObject win32_logicaldisk -computername $Server|
Where-Object { $_.DriveType -eq 3 } |
Select SystemName,DeviceID,VolumeName,@{Name="Size(GB)";Expression={"{0:N2}" -f($_.size/1gb)}},@{Name="FreeSpace(GB)";Expression={"{0:N2}" -f($_.freespace/1gb)}} |Out-String
}
$Output

}


Any advice greatly appreciated


Aucun commentaire:

Enregistrer un commentaire