1

I'm wanting to have an If/Else script that can write an output to a variable which I will use later on in the script.

Here is what I have:

if (!(Get-Module -ListAvailable -Name servermanager)) {
    Import-Module servermanager
}

$WinFeat = Get-WindowsFeature -Name telnet-client | Where installed
if ($WinFeat -EQ $null) {
    Add-WindowsFeature telnet-client
}

Essentially I was thinking that I could do something like this:

if (!(Get-Module -ListAvailable -Name servermanager)) {
    Import-Module servermanager
} else {
    $alreadyModule
}

$WinFeat = Get-WindowsFeature -Name telnet-client | Where installed
if ($WinFeat -EQ $null) {
    Add-WindowsFeature telnet-client
} else {
    $alreadyFeature
}

if (($alreadyModule) -and ($alreadyFeature) {Write-Host "STUFF INSTALLED"}

That "stuff" will be used in an stdout that I will then use in an Ansible Playbook.

So essentially I want to say IF this is already installed, AND that is already installed, then write STUFF INSTALLED to be used in Ansible... but I can't seem to figure out how to write a variable without it first starting with:

$alreadyModule = ....
$alreadyFeature = ....

Any help would be greatly appreciated.

1
  • 2
    $alreadyFeature = (Get-WindowsFeature -Name telnet-client).Installed? Commented Oct 20, 2017 at 11:11

1 Answer 1

1

Thanks Ansgar, I've included yours and come up with:

if (!(Get-Module -ListAvailable -Name servermanager)) {
    Import-Module servermanager
} else {
    $alreadyModule = (Get-Module -ListAvailable -Name servermanager)
}


$WinFeat = Get-WindowsFeature -Name telnet-client | Where installed
if ($WinFeat -EQ $null) {
    Add-WindowsFeature telnet-client
} else {
    $alreadyFeature = (Get-WindowsFeature -Name telnet-client).Installed
}

if (($alreadyFeature) -and ($alreadyModule)) {Write-Host "STUFF INSTALLED"}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.