Connect Workday to your favorite reporting tools without moving data.
Learn More →Automate Slack Integration Tasks from PowerShell
Are you looking for a quick and easy way to access Slack data from PowerShell? We show how to use the Cmdlets for Slack and the CData ADO.NET Provider for Slack to connect to Slack data and synchronize, automate, download, 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.
Cmdlets or ADO.NET?
The cmdlets are not only a PowerShell interface to the Slack API, 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.
After obtaining the needed connection properties, accessing Slack data in PowerShell consists of three basic 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 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 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 System.Data.CData.Slack.SlackParameter("@myId","001d000000YBRseAAH"))
$cmd.ExecuteNonQuery()
CodeProject