计划任务中的Powershell模块

计划任务中的Powershell模块,powershell,scheduled-tasks,powershell-module,Powershell,Scheduled Tasks,Powershell Module,关于模块有一些我不太明白的地方 如果我作为一个普通用户 get-module -listavailable 我得到的结果如下: Directory: C:\Windows\system32\WindowsPowerShell\v1.0\Modules ModuleType Name ExportedCommands ---------- ---- ----------

关于模块有一些我不太明白的地方

如果我作为一个普通用户

get-module -listavailable 
我得到的结果如下:

Directory: C:\Windows\system32\WindowsPowerShell\v1.0\Modules

ModuleType Name                                ExportedCommands
---------- ----                                ----------------
Manifest   ADRMS                               {Update-ADRMS, Unins
Manifest   AppLocker                           {Set-AppLockerPolicy
Manifest   BestPractices                       {Get-BpaModel, Invok
Manifest   BitsTransfer                        {Add-BitsFile, Remov
Manifest   CimCmdlets                          {Get-CimAssociatedIn
Script     DSV
Script     DSVAsset                            {Get-HTMLPage, Get-H
Script     DSVDB                               {Execute-UpdateULLoC
Script     DSVHnas                             {Get-HNASFileScan, B
Script     DSVLog                              {Start-DSVTranscript
Script     Experimental.IO                     {Where-Wildcard, Get
Manifest   FailoverClusters                    {Add-ClusterDisk, Ad
Script     ISE                                 {New-IseSnippet, Imp
Manifest   Microsoft.PowerShell.Diagnostics    {Get-WinEvent, Get-C
Manifest   Microsoft.PowerShell.Host           {Start-Transcript, S
Manifest   Microsoft.PowerShell.Management     {Add-Content, Clear-
Manifest   Microsoft.PowerShell.Security       {Get-Acl, Set-Acl, G
Manifest   Microsoft.PowerShell.Utility        {Format-List, Format
Manifest   Microsoft.WSMan.Management          {Disable-WSManCredSS
Script     Module                              {New-PSScript, New-G
Script     PSDiagnostics                       {Disable-PSTrace, Di
Script     PSFTP                               {Send-FTPItem, Recei
Binary     PSScheduledJob                      {New-JobTrigger, Add
Manifest   PSWorkflow                          {New-PSWorkflowExecu
Manifest   PSWorkflowUtility                   Invoke-AsWorkflow
Manifest   ServerManager                       {Get-WindowsFeature,
Manifest   TroubleshootingPack                 {Get-Troubleshooting
Manifest   WebAdministration                   {Start-WebCommitDela
这就是我所期望的

但是,当从计划任务(与其他用户)执行相同操作时,我得到以下结果:

Directory: C:\Windows\system32\WindowsPowerShell\v1.0\Modules

ModuleType Name                                ExportedCommands                
---------- ----                                ----------------                
Manifest   BitsTransfer                        {Add-BitsFile, Remove-BitsTra...
Manifest   CimCmdlets                          {Get-CimAssociatedInstance, G...
Script     ISE                                 {New-IseSnippet, Import-IseSn...
Manifest   Microsoft.PowerShell.Diagnostics    {Get-WinEvent, Get-Counter, I...
Manifest   Microsoft.PowerShell.Host           {Start-Transcript, Stop-Trans...
Manifest   Microsoft.PowerShell.Management     {Add-Content, Clear-Content, ...
Manifest   Microsoft.PowerShell.Security       {Get-Acl, Set-Acl, Get-PfxCer...
Manifest   Microsoft.PowerShell.Utility        {Format-List, Format-Custom, ...
Manifest   Microsoft.WSMan.Management          {Disable-WSManCredSSP, Enable...
Script     PSDiagnostics                       {Disable-PSTrace, Disable-PSW...
Binary     PSScheduledJob                      {New-JobTrigger, Add-JobTrigg...
Manifest   TroubleshootingPack                 {Get-TroubleshootingPack, Inv...
Manifest   WebAdministration                   {Start-WebCommitDelay, Stop-W...
为什么这两者之间有区别

我很困惑,因为我真正感兴趣的模块是我自己创建并放入文件夹中的模块:

C:\Windows\system32\WindowsPowerShell\v1.0\Modules
除了通过计划任务运行之外,它似乎工作正常

我错过了什么?我忘了什么

进一步-我可以确认,
$env:PSModulePath
对于以下两种情况都是相同的:

C:\Users\GRIT.SVC.IPPlan\Documents\WindowsPowerShell\Modules;
C:\Windows\system32\WindowsPowerShell\v1.0\Modules\;
C:\Program Files\Common Files\Microsoft Lync Server 2013\Modules\;
C:\Program Files\Quest Software\Management Shell for AD\;
C:\Program Files\Microsoft Monitoring Agent\Agent\PowerShell\;
C:\Program Files\System Center Operations Manager 2012\Powershell\

当然除了用户路径。

依赖模块自动发现是脆弱的。脚本应该显式定义它需要的所有模块,然后显式加载它们

如果某个模块没有列出,可能是因为它没有签名,并且执行策略不允许加载或发现它。尝试显式加载其中一个模块,看看会出现什么错误。

发现问题

它与ExecutionPolicy无关-直接路径也无法加载模块

一旦被发现,事情就简单多了

问题是64/32位

模块位于 C:\Windows\System32\WindowsPowerShell\v1.0\Modules

然而,任务正在执行

C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe

因此,模块应位于 C:\Windows\SysWOW64\WindowsPowerShell\v1.0\Modules


但谢谢您的关注。

啊,这可能是执行政策的原因。。明天我会测试它,我确实尝试了包含模块的完整路径,但仍然失败了——我想是同样的错误,但我不是100%确定。将于明天返回WIT不是ExecutionPolicy,已设置为unrestricted。您如何确定它运行的是64位版本而不是32位版本?