windows - 在命令提示符 (cmd.exe) 中运行 psm1 PowerShell 脚本?

标签 windows powershell cmd

我对 PowerShell 一无所知,但我想安装这个:https://www.powershellgallery.com/packages/lolcat/

因此,我以管理员身份启动 PowerShell,并且:

PS C:\WINDOWS\system32> Install-Module -Name lolcat                                                                                                                                                                                             NuGet provider is required to continue
PowerShellGet requires NuGet provider version '2.8.5.201' or newer to interact with NuGet-based repositories. The NuGet
 provider must be available in 'C:\Program Files\PackageManagement\ProviderAssemblies' or
'C:\Users\Me\AppData\Local\PackageManagement\ProviderAssemblies'. You can also install the NuGet provider by running
'Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force'. Do you want PowerShellGet to install and import
 the NuGet provider now?
[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"): y

Untrusted repository
You are installing the modules from an untrusted repository. If you trust this repository, change its
InstallationPolicy value by running the Set-PSRepository cmdlet. Are you sure you want to install the modules from
'PSGallery'?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): y
PS C:\WINDOWS\system32> lolcat

Usage: lolcat [OPTION]... [FILE1[, FILE2[, ...]]]
...

不错,有效。所以首先我找到新安装的脚本在哪里:

PS C:\WINDOWS\system32> (Get-Module lolcat).Path
C:\Program Files\WindowsPowerShell\Modules\lolcat\1.0.7\lolcat.psm1

好的,现在我想尝试从 cmd.exe 调用它:

C:\Users>PowerShell.exe -File "C:\Program Files\WindowsPowerShell\Modules\lolcat\1.0.7\lolcat.psm1"
Processing -File 'C:\Program Files\WindowsPowerShell\Modules\lolcat\1.0.7\lolcat.psm1' failed because the file does not have a '.ps1' extension. Specify a valid Windows PowerShell script file name, and then try again.
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Try the new cross-platform PowerShell https://aka.ms/pscore6

不,不起作用。

是否可以从 cmd.exe 调用此 PowerShell 脚本 - 如果可以,如何调用?

最佳答案

该错误是由于 powershell.exe 中的 -File 参数造成的。 .ps1 文件除外。

如果您想从 cmd 运行 C:\Program Files\WindowsPowerShell\Modules\lolcat\1.0.7\lolcat.psm1,请创建一个 .ps1 脚本,其中你可以写类似的东西

Import-Module lolcat

# now you have all the functions from the lolcat module loaded into this PowerShell session

# do stuff

然后从 cmd 调用此脚本。

解释.ps1.psm1之间的区别here .

关于windows - 在命令提示符 (cmd.exe) 中运行 psm1 PowerShell 脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67257599/

相关文章:

windows - 如何在 Windows 上使用 MinGW 构建 OpenCV 3.2.0

asp.net - Powershell Invoke-WebRequest 表单发布

python - 当我在 Python 中使用命令时,命令的运行与预期不同

windows - 如何判断文件夹是否与 GoLang 或 CMD 共享?

.net - 检测 Windows 机器是否正在运行病毒扫描程序?

javascript - 在 Windows 中不使用 node-sass 生成 CSS 文件

windows - R 脚本的 GUI 前端

exception - 电源外壳 2 : How to determine what exceptions a cmdlet can throw?

powershell - 为什么在PowerShell中不能多次读取KeyPress?

vbscript - VBScript执行成功后如何关闭CMD提示窗口