Reputation: 979
I need another set of eyes because I'm certain this is simple but it's kicking my butt right now. I'm writing a script to install Windows roles/features on both Server 2008 and Server 2012 machines.
I know that 2008 uses Add-WindowsFeature
whereas 2012 uses Install-WindowsFeature
. So my goal is to look at the OS and if it's 2008 create a variable called $Install
with a value of Add-WindowsFeature
and if it's not 2008 give it the value of Install-WindowsFeature
.
This is what I have now:
$OS = (Get-WmiObject Win32_OperatingSystem).Name
if ($OS -like '2008') {
$Install = 'Add-WindowsFeature'
} else {
$Install = 'Install-WindowsFeature'
}
Currently, when I call up the $OS
variable I get a returned value of:
Microsoft Windows Server 2008 R2 Standard |C:\Windows|\Device\Harddisk0\Partition2
But when I call up the $Install
variable I get a returned value of:
Install-WindowsFeature
I've also tried -contains
instead of -like
but I get the same result. I've also tried eliminating the spaces before the curly brackets, but still no change. What am I doing wrong?
Upvotes: 0
Views: 107
Reputation: 416131
The -like
operator is not a regular expression comparison. If you don't have any wildcards, the -like
match is the same as an equality comparison.
You need to either include wildcards, like this:
if ($OS -like '*2008*') {
do a contains check, like this:
if ($OS.Contains('2008')) {
or use a regular expression match, like this:
if ($OS -match '2008') {
Documentation is here:
Upvotes: 4
Reputation: 121
Alternatively the cmdlet Enable-WindowsOptionalFeature
should be available on 2008 and beyond machines. You should be able to use that to install whatever feature you need regardless of the OS.
Upvotes: 1