Powershell脚本更改注册表

时间:2013-09-26 16:30:22

标签: powershell vbscript

我需要一些帮助。我正在尝试编写一个脚本,它将在HKU和HKCU键中搜索值“SMTP server”和“SMTP use auth”,然后用新值替换值。

到目前为止我的方式是

$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 Messaging Subsystem \ Profiles \ User@domain.com \ 9375CFF0413111d3B88A00104B2A6676 \ 00000001

HKCU:\ Software \ Microsoft \ Windows NT \ CurrentVersion \ Windows Messaging Subsystem \ Profiles \ User@domain.com \ 9375CFF0413111d3B88A00104B2A6676 \ 00000001

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

谢谢,

2 个答案:

答案 0 :(得分:0)

试试这个:

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

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

答案 1 :(得分:0)

尝试这样的事情:

$Fi = (Get-Content $dfile) -replace '^.*?::HKEY_USERS\\', 'HKCU:\'
相关问题