如何使用替代凭据来编写注册表项?
How can I use alternative credentials to write a registry key?
我正在编写的脚本需要查询注册表项(完成),通过代理连接到网络服务(完成),然后在 HKLM 中设置注册表项。
我们正在尝试修改脚本以在注册表中存储戴尔机器的保修结束日期。该脚本需要通过我们的代理,偶然我注意到 PowerShell 默认遵守 IE 代理设置(甚至使用 PAC 文件),这太棒了!不幸的是,我们还有一个要求——脚本必须写入 HKLM,只有管理员才能做到。
我尝试根据 technet 上的信息使用 New-Item
cmdlet 和 -Credential
,但没有成功。
The provider does not support the use of credentials. Perform the
operation again without specifying credentials."
我发现 this thread,它建议使用凭据映射一个新的 PSDrive。但是,这会引发相同的错误。
在 PowerShell 中,我可以使用其他凭据写入 HKLM 吗?
如果你有 PS Remoting enabled on the host, you can run a command via Invoke-Command
:
$key = 'HKLM:\Some\Subkey'
$value = 'ValueName'
$type = 'String'
$data = 'foobar'
$cred = Get-Credential
Invoke-Command -Computer $env:COMPUTERNAME -ScriptBlock {
New-ItemProperty -Path $args[0] -Name $args[1] -Type $args[2] -Value $args[3]
} -ArgumentList $key, $value, $type, $data -Credential $cred
我们将以不同的方式解决这个问题,使用 GPO 授予用户对我要写入的密钥的权限。
这似乎是 Powershell 中的一个奇怪的缺点。
试试这个
$key = 'HKLM:\SOFTWARE\WOW6432Node\Citrix\Ica\Session'
$value = 'ClientAddress'
$type = 'String'
$data = '10.11.19.35'
$secpasswd = “your-password” | ConvertTo-SecureString -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential (“domain\your-username”, $secpasswd)
$cmd = "New-ItemProperty -Path $key -Name $value -Type $type -Value $data"
$pinfo = New-Object System.Diagnostics.ProcessStartInfo
$pinfo.FileName = "C:\Windows\System32\WindowsPowerShell\v1.0\Powershell.exe"
$pinfo.RedirectStandardError = $true
$pinfo.RedirectStandardOutput = $true
$pinfo.UseShellExecute = $false
$pinfo.Arguments = "-Command $cmd"
$pinfo.Username = $credential.username
$pinfo.Password = $credential.password
$p = New-Object System.Diagnostics.Process
$p.StartInfo = $pinfo
$p.Start() | Out-Null
$p.WaitForExit()
$stdout = $p.StandardOutput.ReadToEnd()
$stderr = $p.StandardError.ReadToEnd()
Write-Host "stdout: $stdout"
Write-Host "stderr: $stderr"
Write-Host "exit code: " + $p.ExitCode
我正在编写的脚本需要查询注册表项(完成),通过代理连接到网络服务(完成),然后在 HKLM 中设置注册表项。
我们正在尝试修改脚本以在注册表中存储戴尔机器的保修结束日期。该脚本需要通过我们的代理,偶然我注意到 PowerShell 默认遵守 IE 代理设置(甚至使用 PAC 文件),这太棒了!不幸的是,我们还有一个要求——脚本必须写入 HKLM,只有管理员才能做到。
我尝试根据 technet 上的信息使用 New-Item
cmdlet 和 -Credential
,但没有成功。
The provider does not support the use of credentials. Perform the operation again without specifying credentials."
我发现 this thread,它建议使用凭据映射一个新的 PSDrive。但是,这会引发相同的错误。
在 PowerShell 中,我可以使用其他凭据写入 HKLM 吗?
如果你有 PS Remoting enabled on the host, you can run a command via Invoke-Command
:
$key = 'HKLM:\Some\Subkey'
$value = 'ValueName'
$type = 'String'
$data = 'foobar'
$cred = Get-Credential
Invoke-Command -Computer $env:COMPUTERNAME -ScriptBlock {
New-ItemProperty -Path $args[0] -Name $args[1] -Type $args[2] -Value $args[3]
} -ArgumentList $key, $value, $type, $data -Credential $cred
我们将以不同的方式解决这个问题,使用 GPO 授予用户对我要写入的密钥的权限。
这似乎是 Powershell 中的一个奇怪的缺点。
试试这个
$key = 'HKLM:\SOFTWARE\WOW6432Node\Citrix\Ica\Session'
$value = 'ClientAddress'
$type = 'String'
$data = '10.11.19.35'
$secpasswd = “your-password” | ConvertTo-SecureString -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential (“domain\your-username”, $secpasswd)
$cmd = "New-ItemProperty -Path $key -Name $value -Type $type -Value $data"
$pinfo = New-Object System.Diagnostics.ProcessStartInfo
$pinfo.FileName = "C:\Windows\System32\WindowsPowerShell\v1.0\Powershell.exe"
$pinfo.RedirectStandardError = $true
$pinfo.RedirectStandardOutput = $true
$pinfo.UseShellExecute = $false
$pinfo.Arguments = "-Command $cmd"
$pinfo.Username = $credential.username
$pinfo.Password = $credential.password
$p = New-Object System.Diagnostics.Process
$p.StartInfo = $pinfo
$p.Start() | Out-Null
$p.WaitForExit()
$stdout = $p.StandardOutput.ReadToEnd()
$stderr = $p.StandardError.ReadToEnd()
Write-Host "stdout: $stdout"
Write-Host "stderr: $stderr"
Write-Host "exit code: " + $p.ExitCode