简体   繁体   中英

How to escape characters in Powershell

I am using below command to test the URL and same is used in monitoring tool.

cmd \c powershell -command "Invoke-WebRequest http://V2-LBDAX-PRD01:8101/DynamicsAx/Services/AxManageabilityServiceGroup" | findstr /c:"200 OK"
cmd \c powershell -command "Invoke-WebRequest http://V2-LBDAX-PRD01:8101/DynamicsAx/Services/BIServices" | findstr /c:"200 OK"
cmd \c powershell -command "Invoke-WebRequest http://V2-LBDAX-PRD01:8101/DynamicsAx/Services/CARDIXFServiceGroup" | findstr /c:"200 OK"
cmd \c powershell -command "Invoke-WebRequest http://V2-LBDAX-PRD01:8101/DynamicsAx/Services/CARItemStorePriceAvailServiceGroup" | findstr /c:"200 OK"

First and second command works, but the third and fourth command doesn't works. How do i escape /C. All url's having /C,i am getting error as below

'ARDIXFServiceGroup' is not recognized as an internal or external command, operable program or batch file.

If you need to encode just the url, you can use HttpUtility.UrlEncode Method

[System.Web.HttpUtility]::UrlEncode("http://V2-LBDAX-PRD01:8101/DynamicsAx/Services/BIServices")

then replace the output with your current url,

Also you can use the Powershell -EncodedCommand to encode the whole command, also you can convert the findstr /c:"200 OK" to Powershell cmdlet Select-String so:

# Encode the Command First:
$command = 'Invoke-WebRequest http://V2-LBDAX-PRD01:8101/DynamicsAx/Services/BIServices | Select-String "200 OK"'
$bytes = [System.Text.Encoding]::Unicode.GetBytes($command)
$encodedCommand = [Convert]::ToBase64String($bytes)
cmd /c powershell -encodedCommand $encodedCommand

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