내가 이것에 대해 좌절하는 것이 잘못되면 나를 용서해주십시오. 그러나 이것이 PowerShell과 같은 스크립팅 언어로 원격으로 복잡한 작업을하는 것을 싫어하는 이유입니다. 그것은 예측할 수없는 전쟁을 끝내지 못합니다. 이것은 PowerShell TrimStart의 버그입니까?
PS C:\WINDOWS\system32> $PSVersionTable.PSVersion
Major Minor Build Revision
----- ----- ----- --------
5 1 15063 632
PS C:\WINDOWS\system32> $testpath = "\\a-server-unc\home\auser\Outlook Data\archive1 - Copy.pst"
PS C:\WINDOWS\system32> $(Split-path -path $testpath -Parent)
\\a-server-unc\home\auser\Outlook Data #Ok - Looks good
PS C:\WINDOWS\system32> $(Split-path -path $testpath -Parent).TrimStart("\\a-server-unc")
home\auser\Outlook Data # Where did my slash go?
PS C:\WINDOWS\system32> $(Split-path -path $testpath -parent).TrimStart("\\a-server-unc\home")
Outlook Data # Wait - What the...
PS C:\WINDOWS\system32> $testpath
\\a-server-unc\home\auser\Outlook Data\archive1 - Copy.pst # Yep - I'm not insane. Powershell is eating my characters.
PS C:\WINDOWS\system32> $testpath = "\\a-server-unc\home\aloser\Outlook Data\archive1 - Copy.pst" #Let's try changing the string a little
PS C:\WINDOWS\system32> $(Split-path -path $testpath -Parent).TrimStart("\\a-server-unc\home")
loser\Outlook Data # I must be insane
PS C:\WINDOWS\system32> $(Split-path -path $testpath -Parent).TrimStart("`\`\a-server-unc`\home") #Certainly escaping the slashes will fix this?
loser\Outlook Data # Nope, more hours wasted on unpredictable behavior
지금 분명히 여기에 몇 가지 이상한, "완벽하게 정상적인"행동이,하지만 난 내 문자열을 먹는 파워 쉘의 조금 피곤 해요 :
그래서, 여기 있습니다.
여기에있는 마법의 묘약은 무엇입니까? (당신이 기대하는 것 같은) TrimStart()
가 입력되지 문자열로 문자 배열을 참고, 특히 https://msdn.microsoft.com/en-us/library/system.string.trimstart.aspx
:
"나는 PowerShell을 같은 스크립트 언어에서 원격으로 복잡한 무엇을하려고 싫어하는 이유 *는하지만,이는 그것은 예측 불가능과 끝없는 전투입니다. *."- 닷넷에서 String.TrimStart()이 있었다 PowerShell이 출시되기 최소 3 년 (15 년 이상). C#과 VB.Net, Python, PHP에서도 마찬가지입니다. 자바 스크립트, 자바, 스위프트, 루비, C++의 std :: string; 지금까지 내가 찾은 유일한 언어는 두 가지 버전 중 하나를 선택하는 것이다. 그 언어에서 오는 언어는 무엇이 다른가를 예측하고 '예측할 수없는'것이라고 설명하도록 이끌고 있습니까? – TessellatingHeckler
어쨌든'.Trim()'은 공백, 탭, 개행 문자 인 문자열의 양쪽 끝에서 공백을 제거합니다. 또는 특정 문자를 제거하도록 확장 될 수 있습니다 (예 : 어떤 종류의 따옴표 문자 나 문자열의 양쪽에서 괄호/괄호를 제거하십시오. 이러한 맥락에서 문자 배열은 의미가 있으며 .TrimStart와 .TrimEnd를 제한하는 것이 더 합리적입니다. 다른 방법을 일반화하고 한쪽 끝에서 고정 된 문자열을 제거 할 수있는 함수를 사용하면 양쪽 끝에서 고정 된 문자열을 제거 할 수있는 함수가됩니다. 그만큼 유용하지 않기 때문입니다. – TessellatingHeckler
'-replace "^ \\\ a-server-unc"'는 정규식 패턴이 일치하여 특수 문자가 이스케이프 처리되거나'[regex] :: Escape()' '가 필요하다는 경고와 함께 처리합니다. 그것은 대소 문자를 구별합니다 ('.Replace()'는 대소 문자를 구별합니다). – TessellatingHeckler