[python-win32] _winreg.SaveKey question

Roger Upole rwupole at msn.com
Thu Oct 26 20:01:29 CEST 2006


Rodney McBride wrote:
>I am getting an access denied error when calling _winreg.SaveKey and I
> believe that the error is being caused by not having the SeBackupPrivilege
> set when calling the _winreg.SaveKey method for a remote host (I am
> attempting to export a registry key from a remote host to a file for backup
> purposes). My question is how do I set the 'SeBackupPrivilege'?
> 
> Here's a copy of my source code:
> 
> ##################################
> import os, string, sys, win32con, _winreg
> 
> def createFolder(path):
>    if os.path.exists (path):
>        pass    # do nothing
>    else:
>        os.mkdir(path)
> 
> def remoteSaveKey(host, subkey, file):
>    rhostreg = _winreg.ConnectRegistry(host, _winreg.HKEY_LOCAL_MACHINE)
>    remotekey = _winreg.OpenKey(rhostreg, subkey, 0, _winreg.KEY_ALL_ACCESS)
> 
>    _winreg.SaveKey(remotekey, file)
> 
> destsrvr = "\\\\server1\\backup$\\"
> # Registry key (and all subkeys) to be processed for backup/restore
> regpath = "Software\\Cisco Systems, Inc."
> 
> # Open hostfile and read hostnames into a list
> readfile = open('d:\scripts\dev\SideAsrv.txt', 'r')
> hostfile = []
> for host in readfile:
>    hostfile += [host.strip()]
> 
> readfile.close ()
> 
> for srvname in hostfile:
>    outputpath = destsrvr + srvname
>    createFolder(outputpath)
>    outputfile = outputpath + "\\" + srvname
>    srvname = "\\\\" + srvname
>    # Call the function to connect to and export the remote registry key
>    output = remoteSaveKey(srvname, regpath, outputfile)
>    # DEBUG
>    print outputfile
>    # Select the next host
> #####################
> 

If the account that you're running under doesn't possess SeBackupPrivilege,
you can use win32security.LsaAddAccountRights to grant it.  However, even
if you already have the privilege it needs to be enabled using AdjustTokenPrivileges.
See \Lib\site-packages\win32\Demos\security\set_file_audit.py for an example.

          Roger





More information about the Python-win32 mailing list