Spaces cause split in path with PowerShell

前端 未结 12 866
广开言路
广开言路 2020-12-02 15:24

I\'m having an issue with powershell when invoking an exe at a path containing spaces.

PS C:\\Windows Services> invoke-expression \"C:\\Windows Services\\My

12条回答
  •  感动是毒
    2020-12-02 15:34

    Can use the . dot operator.

    . "C:\Users\user\AppData\Local\Programs\Microsoft VS Code\bin\code.cmd"
    

    or the Start-Process command

    Start-Process -PSPath "C:\Users\user\AppData\Local\Programs\Microsoft VS Code\bin\code.cmd"
    

    or using ProcessStartInfo and Process

    $ProcessInfo = New-Object -TypeName System.Diagnostics.ProcessStartInfo
    $ProcessInfo.FileName = 'C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe'
    if($Admin){ $ProcessInfo.Verb = 'runas' }
    $ProcessInfo.UseShellExecute = $false
    
    $CommandParameters = '-noexit -noprofile -command Set-Location -LiteralPath c:\; $host.ui.RawUI.WindowTitle = ''[{0}] PS''; Set-PSReadlineOption -HistorySaveStyle SaveNothing;' -f $Cred.UserName
    $ProcessInfo.Arguments = $CommandParameters
    $ProcessInfo.Domain = ($Cred.UserName -split '\\')[0]
    $ProcessInfo.UserName = ($Cred.UserName -split '\\')[1]
    $ProcessInfo.Password = $Cred.Password
    
    $ProcessObject = New-Object -TypeName System.Diagnostics.Process
    $ProcessObject.StartInfo = $ProcessInfo
    $ProcessObject.Start() | Out-Null
    

提交回复
热议问题