Stig
Stig

Reputation: 87

Powershell modules in scheduled task

There is something about modules I don't quite get....

If I as a normal user do

get-module -listavailable 

I get a result like this:

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

and that's what I expect...

But when doing the same from a scheduled task (with a different user) I get this:

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...

Why is there a difference between those two?

I'm stumped as the module I'm in fact interested in is the modules I have created myself and put into the folder:

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

Which seems to be working fine, except for running via scheduled tasks.

What am I missing? What have I forgot?

Further - I can confirm the $env:PSModulePath is the same for both:

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\

Except for the user path of course.

Upvotes: 2

Views: 4592

Answers (2)

Stig
Stig

Reputation: 87

Found the problem...

It had nothing to do with ExecutionPolicy - nor the direct path couldn't load the module either...

It was much simpler, once it was found...

the problem is 64/32 bit....

the modules was located in C:\Windows\System32\WindowsPowerShell\v1.0\Modules

however the task was executing

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

and therefor the modules should be located in C:\Windows\SysWOW64\WindowsPowerShell\v1.0\Modules

But thanks for your attention.

Upvotes: 3

Stephen Connolly
Stephen Connolly

Reputation: 1639

Relying on module autodiscovery is brittle. You script should define explicitly all the modules it requires and then load them explicitly.

If a module isn't listed, it might be because it isn't signed and the execution policy won't allow it to load or be discovered. Try explicitly loading one of the modules and see what error you get.

Upvotes: 1

Related Questions