使用powershell修改notes.ini

发布于 2024-10-09 06:19:26 字数 1927 浏览 0 评论 0原文

我有一个 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

我缺少什么

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%' = $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

What am I missing

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绮烟 2024-10-16 06:19:27

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

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

而在这一行中,您将输出写入主机(即 powershell 控制台),它不会结束在管道上,不会被拾取 Set-Content:

    write-Host $line

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

    Write-Output $line

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文