Note
1. Run as Logged in User
2. Provide the image url in the parameter 'ImgURL' which should be downloaded and set as desktop background. the datatype should be a string.
example: https://codetheweb.blog/assets/img/icon1.png
import os
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)
Download_path = os.environ['TEMP'] + r'\Temp.png'
url = itsm.getParameter('ImgURL') ## Provide an image URL which you want to set as desktop background. the datatype should be a string.
# url = "https://www.comodo.com/new-assets/images/comodo-cybersecurity-managed-detection-and-response.png"
Dwn_content = r'''
$url = "''' + url + '''"
$wc = New-Object System.Net.WebClient
$wc.DownloadFile($url, "''' + Download_path + '''")
'''
def ecmd(command):
with disable_file_system_redirection():
obj = Popen(command, shell=True, stdout=PIPE, stderr=PIPE)
out, err = obj.communicate()
return err
def RunPowerShellScript(ps_content, Scriptfile):
file_name = Scriptfile
file_path = os.path.join(os.environ['TEMP'], file_name)
with open(file_path, 'wb') as wr:
wr.write(ps_content)
try:
print ecmd('powershell "%s"' % file_path)
os.remove(file_path)
except:
return "Script execution failed"
RunPowerShellScript(Dwn_content, 'Down_file.ps1')
if os.path.exists(Download_path):
print "Downloaded successfully to " + Download_path
if ctypes.windll.user32.SystemParametersInfoA(20, 0, Download_path, 3):
print "Script executed successfully"
else:
"Script execution failed"
os.remove(Download_path)
else:
print "Download Failed"
Comments