Trying to Array information into .csv file. I would like to put the member in a csv

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
cgjackson@yahoo.com
Posts: 7
Joined: Wed Mar 01, 2017 12:54 pm

Trying to Array information into .csv file. I would like to put the member in a csv

Post by cgjackson@yahoo.com » Thu May 25, 2017 8:11 am

  1. ##Powershell Test
  2.  
  3.  
  4.  
  5. ## parameter declarations
  6.  
  7. param (
  8.  
  9.     [string]$DateFrom = $( Read-Host "Input Last Date Updated, format YYYY-MM-DD" ),
  10.  
  11.     [string]$DateTo = $( Read-Host "Input Date to Update To (Leave blank for today), format YYYY-MM-DD" )
  12.  
  13. )
  14.  
  15.  
  16.  
  17. $DateTo = "_$DateTo";
  18.  
  19. $matches = @()
  20.  
  21.  
  22.  
  23. ## WebRequest Section. Deprecated, Use Invoke-WebRequest in PS 3.0 and higher
  24.  
  25. $HTTPRequest = "https://appstest.mclaneco.com/employee-photo-booth/rest/image/queries/update/activedirectory/$DateFrom$DateTo"
  26.  
  27.  
  28.  
  29. $WebRequest = [System.Net.WebRequest]::Create("$HTTPRequest")
  30.  
  31. $WebRequest.Method = "GET"
  32.  
  33. $WebRequest.ContentType = "application/json; charset=utf-8"
  34.  
  35. $Response = $WebRequest.GetResponse()
  36.  
  37. $ResponseStream = $Response.GetResponseStream()
  38.  
  39. $ReadStream = New-Object System.IO.StreamReader $ResponseStream
  40.  
  41. $Data=$ReadStream.ReadToEnd()
  42.  
  43.  
  44.  
  45. ## Filters Input and seperates each userId=employeeId, to a PSObject
  46.  
  47. $matches += $Data | Select-String -Pattern '([a-zA-Z0-9]{3,8})=([0-9]{9})' -AllMatches |
  48.  
  49. ForEach-Object {$_.matches} |  ForEach-Object {New-Object PSObject -Property @{
  50.  
  51. UsrID = $($_.groups[1]);
  52.  
  53. EmpID = $($_.groups[2]);
  54.  
  55. }};
  56.  
  57.  
  58.  
  59.  
  60.  
  61. Write-Host $HTTPRequest;
  62.  
  63.  
  64.  
  65.  
  66.  
  67. ## Checks to see if the photo directory is available for an employee ID, switch to production file path for final result
  68.  
  69. ForEach($match in $matches){
  70.  
  71.     $ChkFile = "\\vserver-appdata01.mclane.mclaneco.com\test_emp_photos$\Photos\$($match.EmpID)";
  72.  
  73.     $FileExists = Test-Path $ChkFile;
  74.  
  75.     if($FileExists -eq $True){
  76.  
  77.         Write-Host "$($match.UsrID) -> $($match.EmpID)"
  78.  
  79.  
  80.  
  81.         Write-Host "Has Image Dir";
  82.  
  83.     }
  84.  
  85.    
  86.  
  87. }
Attachments
dPhotoTest.ps1
Works with results window, but I am have problem getting it to a file.
(1.57 KiB) Downloaded 38 times

jvierra
Posts: 14135
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Trying to Array information into .csv file. I would like to put the member in a csv

Post by jvierra » Thu May 25, 2017 9:53 am

To get Json data as an object we would normally use "Invoke-WebRequest".

Locked