Powershell 从文本文件中读取多行并将其添加到注册表

Powershell 从文本文件中读取多行并将其添加到注册表,powershell,Powershell,PowerShell非常新 首先,我想给出我要做的场景 我们有一个SharePoint网站,用户可以将其OneNote书籍添加到此SharePoint网站。SharePoint网站的URL现在正在更改。因此,我将注册表项导出到一个文本文件中,将部分路径更改为新路径,并将新URL添加到注册表中。 我遇到的问题是当我导出URL时,因为我导出的方式确保文本文件有一个名为Value的字符串,并且URL位于它下面 因此,第一个问题是如何将值写入一个文本文件中,在这种情况下,只有值被写入URL的 第二个问题

PowerShell非常新

首先,我想给出我要做的场景

我们有一个SharePoint网站,用户可以将其OneNote书籍添加到此SharePoint网站。SharePoint网站的URL现在正在更改。因此,我将注册表项导出到一个文本文件中,将部分路径更改为新路径,并将新URL添加到注册表中。 我遇到的问题是当我导出URL时,因为我导出的方式确保文本文件有一个名为Value的字符串,并且URL位于它下面

因此,第一个问题是如何将值写入一个文本文件中,在这种情况下,只有值被写入URL的 第二个问题是如何将这些更改后的值写回注册表? 每个URL都是一个新字符串,名称以1、2、3等开头

谢谢大家提前抽出时间

# Create a new folder if not exist file
$Folder = "C:\backup"
if(-not(Test-Path $Folder)){
New-Item -Path $Folder -ItemType Directory
}

# Start Logging
Start-Transcript -Path "C:\backup\onenote.log"

#Set Variables

$OneNoteBooks = "C:\backup\onenotenotebooks.txt"
$NewPath = '//newpath.com/'


# Delete the existing file if exists
If (Test-Path $OneNoteBooks){Remove-Item $OneNoteBooks}

# Create a new text file
New-Item -Path $OneNoteBooks -ItemType File

# Exporting OneNote SharePoint Notebooks and Correcting them to the new URL
Write-Host "Exporting OneNote SharePoint Notebooks and Correcting them to the new URL"

Push-Location
Set-Location 'HKCU:\Software\Microsoft\Office\14.0\Onenote\opennotebooks' 

Get-Item . | Select-Object -ExpandProperty Property | ForEach-Object {

New-Object PSobject -Property @{"property"=$_;

"Value" = (Get-ItemProperty -Path . -Name $_).$_}} | Format-Table Value -AutoSize |     Out-File $OneNoteBooks

Pop-Location

$ReplaceURL = Get-Content -Path $OneNoteBooks
ForEach-Object{
$ReplaceURL -replace "`//.*?(`/)", $NewPath | Out-File $OneNoteBooks

}


# Add Changed URL's to the registry

ForEach-Object {
New-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion -Name     PowerShellPath -PropertyType String -Value $PSHome


# Stop Logging
Stop-Transcript

问题出在这行代码中

"Value" = (Get-ItemProperty -Path . -Name $_).$_}} | Format-Table Value -AutoSize | Out-File $OneNoteBooks
我建议删除格式表。如果它没有向您展示所有内容,请尝试以下方法:

"Value" = (Get-ItemProperty -Path . -Name $_).$_}} | Foreach-Object {$_.Value} | Out-File $OneNoteBooks
"Windows Registry Editor Version 5.00`n" > c:\onenotebooks.reg
"[$($pwd.ProviderPath)]" >> c:\onenotebooks.reg
Get-Item . | Select-Object -Exp Property | 
    ForEach-Object { "`"$_`"=`"$((Get-ItemProperty . -Name $_).$_)`"" } >> c:\onenotebooks.reg

从哪里开始?在ForEach循环中,您创建了一个具有两个属性的对象,然后使用Format Table alias FT used,并从这两个属性中选择一个,使整个对象无意义。。。相反,只需获取并输出所需的值

Get-Item . | Select-Object -ExpandProperty Property | ForEach-Object {Get-ItemProperty -Path . -Name $_).$_} | Out-File $OneNoteBooks
或者更好的方法是,使用Get项的内置函数在ForEach循环中获取和设置值。试试这个

# Create a new folder if not exist file
$Folder = "C:\backup"
if(-not(Test-Path $Folder)){
New-Item -Path $Folder -ItemType Directory
}

# Start Logging
Start-Transcript -Path "C:\backup\onenote.log"

#Set Variables

$OneNoteBooks = "C:\backup\onenotenotebooks.txt"
$NewPath = '//newpath.com/'
$OldPath = '//oldpath.com/'
$SitesChangingMask = "$([regex]::escape("$oldpath"))(nibremv|finance|sites/procurement|Comm|Departments/NITAS|ro|nitd|cnibr|communities/sig)/?"

# Delete the existing file if exists
If (Test-Path $OneNoteBooks){Remove-Item $OneNoteBooks}

# Create a new text file
New-Item -Path $OneNoteBooks -ItemType File

# Exporting OneNote SharePoint Notebooks and Correcting them to the new URL
"Exporting OneNote SharePoint Notebooks and Correcting them to the new URL"

Push-Location
Set-Location 'HKCU:\Software\Microsoft\Office\14.0\Onenote\opennotebooks' 

$OneNoteReg = Get-Item . 
$OneNoteReg.Property | ForEach-Object {
    $CurrentValue = $OneNoteReg.GetValue($_)
    $NewValue = if($CurrentValue.ToString() -match $SitesChangingMask){$CurrentValue.ToString() -replace "$OldPath", $NewPath}else{$CurrentValue.ToString()}
    Set-ItemProperty 'HKCU:\Software\Microsoft\Office\14.0\Onenote\opennotebooks' -Name $_ -Value $NewValue
    [PSCustomObject][Ordered]@{
        "Value Name"=$_
        "Original Value"=$CurrentValue
        "Updated Value"=$NewValue
    }
} | FT -AutoSize | Out-File $OneNoteBooks

Pop-Location

# Stop Logging
Stop-Transcript

我也改变了你的正则表达式模式,因为旧的似乎不正确。例如,在HTTP://www.microsoft.com/powershell中,它会将该字符串更新为http://www.microsoft.com//newsite.com/powershell 我认为这不是您的意图。

将数据保存到文件时,请跳过使用格式化命令。您可以在此处使用CSV文件:

Get-Item . | Select-Object -Exp Property | ForEach-Object {
  New-Object PSCustomObject -Property @{"Property"=$_;"Value"=(Get-ItemProperty . -Name $_).$_}} |
Export-Csv $OneNoteBooks
修改文件,然后像这样重新读取:

$props = Import-Csv $OneNoteBooks
然后可以访问属性名称和值,如下所示:

$props.Property[0]
$props.Value[0]
..
当然,您实际上不需要转到文件来修改值。您可以在内存中执行此操作:

$props = Get-Item . | Select-Object -Exp Property | ForEach-Object {
  New-Object PSCustomObject -Property @{"Property"=$_;"Value"=(Get-ItemProperty . -Name $_).$_}}

$props = $props | Foreach {$_.Value = $_.Value -replace 'pattern','replacement'}
如果您希望创建一个可导入的.reg文件,请尝试以下操作:

"Value" = (Get-ItemProperty -Path . -Name $_).$_}} | Foreach-Object {$_.Value} | Out-File $OneNoteBooks
"Windows Registry Editor Version 5.00`n" > c:\onenotebooks.reg
"[$($pwd.ProviderPath)]" >> c:\onenotebooks.reg
Get-Item . | Select-Object -Exp Property | 
    ForEach-Object { "`"$_`"=`"$((Get-ItemProperty . -Name $_).$_)`"" } >> c:\onenotebooks.reg

嗨,基思。谢谢你的回复。这确实帮了大忙。虽然我更愿意使用文本文件,但如果CSV文件是我唯一的选择,我总是可以使用它。然而您提供的代码不起作用。它甚至没有将其转换为CSV。有没有可能只有像1这样的东西。根据URL的行数,每行上有2行,以此类推?我假设将其导入注册表会更容易。我也需要帮助。再次感谢。您使用的是哪个版本的PowerShell?我在V4上对此进行了测试,它正确地创建了csv,并正确地将其导入。请参阅更新的答案,以创建可以使用reg.exe导入的.reg文件。我想这就是原因。我们公司仍在版本3上。感谢您的帮助和努力。您好,技术员。不要再让你生气了:但是代码不起作用。我在尝试时遇到访问被拒绝错误。我尝试直接通过注册表和powershell添加一个值,我能够。所以我觉得在代码的某个地方遗漏了什么?我更改URL的部分正在处理我的代码。我真的很喜欢你对旧价值和新价值的看法。我很想让它发挥作用,但最后我必须将它们导入注册表。你能帮我吗?提前感谢。更新和测试。这应该对你有用了。我将.SetValue改为使用Set ItemProperty.Hi themmad。您能否告诉我如何修改此脚本,使其仅在看到oldpath.path.intra时才会更改?这应该可以做到。添加了$OldPath变量,并将-Replace更改为使用该变量。这就像一个符咒。除了一个我忘了提的问题。只有以下站点正在从旧路径更改为新路径。因此,脚本只有在看到这些路径时才需要更改。你不知道我多么感激你的帮助。这正是我要找的。非常感谢。但是现在,根据URL的行数,如何将名称为1、2、3等的每一行导入注册表?提前谢谢。