Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Change part of text file and keep other content in the text file

$
0
0

I'm writing a script to edit out template .opt files. I need to edit a couple of things in the file but otherwise keep all the other content in the .opt file. The script I wrote will replace the content I'm looking for but doesn't keep all the other information in the .opt file. You can run the script against and should read file ServerName_TSMVE_Proxy_dsm.opt on \\servername\e$\TSMVE_OPT_Files\TSMVE_Jobs and the script will edit the file and wipe out all the other content I need.

 

Is there a way to keep all the content in the .opt file and replace a couple parts of the .opt file? Please help

 

Attachments:

1. .ps1 file

2. .cvs file on what I need to change

3. ServerName_TSMVE_Proxy_dsm.opt - The temp file

4. aXsxt1006_TSMVE_Proxy_dsm.opt - The new file with edited content but wiped out the rest of the content

 

I need aXsxt1006_TSMVE_Proxy_dsm.opt file to look like ServerName_TSMVE_Proxy_dsm.opt file but just this information changed

   'NODENAME ServerName-VE' = 'NODENAME' + " " + $NODENAME

                        'TCPPORT' = 'TCPPORT' + $TCPPORT

                        'TCPSERVERADDRESS' = 'TCPSERVERADDRESS' + " " + $TCPSERVERADDRESS

                        '-VM=wxxxt1006' = '-VM=' + " " + $SERVERNAME

                        'VMCHOST apsep1271cls.ms.ds.uhc.com' = 'VMCHOST' + " " + $VMCHOST

 

####################### Start of Script #####################################

 

 

$servers = Import-Csv "C:\temp\TSMVEOptInfo.csv"

 

    ForEach ($server in $servers)

            {

                $lookupTable = @{

                        'NODENAME ServerName-VE' = 'NODENAME' + " " + $NODENAME

                        'TCPPORT' = 'TCPPORT' + $TCPPORT

                        'TCPSERVERADDRESS' = 'TCPSERVERADDRESS' + " " + $TCPSERVERADDRESS

                        '-VM=wxxxt1006' = '-VM=' + " " + $SERVERNAME

                        'VMCHOST server.domain' = 'VMCHOST' + " " + $VMCHOST

                        #'something6' = 'something6dfsfds'

                    }

 

                $SERVERNAME = $server.ServerName

                $NODENAME = $server.NODENAME

                $TCPSERVERADDRESS = $server.TCPSERVERADDRESS

                $TCPPORT = $server.TCPPORT

                $VMCHOST = $server.VMCHOST

                #$TSMVEOPTPATH =

                $uncServer = "\\$servername"

                $uncFullPath = "$uncServer\e$" #\\yournservername\e$\TSMVE_OPT_Files\TSMVE_Jobs

                $dir = "\TSMVE_OPT_Files\TSMVE_Jobs\"

                #$cred = Get-Credential

                #$networkCred = $cred.GetNetworkCredential()

                #$DrvMapping = (New-Object -Com WScript.Network).MapNetworkDrive("p:" , "\\$SERVERNAME\e$")

                #$DrvMapping = New-PSDrive –Name “P” –PSProvider FileSystem –Root “$uncFullPath”

 

                $net = new-object -ComObject WScript.Network

                $net.MapNetworkDrive("p:", "$uncFullPath", $false, "Domain\Username", "DomainPassword")

 

                #$original_file = "$uncFullPath" + "$dir" + "ServerName_TSMVE_Proxy_dsm.opt"

                #$destination_file =  "$uncFullPath" + "$dir" + "$ServerName_TSMVE_Proxy_dsm.opt"

 

                $original_file = "p:" + "$dir" + "ServerName_TSMVE_Proxy_dsm.opt"

                $destination_file = "p:" + "$dir" + $ServerName + "_TSMVE_Proxy_dsm.opt"

 

                Get-Content -Path $original_file | ForEach-Object {

                $line = $_

 

                   $lookupTable.GetEnumerator() | ForEach-Object {

                    if ($line -match $_.Key)

                        {

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

                               #break

                        }

                    }

                } | Set-Content -Path $destination_file

            }


Viewing all articles
Browse latest Browse all 6937

Trending Articles