繁体   English   中英

如何在Powershell中替换远程计算机上文件中的字符串?

[英]How to replace a string in file on remote computer in Powershell?

我有一台运行Windows Server 2012的VM,上面有一些预期的服务。 我想从台式机远程更改此VM计算机上的配置文件。 目前,我通过映射远程服务器的C:驱动器来更改此配置文件,然后更改该文件。 现在这使我无法更改多个服务器,因为我无法将多个服务器c:同时映射到同一驱动器。 而且,映射数百个驱动器也不是理想的选择。 我通过映射驱动器更改文件的方式是:

$password = <password text> | ConvertTo-SecureString -asPlainText -Force
$username = "admin"
$credential = New-Object System.Management.Automation.PSCredential($username,$password)
net use Z: \\$ipAddress\C$ <password> /user:admin type 'z:\Program Files\lalaland\Data\Settings.xml'

(Get-Content 'z:\Program Files\lalaland\Data\Settings.xml') | ForEach-Object { $_ -replace $oldString, $newString } | Set-Content 'z:\Program Files\lalaland\Data\Settings.xml'
type 'z:\Program Files\lalaland\Data\Settings.xml'
net use z: /delete

因此,我搜索了一个更好的选项,并在https://gallery.technet.microsoft.com/scriptcenter/PowerShell-Replace-String-58fbfa85中找到了此脚本,但它对我不起作用。

我使用此脚本为:

.\Replace-StringInFile.ps1 -ComputerName  <RemoteComputerHostName> -TargetPath 'C:\Program Files\lalaland\Data' -Fil

eName Settings.xml-替换$ oldString -ReplaceWith $ newString-凭据(Get-Credential)

当我运行命令凭据窗口时,会弹出询问用户名和密码的窗口。 我输入用于映射驱动器的用户名和密码,但是会引发以下错误:

New-PSSession : [<RemoteHostName>] Connecting to remote server <RemoteHostName> failed with the following error message : The user name or password is incorrect. For more information, see the
about_Remote_Troubleshooting Help topic.
At D:\workspace\Replace-StringInFile.ps1:84 char:14
+ ...  $Session = New-PSSession -ComputerName $Computer -Credential $Creden ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [New-PSSession], PSRemotingTransportException
+ FullyQualifiedErrorId : LogonFailure,PSSessionOpenFailed

我不明白的是,当我使用相同的凭据映射驱动器时,它可以正常工作,但使用内部使用New-PSSession的其他脚本不起作用。

任何想法 ?

我可以使用以下cmdlet执行此操作:

function Edit-RemoteFileStringReplace
{
  [cmdletbinding()]
  param([Parameter(Mandatory=$true)][ValidateScript({$_ -match [IPAddress]$_ })][string]$Address,
        [Parameter(Mandatory=$true)][string]$FilePath,
        [Parameter(Mandatory=$true)][string]$Replace,
        [Parameter(Mandatory=$true)][string]$ReplaceWith,
        [Parameter(Mandatory=$true)][System.Management.Automation.PSCredential]$Credential
  )

  $DriveLetter=""
  $FilePathWithoutDriveLetter=""

  $DriveName = $Address.replace('.','x')
  $DriveName = "PSDrive_$DriveName"

  $DriveLetter = (Get-DriveLetterFromPath -Path $FilePath).Substring(0,1)
  $FilePathWithoutDriveLetter = Remove-DriveLetterFromPath -Path $FilePath
  $MappedFilePath="$DriveName" + ":$FilePathWithoutDriveLetter"

  New-PSDrive -Name $DriveName -PSProvider FileSystem -Root \\$Address\$DriveLetter$ -Credential $Credential -ErrorAction Stop
  (Get-Content $MappedFilePath) | ForEach-Object { $_ -replace $Replace, $ReplaceWith } | Set-Content $MappedFilePath
  Remove-PSDrive -Name $DriveName
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM