-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WindowsFeature tool for managing Windows features (#3572)
* 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
Showing
2 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) |