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



PowerShell非常非常新

首先,我想给出我正在尝试做的场景。

我们有一个SharePoint网站,用户可以将他们的OneNote图书添加到这个SharePoint网站。SharePoint网站的URL现在正在更改。所以我将注册表项导出为文本文件,将路径的一部分更改为新路径,并将新的URL添加到注册表中。
我遇到的问题是,当我导出URL的(因为我出口的方式肯定)文本文件有一个名为"值"的字符串和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:backuponenote.log"
#Set Variables
$OneNoteBooks = "C:backuponenotenotebooks.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:SoftwareMicrosoftOffice14.0Onenoteopennotebooks' 
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:SOFTWAREMicrosoftWindowsCurrentVersion -Name     PowerShellPath -PropertyType String -Value $PSHome

# Stop Logging
Stop-Transcript

从哪里开始?在ForEach循环中创建一个具有两个属性的对象,然后使用Format-Table(别名FT使用),并选择两个属性中的一个,使整个对象毫无意义……相反,只需获取并输出您想要的值。

Get-Item . | Select-Object -ExpandProperty Property | ForEach-Object {Get-ItemProperty -Path . -Name $_).$_} | Out-File $OneNoteBooks

或者更好的方法是,使用get -Item中的Item的内置函数来获取和设置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:backuponenote.log"
#Set Variables
$OneNoteBooks = "C:backuponenotenotebooks.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:SoftwareMicrosoftOffice14.0Onenoteopennotebooks' 
$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:SoftwareMicrosoftOffice14.0Onenoteopennotebooks' -Name $_ -Value $NewValue
    [PSCustomObject][Ordered]@{
        "Value Name"=$_
        "Original Value"=$CurrentValue
        "Updated Value"=$NewValue
    }
} | FT -AutoSize | Out-File $OneNoteBooks
Pop-Location
# Stop Logging
Stop-Transcript

我也改变了你的RegEx模式为您的替换,因为旧的一个似乎不正确。例如,在HTTP://www.microsoft.com/powershell中,它将更新该字符串以读取http://www.microsoft.com//newsite.com/powershell,我不认为这是您的意图。

将数据保存到文件时,不使用Format命令。您可以在这里使用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文件,然后再导入,试试这个:

"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

问题出在这行代码中。

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

我建议删除格式表。如果它没有显示所有内容,那么试试这个:

"Value" = (Get-ItemProperty -Path . -Name $_).$_}} | Foreach-Object {$_.Value} | Out-File $OneNoteBooks

最新更新