For example, I have the below function:
Function enable-RemoteDesktopConnections
{
Set-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server" -Name "fDenyTSConnections" -Value 00000000
Set-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server" -Name "fSingleSessionPerUser" -Value 00000000
Set-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp" -Name "UserAuthentication" -Value 00000000
}
This simply enables Remote Desktop. Now, I want to have some error handling within the function, but equally return a success or failure to the call itself so I can update values further in the script.
Can you wrap functions in a IF Statement and then use boolean to determine whether it was successful or not?
For example, take this function, where $a = "3"
Function foo {
If($a -eq "1"){
$true
}else{
$false
}}
We then wrap the Function in an IF i.e. if the function returned true, success, or false, Fail.
If(foo){
Write-Host "Success"
}else{
Write-Host "Fail"
}
Is there a better way, a more effective method, a more robust approach.