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

Windows 在命令提示符(cmd.exe)中运行psm1 PowerShell脚本?,windows,powershell,cmd,Windows,Powershell,Cmd,我对PowerShell一无所知,但我想安装这个: 因此,我以管理员身份启动PowerShell,并且: PS C:\WINDOWS\system32> Install-Module -Name lolcat

我对PowerShell一无所知,但我想安装这个:

因此,我以管理员身份启动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脚本?如果可以,如何调用?

此错误是由于excepts a
.ps1
文件中的
-File
参数引起的

如果要从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
之间的区别。

呃,结果没有我想的那么复杂-因为它是一个“模块”,只需使用模块名称:

C:\Users>PowerShell.exe lolcat

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

...

您实际上并没有运行模块文件。当您安装模块并将其导入powershell会话时,将加载可从任何位置调用的功能。如果执行
Get命令-Module lolcat
,您将看到这些函数是什么。其中一个函数称为
lolcat
,但还有另外两个函数。我们的彩虹就是其中之一。在任何管道的末端使用,以获得一些彩色输出<代码>方向|出彩虹