0

我需要一点帮助。我正在尝试编写一个脚本,它将在 HKCU 和 HKCU 键中搜索值“SMTP 服务器”和“SMTP 使用身份验证”,然后用新值替换这些值。

到目前为止,我这样做的方式是

$null = New-PSDrive -Name HKU   -PSProvider Registry -Root Registry::HKEY_USERS
#Enter the string value to search for in the variable below. 
$SearchString = "SMTP Server"
#==================================================================================
# Main Code for HKU:
# Write a message to the user to let them know the script 
# has started. 
Write-Host "Searching: HKU" 
# Search the registery for the string value.  Store 
#Registry Keys in out-file
Get-ChildItem HKU:\ -Recurse -ErrorAction SilentlyContinue |   
    ForEach-Object {   
  if((get-itemproperty -Path $_.PsPath) -match $searchString)  
  {   
   $_.PsPath  
  }   
} | out-File HKU_email.txt
# Write a message to let the user know the script completed. 
Write-Host "Part1 has completed."
# == End of Main Code =======================================
# Run VBS file to modify text files
Start-Sleep -Seconds 2
cscript.exe HKU.vbs
Start-Sleep -Seconds 2
# Run Through the file and set the values below in the registry.
$Dfile = "HKU_Email.txt"
$Fi = Get-Content $dfile
foreach ($data in $Fi) {Set-ItemProperty -Path $data -Name "SMTP Server" -Value [Byte[]](<i>New SMTPSERVER<i>)) -type Binary}
foreach ($data in $Fi) {Set-ItemProperty -Path $data -Name "SMTP Use Auth" -Value "0" -type Dword}
Write-Host "Changes in Registry for HKU Completed"

VBscript HKU.vbs 解析文件以使行从

Microsoft.PowerShell.Core\Registry::HKEY_USERS\Software\Microsoft\Windows NT\CurrentVersion\Windows 消息子系统\Profiles\User@domain.com\9375CFF0413111d3B88A00104B2A6676\00000001

HKCU:\Software\Microsoft\Windows NT\CurrentVersion\Windows 消息子系统\Profiles\User@domain.com\9375CFF0413111d3B88A00104B2A6676\00000001

我的问题是,有没有办法用 powershell 完成我在 VBscript 中所做的事情?我必须在从 windows xp 到 win7 32/64bit 的大约 100 台机器上运行它。

谢谢,

4

2 回答 2

0

尝试这样的事情:

$Fi = (Get-Content $dfile) -replace '^.*?::HKEY_USERS\\', 'HKCU:\'
于 2013-09-26T21:53:54.757 回答
0

尝试这个:

$a,$b,$path = $_.pspath -split ':'
$newpath = $path -replace 'HKEY_USERS(.*)','HKCU:$1'

这使用了 PowerShell V3 (-split) 的新功能。

于 2013-09-26T18:07:55.497 回答