Are there any fundamental incompatibilities when using the CMD script in the console using PowerShell?

I have an extensive set of CMD scripts for an automation suite.

In the console using CMD.exeeverything is working fine. After installing the update for Windows Creator, when PowerShell becomes the new default Windows shell using the Explorer menu options, my scripts are interrupted randomly. I cannot provide any meaningful code for playback for two main reasons:

  • Missing obvious error; my automated scripts just hang in the end
  • The stop does not even occur in the same place every time

I can tell you that the package is highly dependent on exit codes using findstr.exeand type.

I know that macros such as Windows macros, for example, %Var%are incompatible, but I assumed that since the only call I made was in the file .bat, the behavior .batwould be the only thing I needed to worry about.

If this is not the case, should my starter .batstart direct instance execution CMD.exewith my parameters? If so, what is the best way to do this, PowerShell-friendly?

+4
source share
3 answers

eryksun's comments on this subject deserve attention.

. . , OP.

, , PowerShell:

  • (.bat .cmd) , , script_name.bat

    • , ( script_name, ) , :
      • :
        • , , /PowerShell script (*.ps1), , $env:PATH (%PATH%); ( ) , .
      • :
        • PowerShell script (*.ps1) , .bat .cmd %PATHEXT%.
  • , ./

    • PowerShell - cmd.exe - , , , script_name.bat . [1]

    • , -, , - ./ (.\, Windows); , ./script_name.bat.

  • :

    • : PowerShell, - . .
    • : --% (PSv3 + ), , ( PowerShell, %<var>% -style environment-variable).

[1] eryksun , Vista + cmd PowerShell, NoDefaultCurrentDirectoryInExePath ( ). , , , . %PATH%/$env:PATH; ., cmd.


:

Windows Creator, PowerShell Windows

:

  • Win-X ( ) PowerShell, cmd , .

  • " " File Open Windows PowerShell Open command prompt (cmd).

, / : HKEY_CLASSES_ROOT\batchfile HKEY_CLASSES_ROOT\cmdfile shell\open "%1" %*, cmd /c, .

, , :

  • cmd, cmd Run, Win-R ( ).

    • -: cmd.
  • PowerShell, "" File.

    • PowerShell :

      • , .
      • , -, .
    • PowerShell .

    • , ( ) , , (, cd /d "%~dp0").

+4

, . , shim - .

erykson, , . , script CMD PowerShell, Jeff Zeitlin .

, , script_name.bat.

script_name.bat script, . , , script_name.bat, CMD PowerShell, :

  • script_name.bat script_name_shim.bat
  • script_name.bat
  • :

    @echo off
    CMD.exe /C "%~dp0script_name_shim.bat" %*
    exit /b %errorlevel%
    

script CMD.exe, , PowerShell, .

+1

, , , .
cmd.exe win10cu.

PowerShell , cmd.exe.
/ .

  • PowerShell script Help.exe Get-Command .
  • PoSh ErrorAction SilentlyContinue.
  • Applications (* .exe files) are considered identical and are removed using the where clause.

help.exe |
  Select-String '^[A-Z][^ ]+'|
    ForEach-Object {
      Get-Command $_.Matches.Value -ErrorAction SilentlyContinue
    }| Where-Object CommandType -ne 'Application'|Select *|
       Format-Table -auto CommandType,Name,DisplayName,ResolvedCommand,Module

An example output on my system, all of these elements are likely to work differently in PowerShell:

CommandType Name   DisplayName            ResolvedCommand Module
----------- ----   -----------            --------------- ------
      Alias call   call -> Invoke-Method  Invoke-Method   pscx
      Alias cd     cd -> Set-LocationEx   Set-LocationEx  Pscx.CD
      Alias chdir  chdir -> Set-Location  Set-Location
      Alias cls    cls -> Clear-Host      Clear-Host
      Alias copy   copy -> Copy-Item      Copy-Item
      Alias del    del -> Remove-Item     Remove-Item
      Alias dir    dir -> Get-ChildItem   Get-ChildItem
      Alias echo   echo -> Write-Output   Write-Output
      Alias erase  erase -> Remove-Item   Remove-Item
      Alias fc     fc -> Format-Custom    Format-Custom
   Function help                                          pscx
      Alias md     md -> mkdir            mkdir
   Function mkdir
   Function more
      Alias move   move -> Move-Item      Move-Item
   Function Pause
      Alias popd   popd -> Pop-Location   Pop-Location
   Function prompt
      Alias pushd  pushd -> Push-Location Push-Location
      Alias rd     rd -> Remove-Item      Remove-Item
      Alias ren    ren -> Rename-Item     Rename-Item
      Alias rmdir  rmdir -> Remove-Item   Remove-Item
      Alias set    set -> Set-Variable    Set-Variable
      Alias sc     sc -> Set-Content      Set-Content
      Alias sort   sort -> Sort-Object    Sort-Object
      Alias start  start -> Start-Process Start-Process
      Alias type   type -> Get-Content    Get-Content
0
source

Source: https://habr.com/ru/post/1679663/


All Articles