I tried editing a few of the creation of shortcuts on the desktop procedures I found but was unsuccessful. I am in need of a procedure that would check every users desktop for a URL shortcut link by a specific name and delete it.
Please cancel request. I was able to write it myself. Please see below for my example. My organization uses OneDrive so that made it an interesting variable. You will need to update my “list” to match your possible OneDrive formats. I removed my org for confidentiality.
iconname = itsm.getParameter('Shortcut_Name') # provide the shortcut icon name as Shortcut_Name parameter eg. comodo
username = itsm.getParameter('Username') # provide the username parameter to run the script with elevated permissions
password = itsm.getParameter('Password') # provide the password parameter corresponding to the username
import os
import ctypes
import subprocess
from subprocess import PIPE, Popen
def ecmd(command):
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
def run_command_with_elevated_permissions(command):
encoded_command = 'powershell -Command "{}"'.format(command)
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
process = subprocess.Popen(['runas', '/user:{}'.format(username), encoded_command], startupinfo=startupinfo, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
process.stdin.write(password.encode('utf-8'))
process.stdin.close()
output, error = process.communicate()
return output, error, process.returncode
found = False
# Define the list of OneDrive folder names to check
onedrive_folders = ['OneDrive', 'OneDrive - ORG NAME', 'OneDrive - ORG NAME VARIATION']
# Iterate through each user folder
for user_folder in os.listdir('C:\\Users'):
for onedrive_folder in onedrive_folders:
user_onedrive_path = os.path.join('C:\\Users', user_folder, onedrive_folder)
# Check if the user folder has the specified OneDrive folder
if not os.path.isdir(user_onedrive_path):
continue
desktop_folder_path = os.path.join(user_onedrive_path, 'Desktop')
# Check if the Desktop folder exists within the OneDrive folder
if not os.path.isdir(desktop_folder_path):
continue
# Print the list of files in the user's desktop folder
print('Files in the desktop folder for user {}:'.format(user_folder))
for file in os.listdir(desktop_folder_path):
print(file)
# Check for the internet shortcut file
for file in os.listdir(desktop_folder_path):
if file.lower().endswith('.url') and iconname.lower() in file.lower():
icon_path = os.path.join(desktop_folder_path, file)
print('Deleting: {}'.format(icon_path)) # Debug information
try:
os.remove(icon_path)
found = True
print('The Quick Icon "{}" has been deleted'.format(iconname))
except Exception as e:
print('Error: {}'.format(str(e)))
if not found:
print('The Quick Icon "{}" was not found on any user desktops.'.format(iconname))
print('Desktop icon deletion procedure completed.')
2 Likes
Can we consider using a logging framework to record the script’s activities and any errors encountered during execution? Instead of relying solely on print statements for displaying information.?