Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WindowsFeature tool for managing Windows features #3572

Merged
merged 7 commits into from
Jan 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
SRIKKANTH marked this conversation as resolved.
Show resolved Hide resolved
# 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:
squirrelsc marked this conversation as resolved.
Show resolved Hide resolved
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:
squirrelsc marked this conversation as resolved.
Show resolved Hide resolved
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"
)
Loading