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"

  • 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

  • This is documented in the SDK under "Parameterizing Script Policies and Scheduled Tasks" Relaxed

  • Just a quick question. What is the point of the parameters options on the scheduled task GUI? whats that used for?

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

  • Thanks thats what was confusing me. The powershell code was to set the parameter but if i can just do it via the GUI and its the same thing then thats great. 

    Then just call it like $MyParameter = $Task.DirObj.Parameters.Value("Test_Parameter").Value

    I will check this out when i logon tomorrow. 

  • Hi. 

    So i have created a new scheduled task. Assigned it the AzureLocation as the parameter value on the task and given it a value. It runs correctly when executed. This is epic stuff. Would be amazing to only have a single script and single scheduled task that can take the different values for the AzureLocation but right now i will be happy to have a single script and four scheduled tasks each with a different value for AzureLocation.  

    So now i am set with either Automation work flows that can be run from the WI or scheduled tasks. 

    $AzureLocation = $Task.DirObj.Parameters.Value("AzureLocation").Value
    
    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 = "something.onmicrosoft.com"
        Thumbprint = "something"
    }
    
    #CSV Export Location
    $CSVExport = "C:\$AzureLocation-OneDrive.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
    
    }
    
    Export-OneDrive-URL

  • You could also consider creating multiple Parameters (AzureLocation1, AzureLocation2...) within a single Scheduled Task, each Parameter containing a value you'd like the script to consider, and then build a bit of extra logic at the beginning of the script to pick one.

Reply Children
No Data