Beefcake
Beefcake

Reputation: 801

Powershell write IF statement into a Variable

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.

Upvotes: 1

Views: 2576

Answers (1)

Beefcake
Beefcake

Reputation: 801

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

Upvotes: 1

Related Questions