[英]Running a psm1 PowerShell script in Command Prompt (cmd.exe)?
I know nothing of PowerShell, but I wanted to install this: https://www.powershellgallery.com/packages/lolcat/我对 PowerShell 一无所知,但我想安装这个: https://www.powershellgallery.com/packages/lolcat/
So, I start PowerShell as administrator, and:因此,我以管理员身份启动 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[, ...]]]
...
Nice, it works.很好,它有效。 So first I find where is the newly installed script:
所以首先我找到新安装的脚本在哪里:
PS C:\WINDOWS\system32> (Get-Module lolcat).Path
C:\Program Files\WindowsPowerShell\Modules\lolcat\1.0.7\lolcat.psm1
Ok, so now I want to try calling this from cmd.exe:好的,所以现在我想尝试从 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
Nope, does not work.不,不起作用。
Is it possible to call this PowerShell script from cmd.exe - and if so, how?是否可以从 cmd.exe 调用此 PowerShell 脚本 - 如果可以,如何?
The error is due to the fact that the -File
parameter from the powershell.exe excepts a .ps1
file.该错误是由于
-File
中的-File参数不包括.ps1
文件。
If you want to run C:\Program Files\WindowsPowerShell\Modules\lolcat\1.0.7\lolcat.psm1
from cmd, make a .ps1
script where you can write something like如果您想从 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
And then call this script from cmd.然后从 cmd 调用这个脚本。
The difference between .ps1
and .psm1
is explained here . .ps1
和.psm1
之间的区别在此处解释。
Eh, turned out to be less complicated then I thought - since it is a "module", just use the module name:嗯,结果并没有我想的那么复杂——因为它是一个“模块”,所以只需使用模块名称:
C:\Users>PowerShell.exe lolcat
Usage: lolcat [OPTION]... [FILE1[, FILE2[, ...]]]
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.