0

I'm trying to modify PS script, but still cant figureout what can be wrong.

Original script:

cp "image.jpg" \\<IP>\C$
$RemoteReg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('Users', "<IP>")
$WallPaper= $RemoteReg.OpenSubKey("S-1-5-21-780093305-1579027723-3042286928-500\Control Panel\Desktop",$True)
$WallPaper.SetValue("Wallpaper","C:\image.jpg")

But I need to use variable SIDs. So I've made this script:

$hostname = Read-Host 'enter hostname of station'

$username = Read-Host 'enter username (login)'

$objUser = New-Object System.Security.Principal.NTAccount($username)

$userSID = $objUser.Translate([System.Security.Principal.SecurityIdentifier])

$SIDandPath = Join-Path $userSID "Control Panel\Desktop"

cp d:\pic1.jpg \\$hostname\c$

$BaseKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey( 'Users' , $hostname )

$SubKey = $BaseKey.OpenSubKey($SIDandPath,$true)

$SubKey.SetValue("Wallpaper","d:\pic1.jpg")

And I'm still receiving thi error:

You cannot call a method on a null-valued expression. At C:\Users\"username"\Scripty\change_remote_wallpaper.ps1:32 char:17 + $SubKey.SetValue <<<< ("Wallpaper","d:\pic1.jpg") + CategoryInfo : InvalidOperation: (SetValue:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull

Any ideas what can be wrong? If I set the value of $SubKey to "1", then I receive this:

"Method invocation failed because [System.Int32] doesn't contain a method named 'SetValue'."

So I think, that the problem is with "OpenSubKey"

Thanks for any help!!

al'ein
  • 1,711
  • 1
  • 14
  • 21
  • Sounds like `$BaseKey.OpenSubKey($SIDandPath,$true)` is returning null. Can you confirm that the users hive is loaded and that the remote registry service is running? Service must be running for you to get that far. Is the user signed in though? Also see that you need to change the path for the key to be `c:\pic1.jpg` – Matt Oct 01 '15 at 12:03
  • Yes, _$BaseKey.OpenSubKey($SIDandPath,$true)_ is returning null, but in another script it works. So it means, that remote registry service is running. And d:\pic1.jpg is current location of picture in my PC - I've also checked if the picture was copied to new location and it is ok. So I don't know why _$BaseKey.OpenSubKey($SIDandPath,$true)_ is not working in this case. – InLoveWithTux Oct 02 '15 at 21:51

0 Answers0