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 Reply
  • 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"

Children
  • $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):




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