none
Run .Net Script works in Runbook Tester Only RRS feed

  • Question

  • I am trying to run a very simple Powershell script to wipe out every direct member of a collection (based on resource ID not machine name). It runs just fine in rubook tester but does not work once I check in the runbook and hit run. The runbook shows it starts but never finishes. Any suggestions? Here is the PS Script:

    Powershell {
    Import-Module 'C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1'
    cd <Site Code>:\
    $CollectionName = "Orchestrator Test"
    $DirectMembers = Get-CMDeviceCollectionDirectMembershipRule -CollectionName "$CollectionName"

    foreach ($DirectMember in $DirectMembers)
        {
        Remove-CMDeviceCollectionDirectMembershipRule -CollectionName "$CollectionName" -ResourceId $DirectMember.ResourceID -Force
        }
    }

    I have verified my runbook service account is admin on the box, and has SCCM Permissions.

    • Edited by jorlando82 Wednesday, June 10, 2015 7:35 PM correction
    Wednesday, June 10, 2015 4:59 PM

Answers

  • Found the problem! The import-module was not connecting the drive I had to add:

    new-psdrive -Name "<Site Code>" -PSProvider "AdminUI.PS.Provider\CMSite" -Root "<Primary Site Server FQDN>"

    So my script now looks like this:

    Powershell {
    Import-Module 'C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1'
    new-psdrive -Name "<Site Code>" -PSProvider "AdminUI.PS.Provider\CMSite" -Root "<Primary Site Server FQDN>"
    cd <site code>:\
    $CollectionName = "James Orchestrator Test"
    $DirectMembers = Get-CMDeviceCollectionDirectMembershipRule -CollectionName "$CollectionName"

    foreach ($DirectMember in $DirectMembers)
        {
        Remove-CMDeviceCollectionDirectMembershipRule -CollectionName "$CollectionName" -ResourceId $DirectMember.ResourceID -Force
        }
    }

    • Marked as answer by jorlando82 Wednesday, June 10, 2015 7:38 PM
    Wednesday, June 10, 2015 7:38 PM

All replies

  • Found the problem! The import-module was not connecting the drive I had to add:

    new-psdrive -Name "<Site Code>" -PSProvider "AdminUI.PS.Provider\CMSite" -Root "<Primary Site Server FQDN>"

    So my script now looks like this:

    Powershell {
    Import-Module 'C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1'
    new-psdrive -Name "<Site Code>" -PSProvider "AdminUI.PS.Provider\CMSite" -Root "<Primary Site Server FQDN>"
    cd <site code>:\
    $CollectionName = "James Orchestrator Test"
    $DirectMembers = Get-CMDeviceCollectionDirectMembershipRule -CollectionName "$CollectionName"

    foreach ($DirectMember in $DirectMembers)
        {
        Remove-CMDeviceCollectionDirectMembershipRule -CollectionName "$CollectionName" -ResourceId $DirectMember.ResourceID -Force
        }
    }

    • Marked as answer by jorlando82 Wednesday, June 10, 2015 7:38 PM
    Wednesday, June 10, 2015 7:38 PM
  • Thank You Jorlando82. You answer helped me to resolve the issue :)

    Monday, March 28, 2016 10:54 AM