开发者

Using powershell to modify notes.ini

I have a powershell script that parses a lotus notes INI file and replaces text inside the file. But only the replaced text is showing up in the output file.

# 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%' = $us开发者_如何转开发ername 
       '%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

What am I missing


On this line, you are writing he output of the replace operator onto the pipeline, this will then get picked up by Set-Content

            $line -replace $_.Key, $_.Value

whereas on this line, you are writing the output to the host (i.e. the powershell console) it will not end up on the pipeline and will not get picked up up Set-Content:

    write-Host $line

To fix this, just replace write-host with write-output:

    Write-Output $line
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜