我对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脚本吗?如果可以,如何调用?
发布于 2021-04-26 04:00:21
该错误是由于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调用此脚本。
here解释了.ps1
和.psm1
之间的区别。
发布于 2021-04-26 04:00:45
嗯,结果没有我想的那么复杂-因为它是一个“模块”,只需使用模块名称:
C:\Users>PowerShell.exe lolcat
Usage: lolcat [OPTION]... [FILE1[, FILE2[, ...]]]
...
https://stackoverflow.com/questions/67257599
复制相似问题