Skip to content

Instantly share code, notes, and snippets.

@AutomationD
Created October 15, 2014 18:07
Show Gist options
  • Save AutomationD/716d1d3640df7572f4b3 to your computer and use it in GitHub Desktop.
Save AutomationD/716d1d3640df7572f4b3 to your computer and use it in GitHub Desktop.
registry_posh
Puppet::Type.type(:registry_key_posh).provide(:registry_key_posh) do
@doc = %q{Manage registry entry via powershell}
desc "Manage windows users and groups"
confine :operatingsystem => :windows
defaultfor :operatingsystem => :windows
commands :powershell =>
if File.exists?("#{ENV['SYSTEMROOT']}\\sysnative\\WindowsPowershell\\v1.0\\powershell.exe")
"#{ENV['SYSTEMROOT']}\\sysnative\\WindowsPowershell\\v1.0\\powershell.exe"
elsif File.exists?("#{ENV['SYSTEMROOT']}\\system32\\WindowsPowershell\\v1.0\\powershell.exe")
"#{ENV['SYSTEMROOT']}\\system32\\WindowsPowershell\\v1.0\\powershell.exe"
else
'powershell.exe'
end
def exists?
key = resource[:key]
path = resource[:path]+'\\\\'
value = resource[:value]
type = resource[:type]
#add transform to array if just a string
found = false
Puppet.debug("Checking the existence of key: #{self} in #{path}")
#result = powershell 'if (([ADSI]"WinNT://$env:computername/'+group+'").IsMember(([ADSI]"WinNT://$env:userdomain/'+ resource[:name]+'").ADsPath) -eq $False){echo 0} else {echo 1}'
#result = powershell 'if (Test-Path \''+path+'\'){if ($($(Get-Item \''+path+'\').Property) -contains \''+key+'\'){echo 1}else{echo 0}}else{echo 2}'
result = powershell 'if (Test-Path "'+path+'"){if ($($(Get-Item "'+path+'").Property) -contains "'+key+'"){echo 1}else{echo 0}}else{echo 2}'
Puppet.debug("Value is "+result)
if result.chomp == "0" #if not found (ps returned false on search of user in group)
Puppet.debug("Key '#{resource[:key]}' is not found in '#{path}'. Value not checked")
#####TODO: check if value of the key is right
found = false #
elsif result.chomp == "1"
Puppet.debug("Key '#{resource[:key]}' is found in a path '#{path}. Value not checked'")
found = true
else
Puppet.debug("#{path} does not exits")
found = false
end
found
end
def create
key = resource[:key]
path = resource[:path]+'\\\\'
value = resource[:value]
type = resource[:type]
Puppet.debug("Adding #{key} to #{path}")
#powershell 'New-ItemProperty "'+path+'" -Name "'+key+'" -Value "'+value+'" -PropertyType "'+type+'"'
powershell 'New-ItemProperty "'+path+'" -Name "'+key+'" -Value "'+value+'" -PropertyType "'+type+'"'
end
def destroy
key = resource[:key]
path = resource[:path]
Puppet.debug("Removing #{key} from #{path}")
#powershell 'New-ItemProperty "'+path+'" -Name "'+key+'" -Value "'+value+'" -PropertyType "'+type+'"'
powershell 'Remove-Item -Path \''+path+'\' -Recurse'
end
end
Puppet::Type.newtype(:registry_key_posh) do
@doc = %q{Manage registry key - powershell wrapper}
ensurable do
desc "Manage windows users and groups"
defaultvalues
newvalue(:present) do
provider.create
end
newvalue(:absent) do
provider.destroy
end
end
newparam(:key, :namevar=>true) do
desc "Key name"
end
newparam(:path) do
desc "Registy path"
end
newparam(:value) do
desc "Value"
end
newparam(:type) do
desc "Groups"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment