Account.Record.psm1

#Requires -RunAsAdministrator
Set-StrictMode -Version Latest
<#
 .Synopsis
  It retrieves all Account entity CRM records by specifying Account entity LogicalName.
 
 .Description
    The Get-AccountRecord cmdlet lets you retrieve all Account entity CRM records from your CRM organization.
    This example retrieves an account record with two fields.If some of
    them are null value then they are not retrieved..
    The retrieve results is exported in csv and has two fields like name and Address and maintain logs.
    The field with "Address" contains account entity address information,
    and another field "name" contains entity name value..
      
    The output also contains custom csv headers.
 
    You can use Get-CrmEntityAttributes cmdlet to see all fields logicalname.The retrieved data can be passed to several cmdlets like Set-CrmRecord,
    Removed-CrmRecord to further process it.
 
 .Parameter AccountEntityLogicalName
  This parameter takes input from the user.The input should be Account entity logical name.
 
 .Example
   This example retrieves all account record with AccountName and Address fields in csv file.
   Get-AccountRecord
 
 .Example
   This example retrieves all account record with name and address fields.
   If the value of the field is null, then it won't be returned in the csv field.
   Get-AccountRecord -AccountEntityLogicalName account
#>

        function Get-AccountRecord
            {
                [CmdletBinding()]
                param(
                    [Parameter(Mandatory=$True, Position=0, ValueFromPipeline=$false)]
                    [System.String]
                    $AccountEntityLogicalName
                     )
                 begin {
                     Connect-CrmOnline  
                     $CurrentDate = Get-Date
                     $CurrentDate = $CurrentDate.ToString('MM-dd-yyyy hh:mm:ss')
                        }
                process{
                    try{
                     $account = Get-CrmRecords -EntityLogicalName $AccountEntityLogicalName -FilterAttribute address1_line1 -FilterOperator "not-null" -FilterValue " " -Fields "name","address1_line1"
                     $account.CrmRecords |Select-Object @{ expression={$_.name}; label='Names' }, @{ expression={$_.address1_line1}; label='Address' } | Export-Csv -NoTypeInformation -Path "~\Desktop\accounts.csv"
                     $CurrentDate + " Account Entity records retrieved" | Add-Content '~\Desktop\Success.log'
                     " " | Add-Content '~\Desktop\Success.log'
                        }
                catch [System.Management.Automation.RuntimeException]
                        {
                     $CurrentDate + " An Error has occured" | Add-Content '~\Desktop\Success.log'
                     $CurrentDate + $_.Exception.Message | Add-Content '~\Desktop\Error.log'
                        }
                        }
                 end {}
              }