Skip to content

Commit

Permalink
WindowsFeature tool for managing Windows features
Browse files Browse the repository at this point in the history
WindowsFeature tool for managing Windows features
  • Loading branch information
SRIKKANTH committed Dec 30, 2024
1 parent dbfc839 commit df29bfb
Show file tree
Hide file tree
Showing 2 changed files with 75 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 WindowsFeature
from .wsl import Wsl

__all__ = [
Expand Down Expand Up @@ -260,5 +261,6 @@
"VirtualClientTool",
"Who",
"Whoami",
"WindowsFeature",
"Wsl",
]
73 changes: 73 additions & 0 deletions lisa/tools/windows_feature.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# 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


# WindowsFeature management tool for Windows.
# It can install, uninstall, and check the status of Windows features.
# This tool uses PowerShell to manage Windows features.
class WindowsFeature(Tool):
@property
def command(self) -> str:
return ""

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

def _check_exists(self) -> bool:
return True

def _initialize(self, *args: Any, **kwargs: Any) -> None:
self._powershell = self.node.tools[PowerShell]

def install_feature(self, name: str) -> None:
if self.is_installed(name):
self._log.debug(f"Feature {name} is already installed.")
return
self._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._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]:
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]:
return (
self._powershell.run_cmdlet(
"Get-WindowsFeature | Where-Object { $_.Installed -eq $false } | Select-Object -ExpandProperty Name", # noqa: E501
force_run=True,
)
.strip()
.split("\n")
)

0 comments on commit df29bfb

Please sign in to comment.