【问题标题】:Change the contents of a rdp-file with PowerShell使用 PowerShell 更改 rdp 文件的内容
【发布时间】:2018-12-15 17:29:24
【问题描述】:

我有一个目录 C:\RDP LINKS\,您可以在其中找到一大堆带有 rdp 快捷方式的文件夹。

问题是,所有快捷方式中的默认名称最近都已更改(最初是 administrator@testdomain.local,现在是 administrator@test2domain.local。

我想用 Powershell 更改 rdp 链接的内容。

如果用记事本打开rdp快捷方式,可以看到属性和默认用户名:

我试过这个脚本:

$configFiles = Get-ChildItem "C:\RDP LINKS\" *.rdp -rec
foreach ($file in $configFiles)
{
    (Get-Content $file.PSPath) |
    Foreach-Object { $_ -replace "administrator@testdomain.local", "administrator@test2domain.local" } |
    Set-Content $file.PSPath
}

脚本运行,但名称没有改变。我究竟做错了什么?

【问题讨论】:

  • 查看WScript.Shell COM object,及其方法和成员。您需要特别注意 CreateShortcut()Save() 方法。
  • 谢谢 Jeff,如果快捷方式已经存在,为什么我会对 createShortcut() 感兴趣
  • 其目的是在内存中创建合适的数据结构;如果您将路径传递给现有的快捷方式文件,它将读取数据并在已填充的内存中创建结构;如果路径不预先存在,它将在内存中创建一个包含所有字段的“空白”结构。 Save() 方法会将数据写入指定文件。
  • @JeffZeitlin 这些不是那种捷径。这些是 .rdp 文件,而不是 .lnk 文件。 WScript.Shell 对 .rdp 文件一无所知。 .rdp 文件是包含mstsc.exe 解释的设置的文本文件。
  • 你的用户名"administrator@testdomain.local "后面有一个空格我假设你替换它没有做任何事情

标签: powershell replace str-replace


【解决方案1】:

试试这个(使用全名属性而不是 pspath):

 ls "c:\rdp links\*.rdp" -recurse | %{
     (gc $_ ) -replace "administrator@testdomain.local", "administrator@test2domain.local" |
     set-content $_.FullName -force
}

【讨论】:

  • 谢谢,但这不起作用(脚本运行但没有变化)
  • Rdp 文件不是快捷方式
  • @LoïcMICHEL - 是的,我的注意力被吸引到了上面;挂断了“快捷方式”并错过了“rdp”。如果您稍微修改答案,我将撤回反对票。
  • 尝试以交互方式运行每条 Line 来兜售失败的图。在我的电台上运行良好...
【解决方案2】:

你想多了。这是一个直接的替换。

$configFiles = (Get-ChildItem "C:\RDP LINKS\" *.rdp) 
foreach ($file in $configFiles)
    {
    (get-content $file) -replace 'administrator@testdomain.local', 'administrator@test2domain.local' | set-content $file
}

【讨论】:

    猜你喜欢
    • 2019-12-10
    • 1970-01-01
    • 1970-01-01
    • 2020-12-19
    • 1970-01-01
    • 2022-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多