-
Notifications
You must be signed in to change notification settings - Fork 0
/
Import-Products.ps1
101 lines (87 loc) · 3.03 KB
/
Import-Products.ps1
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
Import-Module sqlserver
$clientId = "a8494215-ace2-4c77-b84c-f13cbe1b6c2b"
$clientSecret = "ifUCBb/qU8v88m00sQtQ9HRCW43xPuaQ1/ivozFmCt0="
$scopes = "App.Elements.ReadWrite.All"
$authorization = Invoke-RestMethod `
-Method Post `
-Uri "http://bps.lumenn.local/api/oauth2/token" `
-ContentType "application/x-www-form-urlencoded" `
-Body "grant_type=client_credentials&client_id=$clientId&client_secret=$clientSecret&scope=$scopes"
$token = $authorization.access_token
$results = Invoke-SqlCmd -Query @"
SELECT
ProductKey,
ProductAlternateKey,
EnglishProductName,
ISNULL(ListPrice, 0) AS ListPrice,
CASE
WHEN Status IS NULL THEN 0
WHEN Status = 'Current' THEN 1
END AS Active
FROM
AdventureWorksDW2019.dbo.DimProduct
"@ -ServerInstance "localhost\SQLEXPRESS" -Verbose -TrustServerCertificate
$lumennBuisnessEntityGUID = 'E554D815-F958-463A-B4DD-E2EB29B29FF2'
$productWorkflowGUID = '2660ca16-457d-432f-8b43-beb282ab999a'
$productFormGUID = '3d9819ff-573a-4d1a-b424-45652e963079'
$pathActiveGUID = 'c6a440c1-51ce-4aa4-a2f3-39cb691f2e88'
$pathBlockedGUID = 'abc2a33f-5bd3-4ba2-85d4-2b9aae166ae2'
$formFieldGUIDs = @{
name = '7ffc9b32-ad57-4939-af60-d1ab29f6c01c';
price = '669e369c-1546-4560-9794-44d46b697416';
erpID = '673a9f06-055f-40b9-b6b6-57b4158db863';
productKey = '8b54d6c3-a340-4909-b435-f62cf0004eb7';
}
$databaseId = 1
$apiVersion = "v5.0"
$i = 1
$errors = New-Object System.Collections.Generic.List[System.Object]
foreach($row in $results) {
$requestBody = @{
workflow = @{
guid = $productWorkflowGUID;
}
formType = @{
guid = $productFormGUID;
}
formFields = @(
@{
guid = $formFieldGUIDs.name;
value = $row.EnglishProductName;
},
@{
guid = $formFieldGUIDs.price;
value = $row.ListPrice;
},
@{
guid = $formFieldGUIDs.erpID;
value = $row.ProductAlternateKey;
},
@{
guid = $formFieldGUIDs.productKey;
value = $row.ProductKey;
}
)
businessEntity = @{
guid = $lumennBuisnessEntityGUID
}
};
$body = ConvertTo-Json $requestBody -Depth 10
try {
$pathGUID = If ($row.Active) {$pathActiveGUID} Else {$pathBlockedGUID}
$response = Invoke-RestMethod `
-Method Post `
-Uri "http://bps.lumenn.local/api/data/$apiVersion/db/$databaseId/elements?path=$pathGUID" `
-Body $body `
-ContentType "application/json" `
-Headers @{Authorization = "Bearer $token"}
}
catch {
$errors.Add($row)
}
Write-Progress -Activity "Import in progress" -Status "$i out of $($results.Length)"
$i++;
}
if ($errors.Count -gt 0) {
$errors | Export-Csv -Path "$env:USERPROFILE\Downloads\ProductErrors.csv"
}