Out-File -encoding problems - TechNet - Microsoft

文章推薦指數: 80 %
投票人數:10人

I have written one of my first PowerShell scripts to execute after I mirror my ... other than to rename my original files to ANSI format? TechNet Products ITResources Downloads Training Support Products Windows WindowsServer SystemCenter MicrosoftEdge   Office Office365 ExchangeServer   SQLServer SharePointProducts SkypeforBusiness Seeallproducts» Resources Channel9Video EvaluationCenter LearningResources MicrosoftTechCompanionApp MicrosoftTechnicalCommunities MicrosoftVirtualAcademy ScriptCenter ServerandToolsBlogs TechNetBlogs   TechNetFlashNewsletter TechNetGallery TechNetLibrary TechNetMagazine TechNetWiki WindowsSysinternals VirtualLabs Solutions Networking CloudandDatacenter Security Virtualization Updates ServicePacks SecurityBulletins WindowsUpdate Trials WindowsServer2016 SystemCenter2016 Windows10Enterprise SQLServer2016 Seealltrials» RelatedSites MicrosoftDownloadCenter MicrosoftEvaluationCenter Drivers WindowsSysinternals TechNetGallery Training Expert-led,virtualclasses TrainingCatalog ClassLocator MicrosoftVirtualAcademy FreeWindowsServer2012courses FreeWindows8courses SQLServertraining MicrosoftOfficialCoursesOn-Demand Certifications Certificationoverview Specialoffers MCSECloudPlatformandInfrastructure MCSE:Mobility MCSE:DataManagementandAnalytics MCSEProductivity Otherresources MicrosoftEvents ExamReplay BornToLearnblog Findtechnicalcommunitiesinyourarea Azuretraining OfficialPracticeTests Supportoptions Forbusiness Fordevelopers ForITprofessionals Fortechnicalsupport Supportofferings Moresupport MicrosoftPremierOnline TechNetForums MSDNForums SecurityBulletins&Advisories NotanITpro? MicrosoftCustomerSupport MicrosoftCommunityForums ThisforumhasmigratedtoMicrosoftQ&A.VisitMicrosoftQ&Atopostnewquestions. LearnMore WindowsServerTechCenter Signin UnitedStates(English) Brasil(Português)Česko(Čeština)Deutschland(Deutsch)España(Español)France(Français)Indonesia(Bahasa)Italia(Italiano)România(Română)Türkiye(Türkçe)Россия(Русский)ישראל(עברית)المملكةالعربيةالسعودية(العربية)ไทย(ไทย)대한민국(한국어)中国(中文)台灣(中文)日本(日本語)  HomeWindowsServer2012WindowsServer2008R2WindowsServer2003LibraryForums Askaquestion Quickaccess Forumshome Browseforumsusers FAQ Searchrelatedthreads RemoveFromMyForums Askedby: Out-File-encodingproblems ArchivedForums901-920  >  WindowsPowerShell Question 0 Signintovote IhavewrittenoneofmyfirstPowerShellscriptstoexecuteafterImirrormymusiccollectiontobackitup. ThePSscriptIwrotewillreplaceastringoftextusingthe"-replace"command. However,whentheoutfileiswritten,theoriginal encodingislost. Forinstance,thefollowinglineisthesource: Andtheoutput: IfIopentheoriginalfilewithNotepad++,Iseetheformatisin"UTF-8withoutBOM." Imodifiedmyscriptfrom:   $fileEntries=[IO.Directory]::GetFiles("\\Server\backup$\test\Playlists"); foreach($fileNamein$fileEntries) { [Console]::WriteLine($fileName); $c=Get-Content$fileName; $n=$c-replace('\\\\server\\music','S:'); Out-File$fileName-inputobject$n; } to: $fileEntries=[IO.Directory]::GetFiles("\\Server\backup$\test\Playlists"); foreach($fileNamein$fileEntries) { [Console]::WriteLine($fileName); $c=Get-Content$fileName; $n=$c-replace('\\\\server\\music','S:'); Out-File$fileName-inputobject$n-encodingUTF8; }   IsthisaproblemwithOut-Fileandit'slackofproperencodingenforcement? Isthereanywaytofixthis,otherthantorenamemyoriginalfilestoANSIformat?   Tuesday,August3,20107:08PM Allreplies 1 Signintovote helpOut-File-fullhasthisdescriptionfor-Encoding   -Encoding Specifiesthetypeofcharacterencoding usedinthefile.Validvaluesare "Unicode","UTF7","UTF8","UTF32","ASCII", "BigEndianUnicode","Default",and"OEM". "Unicode"isthedefault.   Thesearestringvalues. So,wrapyourvalueinquotesas"UTF8"   Tuesday,August3,20107:27PM 0 Signintovote Thanksforthereply. Ididasyousuggested,butsameresult. Tuesday,August3,20107:45PM 0 Signintovote Lookat   http://poshcode.org/1822   Ithinkitshouldworkwith-encoding"UTF8"   -Larry   On8/3/20102:45PM,ChrisLynchwrote: >Thanksforthereply.Ididasyousuggested,butsameresult.     Tuesday,August3,20108:18PM 0 Signintovote Understood. Ibelievethatitshouldworkaswell,butitdoesnot. Iwonderifduringthe-replaceprocesstextisbeingconvertedtoANSIformat,andthenUTF-8whenwritingthefile. Iamsimplyassuminghere. Tuesday,August3,20108:30PM 0 Signintovote Hi, Doyourememberhowyoumanagedyourproblem?Igetthesame regards Monday,May28,20182:41PM 0 Signintovote Pleasedonotreactivateancientthreadsfromotherpeople.Itismuchlikelythattheoriginalposterwillnotcomebackandansweryou. Ifneedecreateanewquestionforyourproblem.Beforeyoushouldcarefullyreviewthefollowinghelptopics: Pleasereadthisfirst: Thisforumisforscripting questionsratherthanscriptrequests. Alsofindscriptshere: PowerShellGalleryorhere: TechNet Gallery-resourcesforITprofessionals. LearnPowerShell: MicrosoftVirtualAcademy-GettingStartedwithMicrosoftPowerShell. Scriptrequests: MicrosoftTechnetScriptCenter-Requests. Bestregards, (79,108,97,102|%{[char]$_})-join'' Editedby BOfH-666 Monday,May28,20182:48PM Monday,May28,20182:45PM 0 Signintovote Himijo, theproblemintheabovecodeisthattheencodinghappensattwo separateinstances,bothwhenthecontentgoesinandout. YoualsohavetochangetheencodingforGet-Contentaccordingly,otherwiseyou'llenduphavingthegarbledcharactersfromtheverybeginninginyourscript. Get-Content-EncodingUTF8 Out-File-EncodingUTF8 Regards, Dequester Editedby Dequester Thursday,July26,20188:07PM Addcode Proposedasanswerby Dequester Thursday,July26,20188:09PM Thursday,July26,20188:06PM  ©2022Microsoft.Allrightsreserved.Newsletter|ContactUs|PrivacyStatement|TermsofUse|Trademarks



請為這篇文章評分?