- 
                Notifications
    You must be signed in to change notification settings 
- Fork 54
WMI resource provider #279
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
          
     Merged
      
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            6 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      bf7dd5c
              
                WMI provider
              
              
                 2f8b303
              
                Added WMI provider
              
              
                 d0998cc
              
                Merge branch 'main' into wmi_provider
              
              
                 9713d2b
              
                Fixed WMI test
              
              
                 6f3e789
              
                Merge branch 'wmi_provider' of https://github.com/anmenaga/DSC-New in…
              
              
                 667b916
              
                Feedback 1
              
              
                 File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,13 @@ | ||
| # Example configuration for reading data from Windows WMI | ||
| $schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/10/config/document.json | ||
| resources: | ||
| - name: Get info from WMI | ||
| type: DSC/WMIGroup | ||
| properties: | ||
| resources: | ||
| - name: Get OS Info | ||
| type: root.cimv2/Win32_OperatingSystem | ||
| - name: Get BIOS Info | ||
| type: root.cimv2/Win32_BIOS | ||
| - name: Get Processor Info | ||
| type: root.cimv2/Win32_Processor | ||
  
    
      This file contains hidden or 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,54 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
|  | ||
| Describe 'PowerShellGroup resource tests' { | ||
|  | ||
| BeforeAll { | ||
| if ($IsWindows) | ||
| { | ||
| $OldPSModulePath = $env:PSModulePath | ||
| $env:PSModulePath += ";" + $PSScriptRoot | ||
|  | ||
| $configPath = Join-path $PSScriptRoot "test_wmi_config.dsc.yaml" | ||
|  | ||
| $dscPath = (get-command dsc -CommandType Application).Path | ||
| $dscFolder = Split-Path -Path $dscPath | ||
| $wmiGroupOptoutFile = Join-Path $dscFolder "wmigroup.dsc.resource.json.optout" | ||
| $wmiGroupOptinFile = Join-Path $dscFolder "wmigroup.dsc.resource.json" | ||
| Rename-Item -Path $wmiGroupOptoutFile -NewName $wmiGroupOptinFile | ||
| } | ||
| } | ||
| AfterAll { | ||
| if ($IsWindows) | ||
| { | ||
| $env:PSModulePath = $OldPSModulePath | ||
| Rename-Item -Path $wmiGroupOptinFile -NewName $wmiGroupOptoutFile | ||
| } | ||
| } | ||
|  | ||
| It 'List shows WMI resources' -Skip:(!$IsWindows){ | ||
|  | ||
| $r = dsc resource list *OperatingSystem* | ||
| $LASTEXITCODE | Should -Be 0 | ||
| $res = $r | ConvertFrom-Json | ||
| $res.Count | Should -BeGreaterOrEqual 1 | ||
| } | ||
|  | ||
| It 'Get works on an individual WMI resource' -Skip:(!$IsWindows){ | ||
|  | ||
| $r = dsc resource get -r root.cimv2/Win32_OperatingSystem | ||
| $LASTEXITCODE | Should -Be 0 | ||
| $res = $r | ConvertFrom-Json | ||
| $res.actualState.CreationClassName | Should -Be "Win32_OperatingSystem" | ||
| } | ||
|  | ||
| It 'Get works on a config with WMI resources' -Skip:(!$IsWindows){ | ||
|  | ||
| $r = Get-Content -Raw $configPath | dsc config get | ||
| $LASTEXITCODE | Should -Be 0 | ||
| $res = $r | ConvertFrom-Json | ||
| $res.results[0].result.actualState[0].LastBootUpTime | Should -Not -BeNull | ||
| $res.results[0].result.actualState[1].BiosCharacteristics | Should -Not -BeNull | ||
| $res.results[0].result.actualState[2].NumberOfLogicalProcessors | Should -Not -BeNull | ||
| } | ||
| } | 
  
    
      This file contains hidden or 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,2 @@ | ||
| wmigroup.resource.ps1 | ||
|         
                  anmenaga marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| wmigroup.dsc.resource.json.optout | ||
  
    
      This file contains hidden or 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,37 @@ | ||
| { | ||
| "$schema": "https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/08/bundled/resource/manifest.json", | ||
| "type": "DSC/WMIGroup", | ||
| "version": "0.1.0", | ||
| "description": "Resource provider to WMI resources.", | ||
| "tags": [ | ||
| "PowerShell" | ||
| ], | ||
| "provider": { | ||
| "list": { | ||
| "executable": "powershell", | ||
| "args": [ | ||
| "-NoLogo", | ||
| "-NonInteractive", | ||
| "-NoProfile", | ||
| "-Command", | ||
| "./wmigroup.resource.ps1 List" | ||
| ] | ||
| }, | ||
| "config": "full" | ||
| }, | ||
| "get": { | ||
| "executable": "powershell", | ||
| "args": [ | ||
| "-NoLogo", | ||
| "-NonInteractive", | ||
| "-NoProfile", | ||
| "-Command", | ||
| "$Input | ./wmigroup.resource.ps1 Get" | ||
| ], | ||
| "input": "stdin" | ||
| }, | ||
| "exitCodes": { | ||
| "0": "Success", | ||
| "1": "Error" | ||
| } | ||
| } | 
  
    
      This file contains hidden or 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,129 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
|  | ||
| [CmdletBinding()] | ||
| param( | ||
| [ValidateSet('List','Get','Set','Test')] | ||
| $Operation = 'List', | ||
| [Parameter(ValueFromPipeline)] | ||
| $stdinput | ||
| ) | ||
|  | ||
| $ProgressPreference = 'Ignore' | ||
| $WarningPreference = 'Ignore' | ||
| $VerbosePreference = 'Ignore' | ||
|  | ||
| if ($Operation -eq 'List') | ||
| { | ||
| $clases = Get-CimClass | ||
|  | ||
| foreach ($r in $clases) | ||
| { | ||
| $version_string = ""; | ||
| $author_string = ""; | ||
| $moduleName = ""; | ||
|  | ||
| $propertyList = @() | ||
| foreach ($p in $r.CimClassProperties) | ||
| { | ||
| if ($p.Name) | ||
| { | ||
| $propertyList += $p.Name | ||
| } | ||
| } | ||
|  | ||
| $namespace = $r.CimSystemProperties.Namespace.ToLower().Replace('/','.') | ||
| $classname = $r.CimSystemProperties.ClassName | ||
| $fullResourceTypeName = "$namespace/$classname" | ||
| $requiresString = "DSC/WMIGroup" | ||
|  | ||
| $z = [pscustomobject]@{ | ||
| type = $fullResourceTypeName; | ||
| version = $version_string; | ||
| path = ""; | ||
| directory = ""; | ||
| implementedAs = ""; | ||
| author = $author_string; | ||
| properties = $propertyList; | ||
| requires = $requiresString | ||
| } | ||
|  | ||
| $z | ConvertTo-Json -Compress | ||
| } | ||
| } | ||
| elseif ($Operation -eq 'Get') | ||
| { | ||
| $inputobj_pscustomobj = $null | ||
| if ($stdinput) | ||
| { | ||
| $inputobj_pscustomobj = $stdinput | ConvertFrom-Json | ||
| } | ||
|  | ||
| $result = @() | ||
|  | ||
| if ($inputobj_pscustomobj.resources) # we are processing a config batch | ||
| { | ||
| foreach($r in $inputobj_pscustomobj.resources) | ||
| { | ||
| $type_fields = $r.type -split "/" | ||
| $wmi_namespace = $type_fields[0].Replace('.','\') | ||
| $wmi_classname = $type_fields[1] | ||
|  | ||
| #TODO: add filtering based on supplied properties of $r | ||
| $wmi_instances = Get-CimInstance -Namespace $wmi_namespace -ClassName $wmi_classname | ||
|  | ||
| if ($wmi_instances) | ||
| { | ||
| $instance_result = @{} | ||
| $wmi_instance = $wmi_instances[0] # for 'Get' we return just first matching instance; for 'export' we return all instances | ||
| $wmi_instance.psobject.properties | %{ | ||
| if (($_.Name -ne "type") -and (-not $_.Name.StartsWith("Cim"))) | ||
| { | ||
| $instance_result[$_.Name] = $_.Value | ||
| } | ||
| } | ||
|  | ||
| $result += @($instance_result) | ||
| } | ||
| else | ||
| { | ||
| $errmsg = "Can not find type " + $r.type + "; please ensure that Get-CimInstance returns this resource type" | ||
| Write-Error $errmsg | ||
| exit 1 | ||
| } | ||
| } | ||
| } | ||
| else # we are processing an individual resource call | ||
| { | ||
| $type_fields = $inputobj_pscustomobj.type -split "/" | ||
| $wmi_namespace = $type_fields[0].Replace('.','\') | ||
| $wmi_classname = $type_fields[1] | ||
|  | ||
| #TODO: add filtering based on supplied properties of $inputobj_pscustomobj | ||
| $wmi_instances = Get-CimInstance -Namespace $wmi_namespace -ClassName $wmi_classname | ||
|  | ||
| if ($wmi_instances) | ||
| { | ||
| $wmi_instance = $wmi_instances[0] # for 'Get' we return just first matching instance; for 'export' we return all instances | ||
| $result = @{} | ||
| $wmi_instance.psobject.properties | %{ | ||
| if (($_.Name -ne "type") -and (-not $_.Name.StartsWith("Cim"))) | ||
| { | ||
| $result[$_.Name] = $_.Value | ||
| } | ||
| } | ||
| } | ||
| else | ||
| { | ||
| $errmsg = "Can not find type " + $inputobj_pscustomobj.type + "; please ensure that Get-CimInstance returns this resource type" | ||
| Write-Error $errmsg | ||
| exit 1 | ||
| } | ||
| } | ||
|  | ||
| $result | ConvertTo-Json -Compress | ||
| } | ||
| else | ||
| { | ||
| Write-Error "ERROR: Unsupported operation requested from wmigroup.resource.ps1" | ||
| } | 
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.