PowerShell:更改远程计算机上的桌面墙纸

时间:2015-10-01 11:42:41

标签: powershell wallpaper

我正在尝试修改PS脚本,但仍然无法弄清楚可能出现的问题。

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")

但我需要使用变量SID。 所以我制作了这个剧本:

$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")

我仍然收到这个错误:

  

您无法在空值表达式上调用方法。在    C:\ Users \“username”\ Scripty \ change_remote_wallpaper.ps1:32 char:17   + $ SubKey.SetValue&lt;&lt;&lt;&lt; ( “墙纸”, “d:\ pic1.jpg”)       + CategoryInfo:InvalidOperation:(SetValue:String)[],RuntimeException       + FullyQualifiedErrorId:InvokeMethodOnNull

任何想法可能出错?如果我将$ SubKey的值设置为“1”,那么我会收到:

  

“方法调用失败,因为 [System.Int32] 不包含   方法名为'SetValue'。“

所以我认为,问题在于“OpenSubKey”

感谢您的帮助!!

0 个答案:

没有答案
相关问题