How to handle file paths with special characters?

前端 未结 2 1123
青春惊慌失措
青春惊慌失措 2021-01-15 04:52

I\'ve got a PowerShell script that needs to be able to call another PS script from within the same directory with elevated credentials. Because the scripts need to be porta

2条回答
  •  北荒
    北荒 (楼主)
    2021-01-15 05:41

    So, I was able to figure out a solution that works as long as the brackets aren't empty [].

    Here's what I've got:

    Start-Process Powershell -Verb runas -ArgumentList "-ExecutionPolicy","Bypass","-File","`"$(Get-Item -LiteralPath $PSScriptRoot\elevated.ps1)`""
    

    Using this as the test path:

    C:\Users\Administrator\Desktop\Test Folder\badname-!@#$%^_+-=.,{}`~[&]()
    

    A path with empty brackets [] still doesn't work, but doesn't return an error. I'm assuming this means that it wasn't able to find a file in the expected path while interpreting the brackets as a wildcard still. Not sure if there's really anything that will fix that except maybe checking the path before running and throwing an error.

提交回复
热议问题