AR Powershell Script log

Hi Team.

When running your PS via ARS what are you using to log that the script has run and doing what you expect?

I have been using Start-Transcript and End-Transscript but the issue with that is that it seems to if there is a problem and it cant stop the transscript it  locks the log file up and it cant be deleted until the AR Service is restarted

Just looking for a better solution 

Thank in advance 

Parents
  • If you are working with a Script Module in Active Roles, logging is built-in. Check the properties on the script module, and you will see a Logging tab. You can enable logging and see line-by-line logging with variable assignments.

  • Thanks both.

    I think what i am looking for is something that will write the out put of the script running to a file. 

  • And that's exactly what my approach does.  It's a slightly fancier approach to "debug" logging.Slight smile

  • Thanks Johnny. I must not be following how your script works. 

    Here is an example script that i am working with. 

    How does your function fit in to that so that its capturing the output in the same way i would see it if i run it locally in Powershell console? 

    #Set Runtime Parameters
    #Obtain Azure Location from Scheduled Task parameter
    $AzureLocation = $Task.DirObj.Parameters.Value("AzureLocation").Value
    
    #Date
    $DateYear = $((Get-Date).ToString('yyyy'))
    $DateMonth = $((Get-Date).ToString('MMM'))
    $DateDay = $((Get-Date).ToString('dd'))
    
    #Create Folder structure based on Date
    New-Item -ItemType Directory -Path "path-here\$DateYear\$DateMonth\$DateDay" -Force
    
    #CSV Export location
    $CSVLocation = "CSVLocation-HERE\$DateYear\$DateMonth\$DateDay"
    
    $logfile = "$CSVLocation\Logs\$AzureLocation-Export_OneDrive-$time.txt"
    
    switch ($AzureLocation) {
        'GBR' { $SPURL = 'domain-here-admin.sharepoint.com' }
        default   { $SPURL = "https://domain-here$AzureLocation-admin.sharepoint.com"}
    }
    
    #Connection Properties
        $CxParams = @{
        URL = $SPURL
        ClientID = "ClientID-Here"
        Tenant = "Tenant-Name-Here"
        Thumbprint = "vert-thimbprint-here"
    }
    
    #CSV Export Location
    $CSVExport = "$CSVLocation\$AzureLocation-Temp_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
    
    #Tidyup CSV
    
    Import-Csv $CSVExport | 
        Where-Object{ $_.username -notmatch '@domain-name-here.onmicrosoft.com' } |
        Export-Csv -NoTypeInformation -Path "$CSVLocation\$AzureLocation-OneDrive-for-Business-Users.csv" -Force
        
        #Remove Temp Export
    
        Remove-Item -path "$CSVLocation\$AzureLocation-Temp_OneDrive-for-Business-Users.csv" -Force
    
    }
    
    Export-OneDrive-URL
    
    Disconnect-PnPOnline

  • I will take this one line as an example:

    #Create Folder structure based on Date
    New-Item -ItemType Directory -Path "path-here\$DateYear\$DateMonth\$DateDay" -Force

    Try
    {
    
    #Create Folder structure based on Date
    
    $NewPath = "path-here\$DateYear\$DateMonth\$DateDay"
    
    # My best practice is to put strings like this in [] so I can tell if they are null or not
    
    Logit "Attempting new folder creation [$NewPath]"  
    
    New-Item -ItemType Directory -Path $NewPath  -Force
    
    }
    Catch
    {
    
    Logit "Problem creating new path, OS Reported:"
    Logit $Error[0]
    }

Reply
  • I will take this one line as an example:

    #Create Folder structure based on Date
    New-Item -ItemType Directory -Path "path-here\$DateYear\$DateMonth\$DateDay" -Force

    Try
    {
    
    #Create Folder structure based on Date
    
    $NewPath = "path-here\$DateYear\$DateMonth\$DateDay"
    
    # My best practice is to put strings like this in [] so I can tell if they are null or not
    
    Logit "Attempting new folder creation [$NewPath]"  
    
    New-Item -ItemType Directory -Path $NewPath  -Force
    
    }
    Catch
    {
    
    Logit "Problem creating new path, OS Reported:"
    Logit $Error[0]
    }

Children