Skip to content

Commit

Permalink
cleanup, not tested yet
Browse files Browse the repository at this point in the history
  • Loading branch information
craigcomstock committed Oct 3, 2024
1 parent eb7232a commit 4ea4b7f
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 46 deletions.
54 changes: 33 additions & 21 deletions management/windows-capability/windows-capability.cf
Original file line number Diff line number Diff line change
@@ -1,41 +1,53 @@
bundle agent windows_capability_present(capability_name)
{
methods:
"Present" usebundle => windows_capability:_windows_capability_state("${capability_name}", "Present");
}
bundle agent windows_optionial_capability_NotPresent(capability_name)
{
methods:
"NotPresent" usebundle => windows_capability:_windows_capability_state("${capability_name}", "NotPresent");
}

bundle agent windows_capability
{
methods:
windows.!data:disable_windows_capability_inventory::
"windows_capability_inventory";
"windows_capability:inventory";
}

body file control
{
namespace => "windows_capability";
}

# https://learn.microsoft.com/en-us/powershell/module/dism/get-windowscapability?view=windowsserver2022-ps
# https://learn.microsoft.com/en-us/powershell/module/dism/remove-windowscapability?view=windowsserver2022-ps
# https://learn.microsoft.com/en-us/powershell/module/dism/add-windowscapability?view=windowsserver2022-ps
bundle agent windows_capability_inventory
bundle agent inventory
{
vars:
windows::
"${this.bundle}_cache_file" string => "${sys.statedir}${const.dirsep}${this.bundle}_cache.csv";
"${this.bundle}_command" string => "Get-WindowsCapability -Online | ConvertTo-Csv -notypeinformation | select-object -skip 1 | Set-Content -Path '${${this.bundle}_cache_file}'",
if => not(fileexists("${${this.bundle}_cache_file}"));
"csv" data => readcsv("${${this.bundle}_cache_file}");
"cache_file" string => "${sys.statedir}${const.dirsep}${this.bundle}_cache.csv";
"command" string => "Get-WindowsCapability -Online | ConvertTo-Csv -notypeinformation | select-object -skip 1 | Set-Content -Path '${cache_file}'";

"csv" data => readcsv("${cache_file}");
"i" slist => getindices("csv");
"${this.bundle}[${i}]" string => "${csv[${i}][0]}:${csv[${i}][1]}",
meta => { "inventory", "attribute_name=Windows Capability" };

files:
"${cache_file}"
file_select => default:days_old(1),
delete => default:tidy;

commands:
windows::
"${${this.bundle}_command}"
contain => powershell;
"${command}"
if => not(fileexists("${cache_file}")),
contain => default:powershell;
}

bundle agent windows_capability_present(capability_name)
{
methods:
"Present" usebundle => _windows_capability_state("${capability_name}", "Present");
}
bundle agent windows_optionial_capability_NotPresent(capability_name)
{
methods:
"NotPresent" usebundle => _windows_capability_state("${capability_name}", "NotPresent");
}

bundle agent _windows_capability_state(capability_name, desired_state)
{
Expand All @@ -50,11 +62,11 @@ reports:
commands:
windows.state_not_ok::
"${operation}-WindowsCapability -Online -Name ${capability_name}"
classes => if_ok("state_changed"),
contain => powershell;
classes => default:if_ok("state_changed"),
contain => default:powershell;

files:
windows.state_changed_ok::
"windows_capability_inventory_cache_file"
delete => tidy;
delete => default:tidy;
}
64 changes: 39 additions & 25 deletions management/windows-optional-feature/windows-optional-feature.cf
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,70 @@
# https://learn.microsoft.com/en-us/powershell/module/dism/enable-windowsoptionalfeature?view=windowsserver2022-ps
# https://learn.microsoft.com/en-us/virtualization/hyper-v-on-windows/quick-start/enable-hyper-v#enable-hyper-v-using-powershell

bundle agent windows_optional_feature_enabled( feature_name )
{
methods:
"Enabled" usebundle => windows_optional_feature:_promise_state("${feature_name}", "Enabled");
}

bundle agent windows_optional_feature_disabled( feature_name )
{
methods:
"Disabled" usebundle => windows_optional_feature:_promise_state("${feature_name}", "Disabled");
}

bundle agent windows_optional_feature
{
methods:
windows.!data:disable_windows_optional_feature_inventory::
"windows_optional_feature_inventory";
"windows_optional_feature:inventory";
}

body file control
{
namespace => "windows_optional_feature";
}

bundle agent windows_optional_feature_inventory
bundle agent inventory
{
vars:
windows::
"${this.bundle}_cache_file" string => "${sys.statedir}${const.dirsep}${this.bundle}_cache.csv";
"${this.bundle}_command" string => "Get-WindowsOptionalFeature -Online | ConvertTo-Csv -notypeinformation | select-object -skip 1 | Set-Content -Path '${${this.bundle}_cache_file}'";
"csv" data => readcsv("${${this.bundle}_cache_file}");
"cache_file" string => "${sys.statedir}${const.dirsep}${this.bundle}_cache.csv";
"command" string => "Get-WindowsOptionalFeature -Online | ConvertTo-Csv -notypeinformation | select-object -skip 1 | Set-Content -Path '${cache_file}'";
"csv" data => readcsv("${cache_file}");
"i" slist => getindices("csv");
"${this.bundle}[${i}]" string => "${csv[${i}][0]}:${csv[${i}][1]}",
meta => { "inventory", "attribute_name=Windows Optional Features" };

files:
"${cache_file}"
file_select => default:days_old(1),
delete => default:tidy;

commands:
windows::
"${${this.bundle}_command}"
contain => powershell;
windows::
"${command}"
if => not(fileexists("${cache_file}")),
contain => default:powershell;
}

bundle agent windows_optional_feature_state(feature_name, desired_state)
bundle agent promise_state(feature_name, desired_state)
{
vars:
"state_name" string => ifelse(regcmp("enable*", string_downcase("${desired_state}")), "Enabled", "Disabled");
"state_prefix" string => ifelse(regcmp("enable*", string_downcase("${desired_state}")), "Enable", "Disable");
commands:
"operation" string => ifelse(strcmp("${desired_state}", "Enabled"), "Enable", "Disable");
classes:
windows::
"if((Get-WindowsOptionalFeature -Online -FeatureName ${feature_name} | select-object -expandproperty state) -ne '${state_name}'){exit 0}else{exit 1}"
contain => powershell,
classes => kept0_repaired1("state_not_ok","state_ok");
"state_not_ok" expression => returnszero( "if((Get-WindowsOptionalFeature -Online -FeatureName ${feature_name} | select-object -expandproperty state) -ne '${state_name}'){exit 0}else{exit 1}", "powershell");
commands:
windows.state_not_ok::
# -All enables parent features if need be
"${state_prefix}-WindowsOptionalFeature -Online -FeatureName ${feature_name} -NoRestart -All"
contain => powershell,
classes => if_repaired("restart_needed");
"${operation}-WindowsOptionalFeature -Online -FeatureName ${feature_name} -NoRestart -All"
contain => default:powershell,
classes => default:if_repaired("restart_needed");
# -NoRestart so that we don't wait forever for a Yes reply from a user
# then if we ran this command due to needing to, restart below.

classes:
windows::
"default:restart_needed" expression => "restart_needed";
}
body classes kept0_repaired1(kept0_class, repaired1_class)
{
kept_returncodes => { "0" };
repaired_returncodes => { "1" };
promise_kept => { "${kept0_class}" };
promise_repaired => { "${repaired1_class}" };
}

0 comments on commit 4ea4b7f

Please sign in to comment.