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 2 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 WindowsFeature
from .wsl import Wsl

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

from typing import Any, List

from lisa.executable import Tool
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 WindowsFeature(Tool):
squirrelsc marked this conversation as resolved.
Show resolved Hide resolved
@property
def command(self) -> str:
return ""

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

def _check_exists(self) -> bool:
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 not available: {e}")
squirrelsc marked this conversation as resolved.
Show resolved Hide resolved
return False

def _initialize(self, *args: Any, **kwargs: Any) -> None:
squirrelsc marked this conversation as resolved.
Show resolved Hide resolved
self._powershell = self.node.tools[PowerShell]

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._powershell.run_cmdlet(
squirrelsc marked this conversation as resolved.
Show resolved Hide resolved
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._powershell.run_cmdlet(
f"Uninstall-WindowsFeature -Name {name}",
force_run=True,
)

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

def get_installed_features(self) -> List[str]:
squirrelsc marked this conversation as resolved.
Show resolved Hide resolved
return (
self._powershell.run_cmdlet(
"Get-WindowsFeature | Where-Object { $_.Installed -eq $true } | Select-Object -ExpandProperty Name", # noqa: E501
force_run=True,
)
.strip()
.split("\n")
)

def get_available_features(self) -> List[str]:
squirrelsc marked this conversation as resolved.
Show resolved Hide resolved
return (
self._powershell.run_cmdlet(
"Get-WindowsFeature | Where-Object { $_.Installed -eq $false } | Select-Object -ExpandProperty Name", # noqa: E501
force_run=True,
)
.strip()
.split("\n")
)
Loading