$PSVersionTable.PSVersion
Major Minor Build Revision
----- ----- ----- --------
5 1 22621 4391
Consider the following snippet
# Function to determine if the OS is Windows Server
function Is-WindowsServer {
$osVersion = (Get-WmiObject -Class Win32_OperatingSystem).Caption
return $osVersion -like "*Server*"
}
# Function to check if SMB/Network Sharing is installed
function Is-SMBInstalled {
if (Is-WindowsServer) {
Write-Output "Detected Windows Server. This should print."
$smbFeature = Get-WindowsFeature -Name FS-SMB1 -ErrorAction SilentlyContinue
if ($smbFeature -and $smbFeature.Installed) {
return $true
} else {
return $false
}
} else {
Write-Output "Detected Windows 10/11 client. This should print."
$smbFeature = Get-WindowsOptionalFeature -Online -FeatureName "SMB1Protocol" -ErrorAction SilentlyContinue
if ($smbFeature -and $smbFeature.State -eq "Enabled") {
return $true
} else {
return $false
}
}
Write-Output "This should be unreachable."
}
Is-SMBInstalled # probe
if (Is-SMBInstalled -eq $true) {
Write-Output "If above line is False. This should not print."
}
Expected output on Windows 10/11 with Samba disabled:
Detected Windows 10/11 client. This should print.
False
Script output on Windows 10/11 with Samba disabled:
Detected Windows 10/11 client.
False
If above line is False. This should not print.
Moreover, by itself,
if (Is-SMBInstalled -eq $true) {
Write-Output "If above line is False. This should not print."
}
doesn't seem to properly calls Is-SMBInstalled. Without Is-SMBInstalled # probe, the output is just If above line is False. This should not print.
I've tried refactoring by putting Is-SMBInstalled inside a $variable, this works in a sense that the function is properly called. But the conditional still behaves unexpectedly.
I also tried the grouping function as per https://stackoverflow.com/a/66585435/14097947 if ((Is-SMBInstalled)) {, this also doesn't work.
if (Is-SMBInstalled -eq $true) ...indeed needs to beif ((Is-SMBInstalled) -eq $true) ..., as explained in the answer you linked to; without the(...)around the function call,-eqand$trueare passed as arguments to yourIs-SMBInstalledfunction (which the latter happens to ignore).Write-Output, which writes to the success output stream, just likereturndoes, thereby modifying the function's "return value", as pointed out in sirtao's answer and as explained in more detail in the linked duplicate.