none
Run available task sequence using power shell using UIResource.UIResourceMgr.Executeprogram RRS feed

  • Question

  • Dear All

    We have a power shell script which is supposed to initiate the available task sequences on a client machine. I am using SCCM to deploy the power shell script as well. So it runs with SYSTEM account and fails to initiate the Task sequence.

    Any possible way to run this in user context. Thanks in advance

    I am using the below script which we referred from here

    Function Execute-TaskSequence {
        Param (
            [parameter(Mandatory = $true)]
            [string]$Name
        )
        Try {
            Write-Host "Connecting to the SCCM client Software Center..."
            $softwareCenter = New-Object -ComObject "UIResource.UIResourceMgr"
        }
        Catch {
            Throw "Could not connect to the client Software Center."
        }
        If ($softwareCenter) {
            Write-Host "Searching for deployments for task sequence [$name]..."
            $taskSequence = $softwareCenter.GetAvailableApplications() | Where { $_.PackageName -eq "$Name" }
            If ($taskSequence) {
                $taskSequenceProgramID = $taskSequence.ID
                $taskSequencePackageID = $taskSequence.PackageID
                Write-Host "Found task sequence [$name] with package ID [$taskSequencePackageID]."
                # Execute the task sequence
                Try {
                    Write-Host "Executing task sequence [$name]..."
                    $softwareCenter.ExecuteProgram($taskSequenceProgramID,$taskSequencePackageID,$true)
                    Write-Host "Task Sequence executed."
                }
                Catch {
                    Throw "Failed to execute the task sequence [$name]"
                }
            }
        }
    }

    Friday, April 26, 2019 9:44 AM

All replies

  • Hello,
     
    If you are deploying the scripts via "Run Script" function, then the it is running in system context and can not be changed.
     
    However, you could deploy it through Package and select "Run with user's rights" as Run Mode, then it should run in user context.
     

     
    Hope my answer could help you and look forward to your 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.

    Friday, April 26, 2019 10:25 AM
  • Thank you Ray for your response. The option you suggested should help me in another scenario where the installation is completely silent.

    Unfortunately,In this case we have couple of other functions in the same script which will provide user GUI. I ll not be able to run the complete the script with user rights as it may require elevated privileges as well.

    Luckily, I was able to set a task scheduler and initiate it from there with user access and it seems to work fine.

    I am sure there should be some other better approach to achieve this requirement. 

    Thanks again for your feedback.


    Friday, April 26, 2019 1:16 PM
  • Hello,
     
    Well, we could run command line in a Task sequence to start your script. It allow us to select a "run as" account.
     

     
    Could it help you?
     
    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.

    Friday, April 26, 2019 2:33 PM
  • No Ray. I want to run a separate script outside the task sequence.
    Monday, April 29, 2019 9:19 AM
  • Hello,
     
    What I means is that you could use above method to deploy your script. Thus your script could be running in user context and running as the specific account which has enough rights.
     
    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.

    Monday, April 29, 2019 9:30 AM