Change Desktop Background color Remotely

oldrich Bukac 21 Reputation points
2020-11-24T09:37:42.727+00:00

Hi , I will change Desktop background color on remote computer , I have script , to change color immediately on local machine , when aj run this script Remotely No effect, can help?

$ComputerName = "PC170774"  
  
Invoke-Command -ComputerName $ComputerName -ScriptBlock {   
param(  
[Parameter(Position=0)]  
$R=0,  
[Parameter(Position=1)]  
$G=0,  
[Parameter(Position=2)]  
$B=0  
)  
  
$code = @'   
 using System;   
 using System.Drawing;   
 using System.Runtime.InteropServices;   
 using Microsoft.Win32;   
    
    
 namespace CurrentUser   
 {   
     public class Desktop   
     {   
         [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]   
         private static extern int SystemParametersInfo(int uAction, int uParm, string lpvParam, int fuWinIni);   
         [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]   
         private static extern int SetSysColors(int cElements, int[] lpaElements, int[] lpRgbValues);   
         public const int UpdateIniFile = 0x01;   
         public const int SendWinIniChange = 0x02;   
         public const int SetDesktopBackground = 0x0014;   
         public const int COLOR_DESKTOP = 1;   
         public int[] first = {COLOR_DESKTOP};   
    
    
         public static void RemoveWallPaper()   
         {   
             SystemParametersInfo( SetDesktopBackground, 0, "", SendWinIniChange | UpdateIniFile );   
             RegistryKey regkey = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);   
             regkey.SetValue(@"WallPaper", 0);   
             regkey.Close();   
         }   
    
         public static void SetBackground(byte r, byte g, byte b)   
         {   
             int[] elements = {COLOR_DESKTOP};   
    
             RemoveWallPaper();   
             System.Drawing.Color color = System.Drawing.Color.FromArgb(r,g,b);   
             int[] colors = { System.Drawing.ColorTranslator.ToWin32(color) };   
    
             SetSysColors(elements.Length, elements, colors);   
             RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Colors", true);   
             key.SetValue(@"Background", string.Format("{0} {1} {2}", color.R, color.G, color.B));   
             key.Close();   
         }   
     }   
 }   
    
'@  
try  
{  
    Add-Type -TypeDefinition $code -ReferencedAssemblies System.Drawing.dll   
}catch{  
    # An error is thrown if the type [CurrentUser.Desktop] is already created  
    # so we ignore it.  
}  
finally  
{  
    [CurrentUser.Desktop]::SetBackground($R, $G, $B)  
}  
  
}  

++++++++++++++++++++++++
a try copy this script in 1.ps1 file to remote computer and run :

$RemoteScriptPath = "C:\ps1"  
Invoke-Command -ComputerName $Computername -ScriptBlock { Start-Process powershell.exe -ArgumentList $using:RemoteScriptPath -Wait }  

++++++++++++++++++++++++++

with No effect,,,

Can change Background color on remote machine to see effect immediately(no need logoff or restart)?

Windows 10
Windows 10
A Microsoft operating system that runs on personal computers and tablets.
10,583 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,356 questions
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 95,496 Reputation points MVP
    2020-11-25T09:51:12.227+00:00

    Could it be the remote script is running interactive without a "current user context/profile"?

    This would explain, why the script is working if started locally in a user context and not working if invoked remote.


    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Ian Xue (Shanghai Wicresoft Co., Ltd.) 29,486 Reputation points Microsoft Vendor
    2020-11-25T07:23:39.913+00:00

    Hi,

    Have you tried to login to the remote server and run the script? Please also see if this works.

    Invoke-Command -FilePath $ScriptPath  -ComputerName $Computername  
    

    Best Regards,
    Ian

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. oldrich Bukac 21 Reputation points
    2020-11-25T09:15:26.633+00:00

    Yes I try it, no effect ,

    when run script localy works fine, BG change immediately.

    0 comments No comments

  3. Sebastien Vinchon 1 Reputation point
    2021-03-04T11:57:51.877+00:00

    Did you find a solution?
    I have the same issue (almost).
    I am using a java agent to trigger the powershell script call remotely.
    I does not fail but does not produce the expected result either.
    The same exact script work perfectly when ran from within the target machine manually.

    0 comments No comments