Account.Record.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#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 {}
              }