Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Replace a line by finding a word

$
0
0

Hi,

I have a (easy) problem but not able to solve it. I create a New-PSSessionConfigurationFile with parameters but i'd like to replace a line because when I do the command as-is, it doesn't use the correct syntax. Look by yourself:

My command is :

New-PSSessionConfigurationFile -Path $FilePath -SessionType RestrictedRemoteServer -TranscriptDirectory 'C:\Transcript\' -RunAsVirtualAccount -Full

 

It generates a file but I want to change the line :

# VisibleCmdlets = 'Invoke-Cmdlet1', @{ Name = 'Invoke-Cmdlet2'; Parameters = @{ Name = 'Parameter1'; ValidateSet = 'Item1', 'Item2' }, @{ Name = 'Parameter2'; ValidatePattern = 'L*' } }

by

"VisibleCmdlets=@{Name ='Restart-Computer'; Parameters=@{Name='ComputerName'},  @{Name='Credential'},  @{Name='Force'}}"

So I did :

(Get-content -FilePath $file).Replace((Select-String -Path $FilePath -Pattern '# VisibleCmdlets' -SimpleMatch),"VisibleCmdlets=@{Name ='Restart-Computer'; Parameters=@{Name='ComputerName'},  @{Name='Credential'},  @{Name='Force'}}")

didn't work, so I try :

$content -replace "^# VisibleCmdlets", "VisibleCmdlets=@{Name ='Restart-Computer'; Parameters=@{Name='ComputerName'},  @{Name='Credential'},  @{Name='Force'}}"


But the output is 

VisibleCmdlets=@{Name ='Restart-Computer'; Parameters=@{Name='ComputerName'},  @{Name='Credential'},  @{Name='Force'}} = 'Invoke-Cmdlet1', @{ Name = 'Invoke-Cmdlet2'; Parameters = @{ Name = 'Parameter1'; ValidateSet = 'Item1', 'Item2' }, @{ Name = 'Parameter2'; ValidatePattern = 'L*' } }

It adds to the content not replacing it.

Can you help me ?



The key of learning is practice.



Viewing all articles
Browse latest Browse all 21975

Trending Articles