Last active
August 29, 2015 14:19
-
-
Save jeffpatton1971/6eb4479dd6ab4e4edfe0 to your computer and use it in GitHub Desktop.
A pair of cmdlets for working with mof files, this is not complete, currently it only handles a few mof instances 'MSFT_RegistryResource','MSFT_RoleResource','MSFT_ServiceResource','OMI_ConfigurationDocument'
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
Function Get-MofFile | |
{ | |
[CmdletBinding()] | |
Param | |
( | |
[string]$Path | |
) | |
Begin | |
{ | |
$MofFile = Get-Content $Path | |
$Counter = 0 | |
$Instances = @() | |
foreach ($line in $MofFile) | |
{ | |
$Trimmed = $line.Trim(); | |
if ($Trimmed.StartsWith('instance of')) | |
{ | |
$Instances += $Counter | |
} | |
$Counter ++ | |
} | |
} | |
Process | |
{ | |
$Finish = $Instances.Count | |
For ($Counter =0;$Counter -lt $Finish;$Counter++) | |
{ | |
$Start = $Instances[$Counter] | |
$End = $Instances[$Counter+1] | |
if ($End -eq $null) | |
{ | |
$End = $MofFile.Count | |
} | |
$Instance = $MofFile[$Start..($End -1)] | |
$InstanceName = $Instance[0].Replace('instance of ','') | |
Write-Verbose $InstanceName | |
$InstanceEnd = $InstanceName.IndexOf(' ') | |
if ($InstanceEnd -eq -1) | |
{ | |
$Name = $InstanceName | |
} | |
else | |
{ | |
$Name = $InstanceName.Substring(0,$InstanceEnd) | |
} | |
try | |
{ | |
Write-Verbose $Name | |
ConvertTo-MofInstance -Type $Name.Trim() -Instance $Instance | |
Write-Verbose $Instance | |
} | |
catch | |
{ | |
} | |
} | |
} | |
End | |
{ | |
} | |
} | |
Function ConvertTo-MofInstance | |
{ | |
[CmdletBinding()] | |
param | |
( | |
[ValidateSet('MSFT_RegistryResource','MSFT_RoleResource','MSFT_ServiceResource','OMI_ConfigurationDocument')] | |
[string]$Type, | |
[object]$Instance | |
) | |
begin | |
{ | |
} | |
process | |
{ | |
switch ($Type) | |
{ | |
'MSFT_RegistryResource' | |
{ | |
$ResourceID = ($Instance |Select-String 'ResourceID =').ToString().Trim().Replace(";","").Split("=")[1].Trim().Replace('"','') | |
$ValueName = ($Instance |Select-String 'ValueName =').ToString().Trim().Replace(";","").Split("=")[1].Trim().Replace('"','') | |
$Key = ($Instance |Select-String 'Key =').ToString().Trim().Replace(";","").Split("=")[1].Trim().Replace('"','') | |
$Ensure = ($Instance |Select-String 'Ensure =').ToString().Trim().Replace(";","").Split("=")[1].Trim().Replace('"','') | |
$SourceInfo = ($Instance |Select-String 'SourceInfo =').ToString().Trim().Replace(";","").Split("=")[1].Trim().Replace('"','') | |
$ValueType = ($Instance |Select-String 'ValueType =').ToString().Trim().Replace(";","").Split("=")[1].Trim().Replace('"','') | |
$ModuleName = ($Instance |Select-String 'ModuleName =').ToString().Trim().Replace(";","").Split("=")[1].Trim().Replace('"','') | |
$ModuleVersion = ($Instance |Select-String 'ModuleVersion =').ToString().Trim().Replace(";","").Split("=")[1].Trim().Replace('"','') | |
for ($Counter =0;$Counter -lt ($Instance.Count);$Counter ++) | |
{ | |
$Trimmed = $Instance[$Counter].Trim() | |
if ($Trimmed.StartsWith('ValueData')) | |
{ | |
$ValueData = ($Instance[$Counter+1].Trim()).Replace('"','') | |
} | |
} | |
New-Object -TypeName psobject -Property @{ | |
Instance = $Type; | |
ResourceID = $ResourceID; | |
ValueName = $ValueName | |
Key = $Key; | |
Ensure = $Ensure; | |
SourceInfo = $SourceInfo; | |
ValueType = $ValueType; | |
ModuleName = $ModuleName; | |
ValueData = $ValueData; | |
ModuleVersion = $ModuleVersion; | |
} |Select-Object -Property Instance, ResourceID, ValueName, Key, Ensure, SourceInfo, ValueType, ModuleName, ValueData, ModuleVersion | |
} | |
'MSFT_RoleResource' | |
{ | |
$ResourceID = ($Instance |Select-String 'ResourceID =').ToString().Trim().Replace(";","").Split("=")[1].Trim().Replace('"','') | |
$Ensure = ($Instance |Select-String 'Ensure =').ToString().Trim().Replace(";","").Split("=")[1].Trim().Replace('"','') | |
$SourceInfo = ($Instance |Select-String 'SourceInfo =').ToString().Trim().Replace(";","").Split("=")[1].Trim().Replace('"','') | |
$Names = ($Instance |Select-String 'Name =') | |
if ($Names[0].ToString().Split('=')[0].Trim() -eq 'Name') | |
{ | |
$Name = $Names[0].ToString().Split('=')[1].Trim().Replace('"','').Replace(';','') | |
} | |
$ModuleName = ($Instance |Select-String 'ModuleName =').ToString().Trim().Replace(";","").Split("=")[1].Trim().Replace('"','') | |
$ModuleVersion = ($Instance |Select-String 'ModuleVersion =').ToString().Trim().Replace(";","").Split("=")[1].Trim().Replace('"','') | |
New-Object -TypeName psobject -Property @{ | |
Instance = $Type; | |
ResourceID = $ResourceID; | |
Ensure = $Ensure; | |
SourceInfo = $SourceInfo; | |
Name = $Name; | |
ModuleName = $ModuleName; | |
ModuleVersion = $ModuleVersion; | |
} |Select-Object -Property Instance, ResourceID, Ensure, SourceInfo, Name, ModuleName, ModuleVersion | |
} | |
'MSFT_ServiceResource' | |
{ | |
$ResourceID = ($Instance |Select-String 'ResourceID =').ToString().Trim().Replace(";","").Split("=")[1].Trim().Replace('"','') | |
$StartupType = ($Instance |Select-String 'StartupType =').ToString().Trim().Replace(";","").Split("=")[1].Trim().Replace('"','') | |
$SourceInfo = ($Instance |Select-String 'SourceInfo =').ToString().Trim().Replace(";","").Split("=")[1].Trim().Replace('"','') | |
$Names = ($Instance |Select-String 'Name =') | |
if ($Names[0].ToString().Split('=')[0].Trim() -eq 'Name') | |
{ | |
$Name = $Names[0].ToString().Split('=')[1].Trim().Replace('"','').Replace(';','') | |
} | |
$ModuleName = ($Instance |Select-String 'ModuleName =').ToString().Trim().Replace(";","").Split("=")[1].Trim().Replace('"','') | |
$ModuleVersion = ($Instance |Select-String 'ModuleVersion =').ToString().Trim().Replace(";","").Split("=")[1].Trim().Replace('"','') | |
New-Object -TypeName psobject -Property @{ | |
Instance = $Type; | |
ResourceID = $ResourceID; | |
StartupType = $StartupType; | |
SourceInfo = $SourceInfo; | |
Name = $Name; | |
ModuleName = $ModuleName; | |
ModuleVersion = $ModuleVersion; | |
} |Select-Object -Property Instance, ResourceID, SourceInfo, Name, StartupType, ModuleName, ModuleVersion | |
} | |
'OMI_ConfigurationDocument' | |
{ | |
$Version = ($Instance |Select-String 'Version=').ToString().Trim().Replace(";","").Split("=")[1].Trim().Replace('"','') | |
$Author = ($Instance |Select-String 'Author=').ToString().Trim().Replace(";","").Split("=")[1].Trim().Replace('"','') | |
$GenerationDate = ($Instance |Select-String 'GenerationDate=').ToString().Trim().Replace(";","").Split("=")[1].Trim().Replace('"','') | |
$GenerationHost = ($Instance |Select-String 'GenerationHost=').ToString().Trim().Replace(";","").Split("=")[1].Trim().Replace('"','') | |
New-Object -TypeName psobject -Property @{ | |
Instance = $Type | |
Version = $Version | |
Author = $Author | |
GenerationDate = (Get-Date $GenerationDate) | |
GenerationHost = $GenerationHost | |
} |Select-Object -Property Instance, Version, Author, GenerationDate, GenerationHost | |
} | |
} | |
} | |
end | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment