首页 文章

Powershell脚本更改注册表

提问于
浏览
0

我需要一些帮助 . 我正在尝试编写一个脚本,它将在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

    试试这个:

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

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

  • 0

    尝试这样的事情:

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

相关问题