简体   繁体   中英

Powershell parameter : string in path

I want to launch a .ps1 from Windows Command Prompt. My Powershell script has a path parameter. Here's my command line to launch the .ps1 powershell Set-ExecutionPolicy Unrestricted -scope process ; C:\\\\MyPowerShell.ps1 -Path \\"C:\\\\Test for my script\\test1.txt" powershell Set-ExecutionPolicy Unrestricted -scope process ; C:\\\\MyPowerShell.ps1 -Path \\"C:\\\\Test for my script\\test1.txt" In MyPowerShell.ps1, if I Write-Host Path, I get : C:\\Test. It cuts my string when the first space appears. How can I get my full path ? Thx.

Option 2: Don't escape the path, and instead remove the quotes from what is passed in.

    $path2file = $path2file.replace('"',"").replace("'","")

This will allow anyone to put quotes around the path (an accepted practice norm) and your application strips the quotes and has the whole path in one variable. If the path is not quoted, then this lines has no effect.

Where it will fail is if a quote (single or double) is included in the actual file path.

I've put ' : -Path '\\"C:\\\\Test for my script\\test1.txt"' and it works. Thx :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM