Windows 8 cmdの変更の可能性はありますか?


5

何らかの理由で、開発マシンでWindows 8を試してみることにしました。これまでのところ、非常に良いことですが、Powershellを起動しようとするまで、いくつかのカスタマイズがあり、PATH変更をから引き出すなどvcvars32.bat、さまざまな開発ツールすべてにアクセスできます。

最初に、スクリプトはここからhttps://stackoverflow.com/questions/138144/whats-in-your-powershell-profile-ps1fileからプルされ、x64 Powershellインスタンス内で実行できるようにいくつかの変更が加えられました。このような:

function Get-Batchfile($file) 
{
    $theCmd = "`"$file`" & set" 
    cmd /c $theCmd | Foreach-Object {
        $thePath, $theValue = $_.split('=')
        Set-Item -path env:$thePath -value $theValue
    }
}

function VsVars32($version = "10.0")
{
    $theKey = "HKLM:SOFTWARE\Wow6432Node\Microsoft\VisualStudio\" + $version
    $theVsKey = get-ItemProperty $theKey
    $theVsInstallPath = [System.IO.Path]::GetDirectoryName($theVsKey.InstallDir)
    $theVsToolsDir = [System.IO.Path]::GetDirectoryName($theVsInstallPath)
    $theVsToolsDir = [System.IO.Path]::Combine($theVsToolsDir, "Tools")
    $theBatchFile = [System.IO.Path]::Combine($theVsToolsDir, "vsvars32.bat")
    write-host $theBatchFile
    Get-Batchfile $theBatchFile
    [System.Console]::Title = "Visual Studio " + $version + " Windows Powershell"
}

VsVars32

そして、それはWindows 7で完璧に機能しました。今、Windows 8で、私は以下を取り戻します:

'C:\Program' is not recognized as an internal or external command, operable program or batch file.

さらに調査すると、cmd /c動作の仕方で何かが変わったように見えます。cmd回線を単独で実行すると、次のようになります。

PS> cmd /c "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools\vsvars32.bat"
'C:\Program' is not recognized as an internal or external command, operable program or batch file.

誰かが同様の問題に遭遇しましたが、うまくいけば回避策がありますか?

編集:

以下の回答で述べたように、引用の問題がある可能性があります。私は投稿する前でさえこれを試みました、そして私のトラブルのためにこれを得ました:

PS> cmd /c ""C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools\vsvars32.bat""
x86 : The term 'x86' is not 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 that the path is correct and try again.
At line:1 char:28
+ cmd /c ""C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools\vsvar ...
+                            ~~~
    + CategoryInfo          : ObjectNotFound: (x86:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

引用のいくつかの異なる順列:

単一引用符で二重にラップ:

PS> echo '"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools\vsvars32.bat"'
"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools\vsvars32.bat"

PS> cmd /c '"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools\vsvars32.bat"'
'C:\Program' is not recognized as an internal or external command,
operable program or batch file.

エスケープされた二重引用符:

PS> echo "`"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools\vsvars32.bat`""
"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools\vsvars32.bat"

PS> cmd /c "`"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools\vsvars32.bat`""
'C:\Program' is not recognized as an internal or external command,
operable program or batch file.

もう一度:Windows 7でもまったく同じスクリプトが機能しました。


「C:\ progrma files(x86)\」というスペースの問題があると思いますか?
avirk

たぶん、括弧をエスケープする必要がありますか?それらを変数として扱っているようです。
ジョージダケット

Invoke-CmdScript.ps1は似たようなことをしますが、少しきれいに見えます。たぶん、それを試してみる:poshcode.org/2176
ジェイBazuzi

回答:


1

Windows Power Shellを使用している場合、パスにスペースがある場合は、 ""(二重二重引用符)を使用する必要があります。

""C:\Program Files""

例:cmd /c ""start cd 'C:\Program Files'""現在のディレクトリをとして新しいコマンドプロンプトを開きますC:\Program Files。二重引用符で囲まれていない場合、囲まれたスペース ''のためにエラーがスローされます。

編集:二重の単一引用符でも機能します。

編集:オプションで、 `文字(バッククォート/バックティック)を使用してスペースをエスケープできます。

cmd /c start cd C:\Program` Files

実は、私はそれをテストとして実行しただけです。渡されたコマンドラインcmd /cは変数に構築され、そこで引用符をエスケープします。さらに、述べたように、この同じスクリプトはWin7で正常に機能しました。
マットシーカー

はい、わかりました。渡されるコマンドラインを二重引用符で囲むことは可能ですか?うまくいけば、渡されたときに再囲まれますか?
12

0

この質問を読むことから:https : //stackoverflow.com/questions/6471320/how-to-call-cmd-exe-from-powershell-with-a-space-in-the-specified-commands-dire on stack overflow cmdは次の形式である必要があります

cmd.exe /c "`"$_cmd`""

最後に2つの二重引用符があることに注意してください。1つしかありません。


次の行で:

cmd /c "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools\vsvars32.bat"

たぶん、引用符でC:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools\vsvars32.bat囲まれた引数をqoutのない文字列として扱うため、

cmd /c """C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools\vsvars32.bat"""

つまり"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools\vsvars32.bat"、上記のような文字列として。


ただし、7で機能したのに8で機能しなかった理由についてはわかりません。そうでない場合は削除します。
ジョージデュケット

スニペットを最初に見たとき、同じことが少し私を投げました。実際にはvsvarsを実行し&、の直後にsetを実行します。そのため、渡されるコマンド全体は「[vsvars.batへのパス]&set」であり、その後、setから出てくるものを解析します。
マットシーカー

別の行の編集を参照してください
ジョージデュケット


0

質問を更新して、Win7とWin8の両方で実行しようとしているコマンドをエコーし​​ます。そうする過程で、私は何かに気づきました。Windows 7 PCにはVS2010とVS2012の両方がインストールされており、Win8ボックスにはVS2012しかありません。

$VersionVsVars32関数のデフォルト値を変更して、11.0すべてが再び機能するようにしました。さて、なぜこの問題のトラブルシューティングがはるかに簡単になったフルパスを印刷しなかったのかについては、非常に良い質問です。

弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.