简体   繁体   English

使用-Co​​mmand和-File开关从命令行(cmd.exe)运行Powershell.exe吗?

[英]Run Powershell.exe from command line (cmd.exe) with -Command and -File switches?

I have a simple module called MyModule.psm1 defined as 我有一个名为MyModule.psm1的简单模块,定义为

function Show-Text($p)
{
    Write-Host $p
}

Export-ModuleMember Show-Text

Module is saved here - ( All works fine, the normal way via ISE or direct Powershell command line ) 模块保存在此处-( 一切正常,可以通过ISE或直接通过Powershell命令行正常运行

C:\Windows\SysWOW64\WindowsPowerShell\v1.0\Modules\MyModule (because 64-bit OS)

Now, I have a script called Test.ps1 like this 现在,我有一个像这样的名为Test.ps1的脚本

Show-Text "Hello World"

NB: I do not want to do Import-Module MyModule in the ps1 file 注意:我不想在ps1文件中执行Import-Module MyModule

I am running following command from a cmd.exe window 我正在从cmd.exe窗口运行以下命令

C:\>Powershell -Command "& {Import-Module MyModule}" -File "C:\temp\Test.ps1"

I get the following error 我收到以下错误

Import-Module : The specified module 'MyModule' was not loaded because no valid module file was found in any module directory. Import-Module:未加载指定的模块'MyModule',因为在任何模块目录中均未找到有效的模块文件。 At line:1 char:17 + & {Import-Module <<<< MyModule} -File Show.ps1 + CategoryInfo : ResourceUnavailable: (MyModule:String) [Import-M odule], FileNotFoundException + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Comm 在第1行:char:17 +和{导入模块<<<< MyModule} -File Show.ps1 + CategoryInfo:ResourceUnavailable:(MyModule:String)[Import-M odule],FileNotFoundException + FullyQualifiedErrorId:Modules_ModuleNotFound,Microsoft.PowerShell .Comm
ands.ImportModuleCommand ands.ImportModuleCommand

Can somebody help me how to execute exactly what I have stated above via cmd.exe ONLY without hard-coding or specifying the full Module Path please ? 有人可以帮助我如何通过cmd.exe来执行exactly what I have stated上面exactly what I have stated ,而无需硬编码或指定完整的模块路径吗?

Something on the similar lines we do for loading SystemModules - powershell.exe -ImportSystemModules 我们在加载SystemModules时所做的类似操作powershell.exe -ImportSystemModules

You do understand that SysWOW64 is 32-bit and System32 is 64-bit right? 您知道SysWOW64是32位的,而System32是64位的吗?

The module is present in the 32-bit Powershell module lookup path, and hence will be seen only by the 32-bit Powershell console. 该模块位于32位Powershell模块查找路径中,因此只能由32位Powershell控制台看到。 If you call powershell from a 64-bit cmd console, you will open the 64-bit one. 如果从64位cmd控制台调用powershell,则将打开64位cmd控制台。

Either open a 32bit cmd prompt or move it to the 64-bit modules path, 打开32位cmd提示符或将其移至64位模块路径,

Are you running a 32-bit cmd.exe? 您正在运行32位cmd.exe吗? If so then the Import-Module should work. 如果是这样,则导入模块应该起作用。 If you are running a 64-bit cmd.exe, that will launch a 64-bit PowerShell.exe unless you fully qualify the path to the 32-bit version of PowerShell.exe (c:\\windows\\syswow64\\windowspowershell\\v1.0\\powershell.exe). 如果运行的是64位cmd.exe,则除非您完全限定PowerShell.exe的32位版本的路径(c:\\ windows \\ syswow64 \\ windowspowershell \\ v1),否则它将启动64位PowerShell.exe。 0 \\ powershell.exe)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM