question

iconoclast88 avatar image
0 Votes"
iconoclast88 asked IanXue-MSFT edited

output not in readable format in variable

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-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered IanXue-MSFT edited

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered

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"}


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.