【问题标题】:Powershell: running JScript scripts without logoPowershell:运行没有徽标的 JScript 脚本
【发布时间】:2014-10-10 09:37:17
【问题描述】:

我使用 nologo 选项将cscript.exe 设置为我的默认脚本主机。因此在cmd.exe 正如预期的那样,我得到了:

> ftype jsfile                                                      
jsfile="C:\Windows\System32\CScript.exe" //nologo "%1" %*           

> reg query HKCR\jsfile\Shell\Open\Command                                                   

HKEY_CLASSES_ROOT\jsfile\Shell\Open\Command                                                  
    (Default)    REG_EXPAND_SZ    "C:\Windows\System32\CScript.exe" //nologo "%1" %*     

> echo  WScript.Echo("Test Echo"); > test.js                        

> test.js                                                           
Test Echo              

但是,迁移到 Powershell:

> powershell -nologo                                                              
PS > .\test.js                 
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved. 

Test Echo  

CScript 似乎没有从 Powershell shell 获得//nologo。 我怎样才能解决这个问题?

更多用例

它也适用于:

PS > cscript.exe //nologo test.js
Test Echo  
PS > cmd /c test.js 
Test Echo  
PS > cmd /c test
Test Echo  

复制粘贴测试

有人认为这可能是一个错误。要检查它是否适用于您,您可以在具有提升权限的 PS 控制台中复制以下内容:

## Change the host to cscript nologo and store old setting
$jstype="Registry::HKEY_CLASSES_ROOT\jsfile\Shell\Open\Command"        
$jsvalue=("`"$env:SystemRoot\System32\CScript.exe`"" + ' //nologo "%1" %*') 
$old=(gp  -Path $jstype   )."(default)"
Set-Item -Path $jstype -value $jsvalue                                  

## The output should be the same (i.e. nologo!)
echo  'WScript.Echo("Test Echo");' > test.js
cmd /c test
.\test.js

## Restore old settings
Set-Item -Path $jstype -value $old

cmd /c test.\test.js 的输出是否相同?

【问题讨论】:

  • 如果你运行PS> cmd /c test.js 它是否有效?
  • 嗯。为我工作。
  • @walidtoumi:是的;另请参阅问题更新。
  • @antonio: 可能是一个错误......你可以使用像这样的包装器 function execute-js ($file) { cmd /c $file } 并像这样执行它 execute-js -file file.js
  • @walidtoumi:能否请您复制并粘贴我添加到问题中的测试并告诉我问题是否也适用于您?

标签: powershell jscript file-association wsh


【解决方案1】:

问题详情

Powershell 在文件关联方面似乎有一种奇怪的行为。
如果将以下 sn-p 粘贴到 提升权限的 PS shell 中:

$jstype="Registry::HKEY_CLASSES_ROOT\jsfile\Shell\Open\Command"
Set-Item -Path $jstype -value "cscript.exe total non sense"
echo  'WScript.Echo("Test Echo");' > test.js
cmd /c test.js

... 将 jsfile 与命令字符串 cscript.exe total non sense 相关联时,您会得到应得的错误。
但令人惊讶的写作:

.\test.js

像魅力一样工作。所以看起来 PS 只读取开放命令字符串的第一项(此处为 cscript.exe),然后将用户字符串(.\test.js)附加到它,完全无视开放字符串的其他元素,此处为 @ 987654326@。

确认粘贴:

Set-Item -Path $jstype -value "notepad.exe total non sense"
echo 'WScript.Echo("Test Echo");' > test.js
cmd /c test.js

cmd.exe打开记事本,建议创建文件“total non sense.txt”。

现在写:

.\test.js

根据我刚才所说,实际的命令运行将是notepad.exe .\test.js,实际上记事本现在将打开.\test.js

解决方案

无论这是否是一个错误,一个可能的解决方案是创建一个批处理脚本,使用所需的参数调用cscript.exe。我将其命名为C:\Windows\System32\cscript.cmd

要创建脚本和相关关联,请粘贴:

echo "@cscript.exe //nologo %*" | out-file -encoding ASCII C:\Windows\System32\cscript.cmd
$jstype="Registry::HKEY_CLASSES_ROOT\jsfile\Shell\Open\Command"
$jsvalue=("`"$env:SystemRoot\System32\cscript.cmd`"" + ' "%1" %*') 
Set-Item -Path $jstype -value $jsvalue                                  

如您所见,//nologo 已从注册表移至 cscript.cmd
请注意,echo "string" > 会生成一个 Unicode 文件,cmd.exe 不喜欢该文件,因此使用了 ASCII 过滤器。 打印新的 .js 打开命令:

cmd /c ftype jsfile

即:jsfile="C:\Windows\System32\cscript.cmd" "%1" %*

现在两个:

cmd /c test
.\test 

将产生:

Test Echo

没有横幅,请注意,在这两种情况下,您都不需要指定 .js 扩展名。

为简单起见,我将cscript.cmd 设置为直接运行cscript.exe。您可能想要指定其完整路径。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-05
    相关资源
    最近更新 更多