Hello Everyone,
I am facing an issue with a PowerShell script where I have a function (Get-EncryptionData
) intended to return data as a DataTable. However, when I call this function from a parent script, the variable storing the result ($EncryptionSuccess
) is treated as a PowerShell object rather than a DataTable.
Here is a simplified description of the problem:
- The function retrieves data and processes it into a DataTable.
- I expect the variable
$EncryptionSuccess
to be of type DataTable, but it is being treated as a PowerShell object.
- This causes complications when attempting to use
$EncryptionSuccess
for bulk copy operations.
I've explored various approaches without success. Could you provide guidance on how to explicitly return data as a DataTable from the function, ensuring that the variable is of the correct type? Additionally, if there are conventions or methods specific to handling DataTables in PowerShell functions, any references or documentation would be greatly appreciated.
Thank you for your assistance.
function Get-EncryptionData {
try {
$response = Invoke-WebRequest -Uri $uri -Headers $headers -Method Get -ErrorAction Stop
$Encryption = ($response.Content | ConvertFrom-Json).value
$Encryption = $Encryption | Select-Object -Property id, deviceName, isEncrypted, userPrincipalName
# Create a new DataTable
$table = New-Object System.Data.DataTable
# Define columns
$table.Columns.Add("DeviceID", [string])
$table.Columns.Add("DeviceName", [string])
$table.Columns.Add("IsEncrypted", [bool])
$table.Columns.Add("UserPrincipalName", [string])
$Encryption | ForEach-Object {
# Add a new row to the DataTable
$row = $table.NewRow()
$row["DeviceID"] = $_.id
$row["DeviceName1"] = $_.deviceName
$row["IsEncrypted1"] = $_.isEncrypted
$row["UserPrincipalName1"] = $_.userPrincipalName
$table.Rows.Add($row)
}
return $table
}
catch {
write-host An error occurred: $($_.Exception.Message)
return $false
}
}
#Calling Encryption Script
. ("$Config_ScriptFolderPath\EncryptionStatus.ps1")
$EncryptionSuccess = Get-EncryptionData -ConfigFilePath $ConfigFilePath