Discover how a bimodal integration strategy can address the major data management challenges facing your organization today.
Get the Report →Automate Slack Integration Tasks from PowerShell
Are you in search of a quick and easy way to access Slack data from PowerShell? This article demonstrates how to utilize the Slack Cmdlets for tasks like connecting to Slack data, automating operations, downloading data, and more.
The CData Cmdlets for Slack are standard PowerShell cmdlets that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time and bidirectional access to Slack.
PowerShell Cmdlets or ADO.NET Provider?
The Cmdlets are not only a PowerShell interface to Slack, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete Slack data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for Slack. To access Slack data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for Slack.
Once you have acquired the necessary connection properties, accessing Slack data in PowerShell can be enabled in three steps.
Slack uses the OAuth authentication standard. To authenticate using OAuth, you will need to create an app to obtain the OAuthClientId, OAuthClientSecret, and CallbackURL connection properties. See the Getting Started section of the help documentation for an authentication guide.
PowerShell
-
Install the module:
Install-Module SlackCmdlets
-
Connect:
$slack = Connect-Slack -OAuthClientId "$OAuthClientId" -OAuthClientSecret "$OAuthClientSecret" -CallbackURL "$CallbackURL"
-
Search for and retrieve data:
$ispublic = "True" $channels = Select-Slack -Connection $slack -Table "Channels" -Where "IsPublic = `'$IsPublic`'" $channels
You can also use the Invoke-Slack cmdlet to execute SQL commands:
$channels = Invoke-Slack -Connection $slack -Query 'SELECT * FROM Channels WHERE IsPublic = @IsPublic' -Params @{'@IsPublic'='True'}
ADO.NET
-
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for Slack\lib\System.Data.CData.Slack.dll")
-
Connect to Slack:
$conn= New-Object System.Data.CData.Slack.SlackConnection("OAuthClientId=MyOAuthClientId;OAuthClientSecret=MyOAuthClientSecret;CallbackURL=http://localhost:33333;InitiateOAuth=GETANDREFRESH") $conn.Open()
-
Instantiate the SlackDataAdapter, execute an SQL query, and output the results:
$sql="SELECT Id, Name from Channels" $da= New-Object System.Data.CData.Slack.SlackDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.id $_.name }
Update Slack Data
PowerShell
Update-Slack -Connection $Slack -Columns @('Id','Name') -Values @('MyId', 'MyName') -Table Channels -Id "MyId"
ADO.NET
$cmd = New-Object System.Data.CData.Slack.SlackCommand("UPDATE Channels SET IsPublic='True' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Slack.SlackParameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()
Insert Slack Data
PowerShell
Add-Slack -Connection $Slack -Table Channels -Columns @("Id", "Name") -Values @("MyId", "MyName")
ADO.NET
$cmd = New-Object System.Data.CData.Slack.SlackCommand("INSERT INTO Channels (IsPublic) VALUES (@myIsPublic)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Slack.SlackParameter("@myIsPublic","True")))
$cmd.ExecuteNonQuery()
Delete Slack Data
PowerShell
Remove-Slack -Connection $Slack -Table "Channels" -Id "MyId"
ADO.NET
$cmd = New-Object System.Data.CData.Slack.SlackCommand("DELETE FROM Channels WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Slack.SlackParameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject