#To define a particular parameter, replace the 'parameterName' inside itsm.getParameter('parameterName') with that parameter's name
import os

fromURL=itsm.getParameter('BackgroundImageUrl') ## Here mention the download url

Down_path=os.environ['PROGRAMDATA']
fileName = fromURL.split('/')[-1]
DownTo = os.path.join(Down_path, fileName)

def ecmd(command):
    import ctypes
    from subprocess import PIPE, Popen
    
    class disable_file_system_redirection:
        _disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection
        _revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection
        def __enter__(self):
            self.old_value = ctypes.c_long()
            self.success = self._disable(ctypes.byref(self.old_value))
        def __exit__(self, type, value, traceback):
            if self.success:
                self._revert(self.old_value)
    
    with disable_file_system_redirection():
        obj = Popen(command, shell = True, stdout = PIPE, stderr = PIPE)
    out, err = obj.communicate()
    ret=obj.returncode
    if ret==0:
        if out:
            return out.strip()
        else:
            return ret
    else:
        if err:
            return err.strip()
        else:
            return ret
		
import ctypes
class disable_file_system_redirection:
    _disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection
    _revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection
    def __enter__(self):
        self.old_value = ctypes.c_long()
        self.success = self._disable(ctypes.byref(self.old_value))
    def __exit__(self, type, value, traceback):
        if self.success:
            self._revert(self.old_value)

import subprocess        
with disable_file_system_redirection():
    import urllib
	
def downloadFile(DownTo, fromURL):
    try:
        with open(DownTo, 'wb') as f:
            f.write(urllib.urlopen(fromURL).read())
        if os.path.isfile(DownTo):
            return '{} - {}KB'.format(DownTo, os.path.getsize(DownTo)/1000)
		
    except:
        return 'Please Check URL or Download Path!'
	
print downloadFile(DownTo, fromURL )

import _winreg
Key= r"SOFTWARE\Policies\Microsoft\Windows\Personalization"
Sub_Key= "LockScreenImage"
Field= _winreg.REG_SZ
value = DownTo

from _winreg import *
try:
    Key=r"SOFTWARE\Policies\Microsoft\Windows\Personalization"
    if not os.path.exists(Key):
        key = _winreg.CreateKey(HKEY_LOCAL_MACHINE,Key)
    Registrykey= OpenKey(HKEY_LOCAL_MACHINE,Key, 0,KEY_WRITE)
    _winreg.SetValueEx(Registrykey,Sub_Key,0,Field,value)
    CloseKey(Registrykey)
    print "Success"
except WindowsError:
    print "Error"

try:
    Key=r"SOFTWARE\Policies\Microsoft\Windows\PersonalizationCSP"
    if not os.path.exists(Key):
        key = _winreg.CreateKey(HKEY_LOCAL_MACHINE,Key)
    Registrykey= OpenKey(HKEY_LOCAL_MACHINE,Key, 0,KEY_WRITE)
    _winreg.SetValueEx(Registrykey,"LockScreenImagePath",0,Field,value)
    CloseKey(Registrykey)
    print "Success"
except WindowsError:
    print "Error"
	
try:
    Key=r"SOFTWARE\Policies\Microsoft\Windows\PersonalizationCSP"
    if not os.path.exists(Key):
        key = _winreg.CreateKey(HKEY_LOCAL_MACHINE,Key)
    Registrykey= OpenKey(HKEY_LOCAL_MACHINE,Key, 0,KEY_WRITE)
    _winreg.SetValueEx(Registrykey,"LockScreenImageUrl",0,Field,value)
    CloseKey(Registrykey)
    print "Success"
except WindowsError:
    print "Error"
	
try:
    Key=r"SOFTWARE\Policies\Microsoft\Windows\PersonalizationCSP"
    if not os.path.exists(Key):
        key = _winreg.CreateKey(HKEY_LOCAL_MACHINE,Key)
    Registrykey= OpenKey(HKEY_LOCAL_MACHINE,Key, 0,KEY_WRITE)
    _winreg.SetValueEx(Registrykey,"LockScreenImageStatus",0,_winreg.REG_DWORD,1)
    CloseKey(Registrykey)
    print "Success"
except WindowsError:
    print "Error"
