使用SSIS

时间:2017-05-30 08:15:55

标签: json powershell ssis

我们有一个JSON格式的Web服务(脚本)需要转换为csv文件并使用PowerShell删除到文件共享位置。 所有这些都需要在运行SSIS包时捕获。 怎么能实现这一目标?

# Set the filename to have the date suffix.
$fileName = "2758_RS-DM_AIRData" + (Get-Date -Format yyyyMMdd_hh) + ".csv"
$location = "C:\ExtractFilePowerShellWebService\Data"
New-Item -ItemType file -Name $fileName -Path $location 

# delete if file exist
$FileName2 = (Join-Path $location $fileName)
if (Test-Path $FileName2) {
  Remove-Item $FileName2

}

# Capture data from AIR Web Service

$webclient = new-object System.Net.WebClient
$webclient.Credentials = new-object System.Net.NetworkCredential("A", "B", "C")
$Json = $WebClient.Downloadstring("https://air.ciostage.accenture.com/ExternalServices/Application-service/Application(2758)")     
$jsonserial= New-Object -TypeName System.Web.Script.Serialization.JavaScriptSerializer 
$jsonserial.MaxJsonLength = [int]::MaxValue
$appCollection = @()
$appObj = $jsonserial.DeserializeObject($Json)
foreach($app in $appObj.value){

        $appObject = New-Object System.Object 
        $appObject | Add-Member -MemberType NoteProperty -Name "ApplicationId" -Value $app.ApplicationId 
        $appObject | Add-Member -MemberType NoteProperty -Name "ApplicationNm" -Value $app.ApplicationNm
        $appObject | Add-Member -MemberType NoteProperty -Name "Application Tier" -Value $app.ServiceLevelOfferingNm
        $appObject | Add-Member -MemberType NoteProperty -Name "Application Status" -Value $app.AppStatusNm
        #$appObject | Add-Member -MemberType NoteProperty -Name "Description" -Value $app.Description
        #$appObject | Add-Member -MemberType NoteProperty -Name "Acronym" -Value $app.Acronym
        #$appObject | Add-Member -MemberType NoteProperty -Name "Url" -Value $app.Url
        $appCollection += $appObject
}

#$appCollection | Export-Csv -Path "C:\ExtractFilePowerShellWebService\Data\2758_RS-DM_AIRData.csv" -notypeinformation

#extract data into CSV file.
$appCollection | Export-Csv  -Path (Join-Path $location $fileName) -notypeinformation 

当我运行脚本时,csv文件在提到的位置创建,但它是空的

1 个答案:

答案 0 :(得分:1)

我假设您的数据已经采用适合在csv中的格式,因为Json有很多csv无法显示的元素,我在此示例中使用ConvertTo-Json生成一个Json文件:

Get-Process | ConvertTo-Json -Depth 1 | Out-File .\file.json

请注意-Depth 1,这意味着不会嵌入任何嵌套数组等。

然后我可以使用ConvertFrom-Json和以下内容将其转换为csv:

(Get-Content .\file.json | ConvertFrom-Json) | Select Name,Handles,VM,PM | Export-Csv .\file.csv