我对 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/