Establishing a Connection
With the CData Cmdlets users can install a data module, set the connection properties, and start scripting. This section provides examples of using our Kintone Cmdlets with native PowerShell cmdlets, like the CSV import and export cmdlets.
Installing and Connecting
If you have PSGet, installing the cmdlets can be accomplished from the PowerShell Gallery with the following command. You can also obtain a setup from the CData site.
Install-Module KintoneCmdlets
The following line is then added to your profile, loading the cmdlets on the next session:
Import-Module KintoneCmdlets;
You can then use the Connect-Kintone cmdlet to create a connection object that can be passed to other cmdlets:
$conn = Connect-Kintone -User 'myuseraccount' -Password 'mypassword' -URL 'http://subdomain.domain.com' -GuestSpaceId 'myspaceid'
Connecting to Kintone
In addition to the authentication values, set the following parameters to connect to and retrieve data from Kintone:
- Url: The URL of your account.
- GuestSpaceId: Optional. Set this when using a guest space.
Authenticating to Kintone
Kintone supports the following authentication methods.
Password Authentication
You must set the following to authenticate to Kintone:
- User: The username of your account.
- Password: The password of your account.
- AuthScheme: Set AuthScheme to Password.
API Token
You must set the following to authenticate to Kintone:
- APIToken: The API Token.
To generate an API token access the specific app and click on the cog wheel. Proceed to App Settings tab > API Token. Click on the Generate button, an API token will be generated. You can also specify multiple comma-seperated APITokens.
- AppId: The Application Ids.
The AppId is the number of that specific app in the sequence under Apps in Kintone UI dashboard. You can also specify multiple comma-seperated AppIds.
- AuthScheme: Set AuthScheme to APIToken.
Additional Security
In addition to the mentioned authentication schemese, Kintone offers additional security in the form of both an additional Basic Auth header, and an SSL Certificate.
Using Client SSL
In addition to your authentication information, Kintone may be configured to require an SSL certificate to accept requests. To do so, set the following:
- SSLClientCert: The file containing the certificate of the SSL Cert. Or alternatively, the name of the certificate store for the client certificate.
- SSLClientCertType: The type of certificate.
- SSLClientCertSubject: (Optional) If searching for a certificate in the certificate store, the store is searched for subjects containing the value of the property.
- SSLClientCertPassword: If the certificate store is of a type that requires a password, this property is used to specify that password to open the certificate store.
Basic
Kintone environments using basic authentication will need to pass additional basic credentials. To do so, specify the following:
- BasicAuthUser: The basic login name.
- BasicAuthPassword: The basic password.
Retrieving Data
The Select-Kintone cmdlet provides a native PowerShell interface for retrieving data:
$results = Select-Kintone -Connection $conn -Table "Comments" -Columns @("CreatorName, Text") -Where "AppId='1354841'"The Invoke-Kintone cmdlet provides an SQL interface. This cmdlet can be used to execute an SQL query via the Query parameter.
Piping Cmdlet Output
The cmdlets return row objects to the pipeline one row at a time. The following line exports results to a CSV file:
Select-Kintone -Connection $conn -Table Comments -Where "AppId = '1354841'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\myCommentsData.csv -NoTypeInformation
You will notice that we piped the results from Select-Kintone into a Select-Object cmdlet and excluded some properties before piping them into an Export-CSV cmdlet. We do this because the CData Cmdlets append Connection, Table, and Columns information onto each row object in the result set, and we do not necessarily want that information in our CSV file.
However, this makes it easy to pipe the output of one cmdlet to another. The following is an example of converting a result set to JSON:
PS C:\> $conn = Connect-Kintone -User 'myuseraccount' -Password 'mypassword' -URL 'http://subdomain.domain.com' -GuestSpaceId 'myspaceid' PS C:\> $row = Select-Kintone -Connection $conn -Table "Comments" -Columns (CreatorName, Text) -Where "AppId = '1354841'" | select -first 1 PS C:\> $row | ConvertTo-Json { "Connection": { }, "Table": "Comments", "Columns": [ ], "CreatorName": "MyCreatorName", "Text": "MyText" }
Deleting Data
The following line deletes any records that match the criteria:
Select-Kintone -Connection $conn -Table Comments -Where "AppId = '1354841'" | Remove-Kintone
Modifying Data
The cmdlets make data transformation easy as well as data cleansing. The following example loads data from a CSV file into Kintone, checking first whether a record already exists and needs to be updated instead of inserted.
Import-Csv -Path C:\MyCommentsUpdates.csv | %{ $record = Select-Kintone -Connection $conn -Table Comments -Where ("Id = `'"+$_.Id+"`'") if($record){ Update-Kintone -Connection $conn -Table Comments -Columns @("CreatorName","Text") -Values @($_.CreatorName, $_.Text) -Where "Id = `'$_.Id`'" }else{ Add-Kintone -Connection $conn -Table Comments -Columns @("CreatorName","Text") -Values @($_.CreatorName, $_.Text) } }