命令之间的差异在.bat文件和powershell.exe中运行

时间:2018-01-11 20:42:16

标签: batch-file

我想下载一个scoop安装程序,我发现我的命令不能在.bat文件中运行,但在我将其复制/粘贴到PowerShell中时可以正常工作。

这是命令和图片,使事情变得非常清晰:

iex (new-object net.webclient).downloadstring('https://get.scoop.sh')

PowerShell的输出:

Enter image description here

为什么会出现这种情况,也许我们在将命令放入.bat文件时需要注意的事项?

2 个答案:

答案 0 :(得分:1)

PowerShell与Windows命令处理器(cmd.exe)共享一些命令。 PowerShell包含不在cmd.exe中的额外功能和命令。您的命令似乎正在使用PowerShell脚本。

默认情况下,.bAT文件将使用cmd.exe执行。您可以将文件扩展名更改为.ps1,然后默认使用powershell.exe。否则,您必须针对PowerShell显式运行该文件。

答案 1 :(得分:-1)

批处理文件通常由static void Main(string[] args) { var watcher = new FileSystemWatcher(); watcher.Path = @"C:\test_dir"; // watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName; watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName; watcher.Filter = "*.*"; watcher.Changed += new FileSystemEventHandler(OnChanged); watcher.Created += new FileSystemEventHandler(OnChanged); watcher.Deleted += new FileSystemEventHandler(OnChanged); watcher.Renamed += new RenamedEventHandler(OnRenamed); watcher.IncludeSubdirectories = true; watcher.EnableRaisingEvents = true; while (true) { } } private static void OnRenamed(object sender, RenamedEventArgs e) { Console.WriteLine($"OnRenamed: {e.FullPath}, {e.OldFullPath}"); } private static void OnChanged(object sender, FileSystemEventArgs e) { Console.WriteLine($"OnChanged: {e.ChangeType}, {e.Name}[{e.FullPath}]"); } 执行; PowerShell由CMD.EXE执行。两者是不同的,在一个中工作的脚本在另一个中不起作用。您粘贴到问题中的代码无疑是PowerShell,而不是批处理。您可能会发现查看Microsoft's PowerShell basics的某些内容很有用。

相关问题