Powershell creating extra space

When I iterate thru list in file

This path get picked from the file:
\\pcna-02\InformationTechnologyServices\PackageMigration\SSISPackages\PCPRS01\RPTPROD\QI\CCIP_LOAD.dtsx                                                      
 
gets passed to my variable as this path:
\ \ p c n a - 0 2 \ I n f o r m a t i o n T e c h n o l o g y S e r v i c e s \ P a c k a g e M i g r a t i o n \ S S I S P a c k a g e s \ P C P R S 0 1 \ R P T P R O D \ Q I \ C C I P _ L O A D . d t s x                                                      
The path with all those space don't exists so nothing gets done. The paths inside the files also change with spaces. I have included a test file

Why does it create spaces and how can I avoid that..
My code listed below..
$Files = Get-ChildItem "\\pcna-02\InformationTechnologyServices\PackageMigration\ChangeFiles" 

Foreach($File in $Files){

    $FilePath = $File.FullName
    $Packages = Get-Content $FilePath
    
    IF($File = "FBPRODVS__FBPROD.txt" ){
        $Search = "FBPRODVS\\FBPROD"
        $Replacement = "FAPROD\FAPROD"
    }
    
    IF($File = "FBRPTPRODVS__FBRPTPROD.txt" ){
        $Search = "FBRPTPRODVS\\FBRPTPROD"
        $Replacement = "PCSQ-21\FARPTPROD"
    }
  
  
    IF($File = "PCSQ-03__FBRPTTEST.txt" ){
        $Search = "PCSQ-03\\FBRPTTEST"
        $Replacement = "PCSQ-14\FARPTTEST"
    }
       #Write-Host $FileName
       Write-Host $FilePath 
       Write-Host $Search
       Write-Host $Replacement
    
    

        Foreach($package in $Packages){


        Write-Host $package
        (Get-Content $FilePath) | Foreach-Object {  $_ -replace $SearchString, $Replacement } | Set-Content $FilePath
    }





}

Open in new window

PCSQ-03--FBRPTTEST.txt
LVL 8
Leo TorresSQL DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Raheman M. AbdulSenior Infrastructure Support Analyst & Systems DeveloperCommented:
Check the path is correct:
\\pcna-02\InformationTechnologyServices\PackageMigration\ChangeFiles

You can remove the whitespace by

$Filepath=$Filepath.Replace(' ','')
0
Rainer JeschorCommented:
Hi,
this is due to the encoding of the file. The file is encoded "UCS-2 Little Endian" which means two bytes per character. So you perhaps might want to use the -encoding parameter when reading the file content:
$Packages = Get-Content $FilePath -Encoding Unicode

Open in new window


HTH
Rainer
0
Leo TorresSQL DeveloperAuthor Commented:
RainerJ
Your recomendation returns Chinese characters for a path
sam
marahman3001
Your suggustion made sense and yes the paths seem now to not with Spaces
But still not working not getting other files
0
Redefine Your Security with AI & Machine Learning

The implications of AI and machine learning in cyber security are massive and constantly growing, creating both efficiencies and new challenges across the board. Check out our on-demand webinar to learn more about how AI can help your organization!

Rainer JeschorCommented:
Hi,
I tried the script change with your provided sample.
From the above screenshot I can see that at least the first lines are formatted correctly - therefore I assume that in your folder where you loop through you will have files with different encodings hence an ANSI encoded file will be formatted the wrong way.

Could you perhaps "Write-Host" the file where the chinese characters start and upload this file - just to confirm that encoding?

HTH
Rainer
0
footechCommented:
From similar issues I've seen in the past I think RainerJ is right about encoding being the issue.  But I think we can correct by modifying the line
(Get-Content $FilePath) | Foreach-Object {  $_ -replace $SearchString, $Replacement } | Set-Content $FilePath

Open in new window

to be
(Get-Content $FilePath) | Foreach-Object {  $_ -replace $SearchString, $Replacement } | Out-File $FilePath

Open in new window

The reason is that Set-Content (and Add-Content) uses ANSII as the default encoding while Out-File uses Unicode.  With PS 3.0 you could also use the -encoding parameter with Set-Content to adjust this.
0
Leo TorresSQL DeveloperAuthor Commented:
OK let rethink this I dont have much time to do this

This code has worked before. This one has a \ in the search and replace string.
BUt its not working I did the fancy up above to handle all the scenarios but its not working and i really need this to work today im in a time crunch can anyone tell me whats wrong here

$RootString = "\\pcna-02\InformationTechnologyServices\PackageMigration\SSISPackages\"
$SearchString = "FBPRODVS\\FBPROD"
$Replacement = "FAPROD\FAPROD"

$FileName = Get-ChildItem $RootString  -recurse | Select-String -pattern $SearchString | group path #| select name
#"\\pcna-02\mydocs\ltorres\My Documents\SSIS_Packages\PCSQ-02\PRODUCTION\MM_PCP_DMA_USERS.dtsx"

$FileName | Out-GridView

$FileName | Out-file -width 300 -filepath "C:\Users\ltorres\Desktop\FBPRODVS_FBPROD_exce.txt"


(Get-Content $FileName) | Foreach-Object {  $_ -replace $SearchString, $Replacement } | Set-Content $FileName

Open in new window

0
Leo TorresSQL DeveloperAuthor Commented:
Geting this error

PS C:\Users\ltorres\Dropbox\Documents\Lotteries> C:\Users\ltorres\Dropbox\Documents\Powershell\replace_string.ps1
Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo
Get-Content : Cannot find path 'C:\Users\ltorres\Dropbox\Documents\Lotteries\Microsoft.PowerShell.Commands.GroupInfo' because it does not exist.
At C:\Users\ltorres\Dropbox\Documents\Powershell\replace_string.ps1:19 char:13
+ (Get-Content <<<<  $FileName) | Foreach-Object {  $_ -replace $SearchString, $Replacement } | Set-Content $FileName
    + CategoryInfo          : ObjectNotFound: (C:\Users\ltorre...mands.GroupInfo:String) [Get-Content], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
 

Open in new window

0
footechCommented:
The error is because of what you're setting the $filename variable to in line 5.

Can you provide a sample input file that we can replicate the issue with?
0
Leo TorresSQL DeveloperAuthor Commented:
The FBRPTPRODVS\FBRPTPROD
should be replaced by
FAPROD\FAPROD
in this file and in all files that are in the directory
CCIP-LOAD.txt
0
footechCommented:
OK.  I re-read your question and I think understood it a bit better this time.  Nothing to do with the output file.  The problem (I think) is with your use of $packages.  I don't see any need for that at all.  First you were reading the entire file ($packages), and then dealing with each line ($package), and then doing a foreach-object which I'm pretty sure would be each letter.  Here's some revised code.
$Files = Get-ChildItem "\\pcna-02\InformationTechnologyServices\PackageMigration\ChangeFiles"

Foreach($File in $Files){

    $FilePath = $File.FullName
    
    IF($File.Name -eq "FBPRODVS__FBPROD.txt" ){
        $Search = "FBPRODVS\\FBPROD"
        $Replacement = "FAPROD\FAPROD"
    }
    
    IF($File.Name -eq "FBRPTPRODVS__FBRPTPROD.txt" ){
        $Search = "FBRPTPRODVS\\FBRPTPROD"
        $Replacement = "PCSQ-21\FARPTPROD"
    }
  
  
    IF($File.Name -eq "PCSQ-03__FBRPTTEST.txt" ){
        $Search = "PCSQ-03\\FBRPTTEST"
        $Replacement = "PCSQ-14\FARPTTEST"
    }

    If($File.Name -eq "CCIP-LOAD.xml" ){
        $Search = "FBRPTPRODVS\\FBRPTPROD"
        $Replacement = "FAPROD\FAPROD"
    }
        
       #Write-Host $FileName
       Write-Host $FilePath 
       Write-Host $Search
       Write-Host $Replacement
    
    
       (Get-Content $FilePath) | Foreach-Object {  $_ -replace $Search, $Replacement } | Set-Content $FilePath

}

Open in new window


Edit: Noticed some other code changes that needed to made.  FYI, with just testing the file that you provided in the last post, the following code did just what it sounds like you want (replacing the string).  Replacing Out-File with Set-Content also worked fine.
(Get-Content CCIP-LOAD.xml) | Foreach-Object {  $_ -replace $SearchString, $Replacement } | Out-File CCIP-LOAD.xml

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Leo TorresSQL DeveloperAuthor Commented:
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.