locked
Replace a line by finding a word RRS feed

  • Question

  • 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.


    Monday, August 20, 2018 5:36 PM

Answers

  • It is not necessary to edit this if you  declare it correctly.

    The quotes on "VisibleCmdlets" ae unnecessary.

    $test = @{
        Name ='Restart-Computer'
        Parameters=@{Name='ComputerName'},
        @{Name='Credential'},  
        @{Name='Force'}
    }
    New-PSSessionConfigurationFile -Path $FilePath -VisibleCmdlets $test -Author 'Arnaud Helin' -Description 'Used by PRTG' -CompanyName 'Infodom' -SessionType RestrictedRemoteServer -TranscriptDirectory 'C:\Transcript\' -RunAsVirtualAccount -Full
    


    \_(ツ)_/

    • Marked as answer by arnaud.helin Monday, October 1, 2018 2:04 PM
    Wednesday, August 22, 2018 8:42 AM

All replies

  • help Set-PSSessionConfiguration -online

    This is the command that edits the configuration.


    \_(ツ)_/

    Monday, August 20, 2018 6:04 PM
  • It doesn't help me but thanks for your time.

    By the way, I found a solution. Not the best, but it works.

    $FilePath = 'C:\Windows\System32\WindowsPowerShell\v1.0\SessionConfig\powershell.prtg.pssc'
    $test = "@{Name ='Restart-Computer'; Parameters=@{Name='ComputerName'},  @{Name='Credential'},  @{Name='Force'}}"
    New-PSSessionConfigurationFile -Path $FilePath -VisibleCmdlets $test -Author 'Arnaud Helin' -Description 'Used by PRTG' -CompanyName 'Infodom' -SessionType RestrictedRemoteServer -TranscriptDirectory 'C:\Transcript\' -RunAsVirtualAccount -Full
    (Get-Content -Path $FilePath).Replace("''","'").Replace("'@","@").Replace("}}'","}}") | Set-Content -Path $FilePath
    


    The key of learning is practice.

    Monday, August 20, 2018 8:15 PM
  • Hi,

    Thank you for sharing the solution to this issue, I learn more from your reply, and I believe partners who may visit this thread in the future will benefit from your sharing.

    Best Regards,

    Lee


    Just do it.

    Tuesday, August 21, 2018 6:44 AM
  • It is not necessary to edit this if you  declare it correctly.

    The quotes on "VisibleCmdlets" ae unnecessary.

    $test = @{
        Name ='Restart-Computer'
        Parameters=@{Name='ComputerName'},
        @{Name='Credential'},  
        @{Name='Force'}
    }
    New-PSSessionConfigurationFile -Path $FilePath -VisibleCmdlets $test -Author 'Arnaud Helin' -Description 'Used by PRTG' -CompanyName 'Infodom' -SessionType RestrictedRemoteServer -TranscriptDirectory 'C:\Transcript\' -RunAsVirtualAccount -Full
    


    \_(ツ)_/

    • Marked as answer by arnaud.helin Monday, October 1, 2018 2:04 PM
    Wednesday, August 22, 2018 8:42 AM
  • Thank you jrv, still one of the best.^^

    The key of learning is practice.

    Monday, October 1, 2018 2:04 PM
  • Glad to be of help.


    \_(ツ)_/

    Monday, October 1, 2018 2:08 PM