none
No CMSite provider available for PowerShell scripts RRS feed

  • 问题

  • I have built a simple script to find any updates for Windows Server in SCCM with a max run time of less than an hour, and sets their max run time to 1 hour. Because there is no way to set a default max run time for all updates my plan is for this script to run as a scheduled task once a day.

    #Load Configuration Manager PowerShell Module
    Import-Module ConfigurationManager
    
    #Get SiteCode and set Powershell Drive
    Set-location TWS:
    
    #Find updates for Windows Server with a max runtime of less than 60 minutes (3600 seconds) and set max runtime to 60 minutes
    Get-CMSoftwareUpdate -Fast | Where-Object {($_.LocalizedDisplayName -match "Server") -and ($_.MaxExecutionTime -le 3599)} | ForEach-Object {
        Set-CMSoftwareUpdate -Id $_.CI_ID -MaximumExecutionMins 60 }

    If I run this script manually (either in ISE or the console) it runs without issue. However if the script runs as a scheduled task this is my output:

    Set-location : Cannot find drive. A drive with the name 'TWS' does not exist.
    At SCCM_SetServerUpdatesMaxRuntime.ps1:9 char:1
    + Set-location TWS:
    + ~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (TWS:String) [Set-Location], DriveNotFoundException
        + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.SetLocationCommand

    As best I can tell, my SCCM site drive is created (or becomes available) after I run 'Import-Module ConfigurationManager' manually - but not when the script runs via a scheduled task. This is especially strange to me because I have edited the $env:PSModulePath variable to include the path to the Configuration Manager module, so I would have thought the module was loading automatically and shouldn't need to be called in the script anyway?

    Is there a way I can permanently have my SCCM site set as a location for PowerShell to access, or at least 'create' it as part of a scheduled script? Thanks.


    2019年11月1日 1:52

全部回复

  • Your Import-Module is almost certainly not actually finding the ConfigMgr module. A common line used to import the module is as follows:

    Import-module ($Env:SMS_ADMIN_UI_PATH.Substring(0, $Env:SMS_ADMIN_UI_PATH.Length - 5) + '\ConfigurationManager.psd1')


    Jason | https://home.configmgrftw.com | @jasonsandys

    2019年11月1日 4:01
  • Thanks for the reply - I have tried that too, with the same result. If I add -Verbose following Import-Module I can see the cmdlets are being imported as expected when the script runs via the scheduled task - there just seems to be something about running it manually vs. scheduled which prevents the CMSite from being established?
    2019年11月1日 5:38
  • What user context are you running the script in? does the account have access to Configuration Manager?

    For example running under system context to load the module or even just trying to call Microsoft.ConfigurationManagement.exe will fail.


    Richard Knight | Collection Refresh Manager | Automate detection rules for patch \ msp files | Twitter


    2019年11月1日 6:48
  • Hello,
     
    +1 to Richard Knight.
     
    If your scheduled task is running with the highest privileges, actually it is running under system context and importing module would fail as mentioned above. You could test it with psexec tool.
     
    If it is your case, try using a CM admin account instead then check if it works.
     
    Best Regards,
    Ray

    Please remembers to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    2019年11月1日 17:03
  • Hello,
     
    I noticed that you have not updated for several days. How are things going now? Is it resolved or is there any update? Feel free to feedback.
     
    Best Regards,
    Ray

    Please remembers to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    2019年11月4日 2:12
  • Hello,
     
    Here is a summary of your issue. Feel free to feedback.
     
     
    Issue Symptoms
    ===============
    A script works well when running manually, but not when running as a scheduled task.
     
    Possible Cause
    ===============
    The account under which the script is running may has not enough permissions.
     
    Suggestion
    ===============
    Try running scheduled task with administrative rights which means under system context.
     
     
    Best Regards,
    Ray

    Please remembers to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    2019年11月11日 1:48