Skip to content

Commit

Permalink
WindowsFeature tool for managing Windows features (#3572)
Browse files Browse the repository at this point in the history
* WindowsFeature tool for managing Windows features

WindowsFeature tool for managing Windows features

* Update windows_feature.py

* Update windows_feature.py

* Update windows_feature.py

* Update __init__.py

* Update windows_feature.py

* Update windows_feature.py
  • Loading branch information
SRIKKANTH authored Jan 1, 2025
1 parent 8c3d65d commit 8590aaa
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lisa/tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
from .virtualclient import VcRunner, VcTargetInfo, VirtualClientTool
from .who import Who
from .whoami import Whoami
from .windows_feature import WindowsFeatureManagement
from .wsl import Wsl

__all__ = [
Expand Down Expand Up @@ -260,5 +261,6 @@
"VirtualClientTool",
"Who",
"Whoami",
"WindowsFeatureManagement",
"Wsl",
]
69 changes: 69 additions & 0 deletions lisa/tools/windows_feature.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.

from lisa.executable import Tool
from lisa.operating_system import Windows
from lisa.tools.powershell import PowerShell
from lisa.util import LisaException


# WindowsFeature management tool for Windows Servers.
# It can install, uninstall, and check the status of Windows features.
# Hyper-V, DHCP etc. are examples of Windows features.
# This tool uses PowerShell to manage Windows features.
# Not supported on PC versions like Windows 10, 11 etc.
class WindowsFeatureManagement(Tool):
@property
def command(self) -> str:
return ""

@property
def can_install(self) -> bool:
return False

def _check_exists(self) -> bool:
assert isinstance(
self.node.os, Windows
), "WindowsFeatureManagement is only supported on Windows."
try:
self.node.tools[PowerShell].run_cmdlet(
"Get-WindowsFeature",
force_run=True,
)
self._log.debug("'Get-WindowsFeature' is installed")
return True
except LisaException as e:
self._log.debug(
f"'Get-WindowsFeature' is only available on Windows Server editions {e}"
)
return False

def install_feature(self, name: str) -> None:
if self.is_installed(name):
self._log.debug(f"Feature {name} is already installed.")
return
self.node.tools[PowerShell].run_cmdlet(
f"Install-WindowsFeature -Name {name} -IncludeManagementTools",
force_run=True,
)

def uninstall_feature(self, name: str) -> None:
if not self.is_installed(name):
self._log.debug(f"Feature {name} is not installed.")
return
self.node.tools[PowerShell].run_cmdlet(
f"Uninstall-WindowsFeature -Name {name}",
force_run=True,
)

def is_installed(self, name: str) -> bool:
return bool(
self.node.tools[PowerShell]
.run_cmdlet(
f"Get-WindowsFeature -Name {name} | Select-Object -ExpandProperty Installed", # noqa: E501
force_run=True,
fail_on_error=False,
)
.strip()
== "True"
)

0 comments on commit 8590aaa

Please sign in to comment.