mercredi 29 mai 2019

Powershell If Statement Failure

I need another set of eyes because I'm certain this is simple but it's kicking my butt right now. And I realize this is the most basic of basics, so I'm almost embarrassed asking... But I need another set of eyes... 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 { brackets, but still no change. What am I doing wrong?

Aucun commentaire:

Enregistrer un commentaire