最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

sql - PowerShell: convert date to MSGraph-compatible format - Stack Overflow

programmeradmin2浏览0评论

I have a problem regarding a date format that I need to convert to a compatible format to be able to create appointments in M365.

I have a PostgreSQL server connected with odbc that has data of a business application in it, for customer appointment.

The data is fetched into a System.Data.DataTable object.

The business application stores the date in Postgres like this:

Friday, 21. March 2025 08:00:00.

The column in Postgres is of datatype "timestamp without time zone". I cannot alter that at all. It has to stay that way.

The format needed to create a appointment with MS Graph is:

2017-04-15T14:00:00

So basically I need the name of the day cut out and the date converted.

I know how to do it with PHP, but cannot find anything to convert it with PowerShell.

I tried:

# Define the Application (Client) ID and Secret
$ApplicationClientId = '' 
$ApplicationClientSecret = ''
$TenantId = ''

# Convert the Client Secret to a Secure String
$SecureClientSecret = ConvertTo-SecureString -String $ApplicationClientSecret -AsPlainText -Force

# Create a PSCredential Object Using the Client ID and Secure Client Secret
$ClientSecretCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $ApplicationClientId, $SecureClientSecret

# Connect to Microsoft Graph Using the Tenant ID and Client Secret Credential
Connect-MgGraph -TenantId $TenantId -ClientSecretCredential $ClientSecretCredential

#Connect to Postgres SQL DB using ODBC Connection Sting
$conn = New-Object System.Data.Odbc.OdbcConnection
$conn.ConnectionString = "DSN=SHP"
$conn.Open()

#Query Event Information from Postgres SQL
$sql = 'select address.name as Mitarbeiter,address.globemail as Email,calendar.referenz as Auftragsnummer,calendar.location as Adresse,calendar.start as Termin from "00001".calendar calendar 
inner join "00001".adressen address
on calendar.ma_id = address.ku_nr'
$da = New-Object System.Data.Odbc.OdbcDataAdapter($sql,$conn)
$ds = New-Object System.Data.DataTable
$da.Fill($ds)
#$rowcount = $ds.rows
#$rowcount = ($ds | Measure-Object).Count
#echo $rowcount

foreach ($row in $ds) {

    $shpdate = $row.termin
    $provider = [CultureInfo]::InvariantCulture
    $shpdateformat = 'dddd dd,mmmm,yyyy HH:mm:ss'
    $graphdateformat = 'yyyy-m-ddTHH:mm:ss'

    $GraphDate = [DateTime]::ParseExact($shpdate, $shpdateformat, $provider)
    $GraphDate.ToString($graphdateformat)
}

But I get an error:

Exception when calling "ParseExact" with 3 arguments: "The string was not recognized as a valid DateTime." #GoogleTranslate

发布评论

评论列表(0)

  1. 暂无评论