Summary of techniques for running the cmd command in PowerShell (resolving problems with name conflicts and special characters) _powershell

Source: Internet
Author: User

Introduction

I'm from the old CMD. EXE command line to a good powsershell. EXE has been there for some time. You may know that the new Windows PowerShell can run any old commands. However, the name or syntax of some old commands may cause problems. But that's not the thing.

Trouble 1: Name conflict

The conflict between the PowerShell cmdlet alias and the name of the old command is a common problem. Let's say you like the Service Control command SC. Exe. Sc. EXE is very flexible! I can understand why you like it (do not use NET.) EXE Management Service to find excuses). If you want to view the status of the SMB Server service, you can do so in CMD. EXE in this way:

Copy Code code as follows:

C:\>SC QUERY LanManServer

Service_name:lanmanserver
Type:20 win32_share_process
State:4 RUNNING
(Stoppable, pausable, Ignores_shutdown)
win32_exit_code:0 (0x0)
service_exit_code:0 (0x0)
checkpoint:0x0
wait_hint:0x0


If you try the same thing in PowerShell, you get:
Copy Code code as follows:

PS c:\> SC QUERY LanManServer
Set-content:access to the path ' C:\QUERY ' is denied.
At Line:1 char:1
+ SC QUERY LanManServer
+ ~~~~~~~~~~~~~~~~~~~~~
+ categoryinfo:permissiondenied: (c:\query:string) [set-content], UnauthorizedAccessException
+ Fullyqualifiederrorid:getcontentwriterunauthorizedaccesserror,microsoft.powershell.commands.setcontentcommand

Because SC is the alias of Set-content. It takes precedence over SC. EXE file.

Programme 1 A: use. EXE name extension

To overcome this problem, you can simply put. EXE extension is included in the old command. This eliminates ambiguity and makes the same command in CMD. Both EXE and PowerShell can be used. It's also clear that the person using your script is using the old one here. EXE command rather than PowerShell alias.

Copy Code code as follows:
PS c:\> SC. EXE QUERY LanManServer

Service_name:lanmanserver
Type:20 win32_share_process
State:4 RUNNING
(Stoppable, pausable, Ignores_shutdown)
win32_exit_code:0 (0x0)
service_exit_code:0 (0x0)
checkpoint:0x0
wait_hint:0x0

Programme 1 B: use of CMD/C

Another option is to enclose your command in quotation marks for CMD. EXE to run. But doing so is inefficient, and you have to run a CMD just to execute your command. EXE instance.

Copy Code code as follows:
PS c:\> cmd/c "SC QUERY LanManServer"

Service_name:lanmanserver
Type:20 win32_share_process
State:4 RUNNING
(Stoppable, pausable, Ignores_shutdown)
win32_exit_code:0 (0x0)
service_exit_code:0 (0x0)
checkpoint:0x0
wait_hint:0x0

Scheme 1C: Using the equivalent PowerShell

In many cases, you can use the PowerShell cmdlet instead of your old commands.
For example, here you can use Get-service directly:

Copy Code code as follows:

PS c:\> Get-service LanManServer | FL

Name:lanmanserver
Displayname:server
Status:running
Dependentservices: {Browser}
Servicesdependedon: {SAMSS, SRV}
Canpauseandcontinue:true
Canshutdown:false
Canstop:true
Servicetype:win32shareprocess


Trouble 2:powershell Special characters

Sometimes the characters used in the parameters of the old command have special meaning in the PowerShell.
For example, you want a directory to be fully controlled by all users. In CMD. EXE You can do this:

Copy Code code as follows:

C:\>icacls. EXE c:\test/grant USERS: (F)
Processed File:c:\test
Successfully processed 1 files; Failed Processing 0 Files

In CMD. EXE does not have a problem, but if you run in the PowerShell will be an error:
Copy Code code as follows:

PS c:\> icacls. EXE c:\test/grant USERS: (F)
The term ' F ' isn't recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling
of the name, or if a path was included, verify this path is correct and try again.
At Line:1 char:34
+ icacls. EXE c:\test/grant USERS: (F)
+ ~
+ Categoryinfo:objectnotfound: (f:string) [], commandnotfoundexception
+ Fullyqualifiederrorid:commandnotfoundexceptionn

Attempting to authorize a computer object whose name ends with $ also causes a similar error.
Copy Code code as follows:

PS c:\> icacls. EXE c:\test/grant computername$:(F)
At Line:1 char:39
+ icacls. EXE c:\test/grant computername$:(F)
+ ~~
Invalid variable Reference. ' $ ' is not followed by a valid variable name character. Consider using ${} to delimit the
Name.
+ Categoryinfo:parsererror: (:) [], Parentcontainserrorrecordexception
+ fullyqualifiederrorid:invalidvariablereference

The problem is that parentheses and dollar characters have special meanings in PowerShell. Common characters such as curly braces can also cause similar conflicts. There are also several different solutions to this problem.

Scenario 2 A: Using CMD/C

As with the first question, you can enclose your command in quotation marks to CMD. EXE to handle. Regardless of efficiency, PowerShell does not parse the strings in quotes so that they work correctly.

Copy Code code as follows:

PS c:\> CMD. Exe/c "Icacls. EXE c:\test/grant USERS: (F) "
Processed File:c:\test
Successfully processed 1 files; Failed Processing 0 Files

Scenario 2 B: use PowerShell escape characters

For this scenario, you must first know what characters are used for PowerShell special significance. Then precede each of them with an inverted quotation mark ('), which is the PowerShell escape character. The main problem with this scenario is that you have to know which characters need to be escaped, which makes it more difficult to read and write your scripts.
In our example, you need to handle (and) these two characters:

Copy Code code as follows:

PS c:\> icacls. EXE c:\test/grant USERS: ' (F ')
Processed File:c:\test
Successfully processed 1 files; Failed Processing 0 Files


Scenario 2C: Using the new syntax "–%" with PowerShell v3

There is another option in PowerShell v3 to solve this problem. All you need to do is add the –% sequence (two dashes and a percent semicolon) anywhere on the command line PowerShell will not be able to parse the remaining parts.
In our example, you can use this:

Copy Code code as follows:

PS c:\> icacls. EXE--% c:\test/grant USERS: (F)
Processed File:c:\test
Successfully processed 1 files; Failed Processing 0 Files

You can also use this:
Copy Code code as follows:

PS c:\> icacls. EXE C:\TEST--%/grant USERS: (F)
Processed File:c:\test
Successfully processed 1 files; Failed Processing 0 Files

Scenario 2D: Using the equivalent PowerShell

Using the equivalent PowerShell is also a choice. Icacls. EXE can be replaced with Set-acl. You can find more examples of set-acl from this blog.

Mix and Match

Here's how you can safely enjoy the flexibility that PowerShell brings with your old commands. You may learn a few tricks and start new and old combinations in a new way.

For example, you can replace SC with flexible get-service wildcard characters. EXE in the obscure option:

Copy Code code as follows:
Get-service lan* | % { $_. Name; Sc. EXE sdshow $_. Name}

Alternatively, you can use PowerShell's Get-item (alias Dir) to filter the file subset to ICACLS. EXE to handle:
Copy Code code as follows:
DIR C:\TEST-Recurse |? {$_. Length-ge 1MB} | % {icacls. EXE $_. Fullname/grant Administrator: ' (F ')}

You can even iterate through several numbers and combine the handy FSUTIL. EXE to create a batch of files of different sizes for testing the project:
Copy Code code as follows:
1..100 | % {FSUTIL. EXE FILE CREATENEW C:\TEST\FILE$_. TXT ($_*10KB)


End

Now, you may be convinced that Windows PowerShell is a good friend of the administrators. However, you may not be able to use POWERSHELL because of some old commands with odd names or parameters. Exe. I highly encourage you to use these techniques to completely deactivate CMD. EXE and permanently migrate to PowerShell as your main shell.

Article Source: http://www.pstips.net/using-windows-powershell-to-run-old-command-line-tools-and-their-weirdest-parameters.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.