Share via

output not in readable format in variable

iconoclast88 61 Reputation points
2021-06-08T18:51:28.327+00:00

I'm trying to find if the FS-Resource-Manager feature is installed on a group of servers (for continued server management and automation.

I need to execute a script on servers that don't have the feature yet turned on.
If it is already installed I want the script to end.

To find out if it is installed or not, i put the output of the command "get-windowsfeature *FS-Resource-Manager" in a variable.
Then I want the yes or no captured by putting this answer in a variable. It is either True or False when I ask "$suboutput=$srv.Installed"
I get a False returned. It looks like text, but when i then use it in an "If" statement and write-host text if its equal to the next variable, then the line exits with no results.

PS C:\tmp> $srv=get-windowsfeature *FS-Resource-Manager
PS C:\tmp> $srv

Display Name Name Install State


    [ ] File Server Resource Manager                FS-Resource-Manager            Available

PS C:\tmp> $suboutput=$srv.Installed
PS C:\tmp> $suboutput
False
PS C:\tmp> $boolean=out-string -InputObject $suboutput
PS C:\tmp> $boolean
False

PS C:\tmp> if ($suboutput -eq "False") {Write-Host "Yes, it is reading the string"}
PS C:\tmp>

Now compare this to if I substitute the $boolean variable for a test.

$test="Dog"

It works fine.

PS C:\tmp> $test="Dog"
PS C:\tmp> $test
Dog
PS C:\tmp> if ($test -eq "Dog") {write-host "This is a Dog!"}
This is a Dog!
PS C:\tmp>

Question. What do i need to do to the output to make it readable to the rest of the script?

thanks!

-Josh

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments

Answer accepted by question author

Anonymous
2021-06-09T02:44:55.413+00:00

Hi,

"False" is a String of five characters, not a Boolean. The Booleans in PowerShell are $True and $False though they are printed as True and False. You can run

if ($suboutput -eq $False) {Write-Host "Yes, it is reading the string"}  

or simply

if (-not $suboutput) {Write-Host "Yes, it is reading the string"}  

Best Regards,
Ian Xue

============================================

If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Was this answer helpful?

0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Rich Matheisen 48,116 Reputation points
    2021-06-08T19:43:08.39+00:00

    The "Installed" property is a Boolean value. There's no need to convert it to a string.

    if ($srv.Installed){write-host "Feature is installed"}
    
    if (-not $srv.Installed){write-host "Feature is not installed"}
    

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.