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

Parents
  • Here's what your parameter might look like

  • Thanks mate. So assuming i provide the variables in the acceptable values rather than in the script.

    How do i then reference that variable in the code below? $AzureLocation wont be specified in the script? I see you provided Import-CSV $Workflow.Parameter("Input File") but not sure how that works with the $CSVImport i have below. 

    Sorry first time doing this in ARS so a tad lost. 

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

  • $CSVImport = $Workflow.Parameter("Input File")

    Likewise  your $AzureLocation could be something like this:

    $AzureLocation = $Workflow.Parameter("Azure Location") # Add another parameter to your workflow for this - see screen cap

  • If you don't want to use another script to supply these values to the workflow, then you can just make the parameters mandatory and configure nothing else.  When you manually launch the workflow, you will be prompted to supply these values.  The "read back" code in your script is per my examples regardless.

  • Thank you Jonny. 

    You have been really helpful in your explanations and this now runs on demand if needs be. 

    How could i run this as a scheduled task? I assume i would need four scheduled tasks with each one specifying the AazureLocation variable?

    function Run-Active-Roles ($Request)
    {
    
    Import-OneDrive_URL
    
    }
    
    function Import-OneDrive_URL {   
    
    $AzureLocation = $Workflow.Parameter("AzureLocation")
     
    $CSVImport = UNC-PATH-HERE\$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}
        }
    }

  • As I previously explained, the contents of a parameter can be determined by a script.  So if you setup your workflow to run every 15 minutes, you could setup the script for selecting Azure Location such that it selects a different location value depending on which part of the hour the workflow is running in.  So..

    00:00 - Azure Location 1

    00:15 - Azure Location 2

    00:30 - Azure Location 3

    etc.

  • Thanks mate. I am getting a bit lost with using another script to  obtain the values of $AzureLocation. The on demand side is fine and have that all good supplying the values at run time. 

    I see that i cant use the same script as it needs to be one for a scheduled task. Thats not really a problem. I am just struggling they get my head around how i pass a value through on the scheduled task. I know you said use another script that where im getting lost. do you have any examples i can look at? 

  • Step 1 - add the script below as a Policy Script into your AR Script Library:

    Function GenerateAzureLocationParameter($Request)

    {

    $HourQuarter = $(get-date).Minute # The number of minutes after the hour

    Switch ($HourQuarter)
    {

    {$HourQuarter -lt 15} {$AzureLocation = "Location 1"} # First quarter hour - 00:00 to 00:14

    {($HourQuarter -ge 15) -and ($HourQuarter -lt 30)} {$AzureLocation = "Location 2"} # Second quarter hour - 00:15 to 00:29

    {($HourQuarter -ge 30) -and ($HourQuarter -lt 45)} {$AzureLocation = "Location 3"} # Third quarter hour

    {$HourQuarter -ge 45} {$AzureLocation = "Location 4"} # Fourth quarter hour

    }

    # Return the Azure Location appropriate to the quarter of the hour we're in

    $AzureLocation

    }

    2.  In the definition of your Automation Workflow that is receiving the Parameter, create an Azure Location Parameter that looks like this (the path to your script might be different):




Reply
  • Step 1 - add the script below as a Policy Script into your AR Script Library:

    Function GenerateAzureLocationParameter($Request)

    {

    $HourQuarter = $(get-date).Minute # The number of minutes after the hour

    Switch ($HourQuarter)
    {

    {$HourQuarter -lt 15} {$AzureLocation = "Location 1"} # First quarter hour - 00:00 to 00:14

    {($HourQuarter -ge 15) -and ($HourQuarter -lt 30)} {$AzureLocation = "Location 2"} # Second quarter hour - 00:15 to 00:29

    {($HourQuarter -ge 30) -and ($HourQuarter -lt 45)} {$AzureLocation = "Location 3"} # Third quarter hour

    {$HourQuarter -ge 45} {$AzureLocation = "Location 4"} # Fourth quarter hour

    }

    # Return the Azure Location appropriate to the quarter of the hour we're in

    $AzureLocation

    }

    2.  In the definition of your Automation Workflow that is receiving the Parameter, create an Azure Location Parameter that looks like this (the path to your script might be different):




Children
  • Hi. I am just digging this post up again. Is there no other way of passing the scheduled task a variable other than setting it up to run at different times within a hour? I am at the point where i dont mind having 5 scheduled tasks as long as i can use the same script and just pass it a variable. I assume i cant use the parameters on the scheduled task? 

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