使用powershell修改notes.ini

时间:2010-12-24 17:06:51

标签: powershell

我有一个powershell脚本,用于解析Lotus Notes INI文件并替换文件中的文本。但只有替换的文本显示在输出文件中。

# Example of PowerShell -replace parameter
## Get-DistinguishedName -- look up a DN from a user's (login) name 
function Get-DistinguishedName { 
Param($UserName)
   $ads = New-Object System.DirectoryServices.DirectorySearcher([ADSI]'')
   $ads.filter = "(&(objectClass=Person)(samAccountName=$UserName))"
   $s = $ads.FindOne()
   return $s.GetDirectoryEntry().DistinguishedName
}

clear-Host
set-executionpolicy remotesigned

$original_file = '.\notes.ini'
$destination_file =  '.\notes2.ini'
$OS = Get-WmiObject -Class win32_OperatingSystem -namespace "root\CIMV2" -ComputerName .
$username = [Environment]::UserName
$userprofile = $env:userprofile
$fullname =  Get-DistinguishedName($username) | %{$data = $_.split(","); $data[0].Substring(3)}
write-Host "Creating $userprofile"

if (($OS.Version -eq "5.1.2600") -or ($OS.Version -eq "5.2.3790")) {
   $lookupTable = @{
       '^SU_FILE_CLEANUP=' = 'SU_FILE_CLEANUP=' + $userprofile + '\Local Settongs\Application Data\smkits' 
       '%username%' = $username 
       '%fullname%' = $fullname 
       '%userprofile%' = $userprofile 
       '^Directory=' = 'Directory=' + $userprofile + '\Local Settongs\Application Data\Lotus\Notes\Data'}
} else {
   $lookupTable = @{
       'SU_FILE_CLEANUP=' = 'SU_FILE_CLEANUP=' + $userprofile + '\AppData\Roaming\smkits' 
       '%username%' = $username
       '%fullname%' = $fullname
       '%userprofile%' = $userprofile
       'Directory=' = 'Directory=' + $userprofile + '\AppData\Local\Lotus\Notes\Data'}
}

Get-Content -Path $original_file | ForEach-Object { 
    $line = $_
    $lookupTable.GetEnumerator() | ForEach-Object {
        if ($line -match $_.Key)
        {
            $line -replace $_.Key, $_.Value
            #break
        }
    }
    write-Host $line
} | Set-Content -Path $destination_file

我缺少什么

1 个答案:

答案 0 :(得分:1)

在这一行中,您正在将替换运算符的输出写入管道,然后由Set-Content

获取。
            $line -replace $_.Key, $_.Value

而在这一行中,您正在将输出写入主机(即powershell控制台),它不会最终出现在管道上并且不会被提取到Set-Content:

    write-Host $line

要解决此问题,只需将write-host替换为write-output:

    Write-Output $line