Powershell Variables - Workflow Automation

Hi. 

I am looking to move some scripts over to ARS to run as a scheduled task. If we run this manually we are asked to select a location value which then maps to the correct CSV file in a specified location. 

Whats the best way of passing a value to the script at run time? I assume it would be four scheduled tasks based on how many locations we need to use? 

Thanks in advance 

function Run-Active-Roles ($Request)
{

Import-OneDrive_URL

}

function Import-OneDrive_URL {   
    param (   
        $AzureLocation = (Read-Host -Prompt 'Enter Azure location. GBR or CHE or APC or NAM')   
)

#Import CSV
$CSVImport = "C:\ARS-Scripts\_OneDrive_Export\$AzureLocation-OneDrive-for-Business-Users.csv"

Connect-QADService -Service "ARS.FQDN_HERE" -Proxy
Import-Csv $CSVImport | ForEach-Object {

$CurrentUser = [string]$_.UserName
$OneDriveSiteUrlContents = [string]$_.OneDriveSiteUrl

$TargetUser = Get-QADUser -LdapFilter "(userprincipalname=$CurrentUser)" | select -expandproperty DN

Set-QADUser -Identity $TargetUser -ObjectAttributes @{"ARS_VA_NAME_HERE"=$OneDriveSiteUrlContents}
    }
}

  • Just wanting to ask for clarification here. Are you asking about using/accessing Parameters in Scheduled Tasks or Automation Workflows?

  • Hi Sorry. Did not mean to mix stuff up, was just thinking out loud.

    So i have a workflow that i am able to run and at run time pass it the variable information and this is working fine. I now wish to move this over to a scheduled task. If i can use the same workflow script then amazing or if i need to have a separate scheduled task script then so be it. I just need to be able to pass it a variable when the scheduled task script runs.  

  • This is the automation script so far. I need to pass the $AzureLocation variable at scheduled task run time.  I plan to run once a month but equally the script could be run at any time as a one off. Which is why i am not so keen on the 15 mins past each hour setup. 

    #Set Runtime Parameters
    $AzureLocation = $Workflow.Parameter("AzureLocation")
    
    switch ($AzureLocation) {
        'SERVER1' { $SPURL = 'https://DomainName-admin.sharepoint.com' }
        default   { $SPURL = "https://DomainName$AzureLocation-admin.sharepoint.com"}
    }
    
    #Connection Properties
    $CxParams = @{
        URL = $SPURL
        ClientID = "something"
        Tenant = "domainname.onmicrosoft.com"
        Thumbprint = "something"
    }
    
    #CSV Export Location
    $CSVExport = "c:\$AzureLocation-OneDrive-for-Business-Users.csv"
    
    #Connect to SharePoint Online
    Connect-PnPOnline @CxParams
    
    function Export-OneDrive-URL {  
    
    $Result=@()
    #Get all office 365 personal sites
    $oneDriveSites = Get-PnPTenantSite -IncludeOneDriveSites -Filter "Url -like '-my.sharepoint.com/personal/'"
    $oneDriveSites | ForEach-Object {
    $site = $_
    $Result += New-Object PSObject -property @{ 
    UserName = $site.Owner
    OneDriveSiteUrl = $site.URL
    }
    }
    
    $Result | Select UserName, OneDriveSiteUrl | where {![string]::IsNullOrWhiteSpace( $_.UserName)} | Sort-Object UserName |
    Export-Csv $CSVExport -NoTypeInformation -Encoding UTF8 -Force
    
    }

  • I'm curious as to what you see the benefit is of having this script as a scheduled task vs. as an Automation Workflow.

    Also, has your use case changed?  When we first started discussing this, you seemed to need your script to run regularly across all of your various locations hence why I suggested that in each quarter hour, it act upon a different one.  Being able to run the script ad hoc against any particular location is not out of the question by any means - some additional logic just needs to be added to allow you to specify your ad hoc location parameter and have the script act upon that immediately.

  • And to further what JohnnyQuest indicated, the Parameters defined in a Scheduled Task are only static values, where Parameters in Automation Workflows can be populated via a script, making this more useful for what appears to be your intended use case.

    And to answer one of your questions above, Scheduled Task scripts are not Policy or Library Scripts. So, if you wanted to go the route of Scheduled Tasks, the scripts you might be using in any workflows would need to be recreated/duplicated as a Scheduled Task script.

  • Hi guys. So I am trying to move most of the power shell scripts we have knocking around under the controL of ARS. Some scripts we run manually and some need to run on a schedule. For augment sake the script above exports the one drive url from Azure for each user in a different geo location. Then an hour later a import script comes along and imports the URL in to ARS virtual attribute. 

    This won’t be the only script I have where I need to pass it a variable when the scheduled task runs. I tried using the scheduled task parameter for $AzureLocation and setting it to a value but it never worked. I just need to work out a method for passing variables at scheduled run time that is consistent to setup. I am so comfortable with doing this with automation workflow but I feel lost at the moment with a scheduled task. 

  • I am happy to duplicate any workflow script to a scheduled task if needs be. As long as I can pass it a variable value either dynamic or static I don’t mind. 

  • But you're missing the point Slight smile- AR Scheduled Tasks don't accept dynamic / ad hoc parameters, only workflows do.  In a workflow, you can even use a script to calculate the parameter if you want.  That's how dynamic they can be.

  • So there is no way at all a scheduled task can accept a value to a variable? If that’s the case then I will just have to have four scripts all doing the same thing just with a different value hard coded in the script. Seems such overkill. 

  • There is a property on the Scheduled Task where you can store parameters.

    edsaParameters

    Each Parameter is an XML object that looks like this:

    <Test_Parameter>Foo</Test_Parameter>

    So now you are going to ask how to assign these...

    Set-QADObject -proxy -Identity <Scheduled Task Distinguished Name> -ObjectAttributes @{edsaParameters="<Test_Parameter>Foo</Test_Parameter>"}

    ...it's a multi-value property so if you need to assign multiple parameters, you are going to have to figure out how to append values.Relaxed

    And now you need to know how to read them back in your script...

    $MyParameter = $Task.DirObj.Parameters.Value("Test_Parameter").Value