Was asked yesterday if I could write something that would periodically generate a CSV file of our BlackBerry users and their pin numbers. This "report" would be used for communication purposes in a Disaster situation. I looked on the BlackBerry server for a few minutes hunting for a way to do it from the server. No such luck. I then remembered that all the data resides in SQL Server diligently waiting to be queried. Following is a small script that I have scheduled to run once a week:
# Parse the DN to get the SAMAccountName
$SQLQuery = "SELECT DisplayName,PIN,CreationTime,MailboxSMTPAddr,
Right(MailboxDN,(LEN(MailboxDN)-CHARINDEX('s/cn=', MailboxDN)-4)) AS SamAccountName
ORDER BY DisplayName"
#Set up connection
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = YourServer; Database = YourDB; User ID=YourID; Password=YourPWD; Integrated Security = False"
# Setup SQL Command
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SQLQuery
$SqlCmd.Connection = $SqlConnection
# Create the table and export!
$SqlVMDataReader = $SqlCmd.ExecuteReader()
$SqlVMDataTable = New-Object System.Data.DataTable
$SqlVMDataTable Export-Csv 'L:\DRDocs\BBPins.csv' -noTypeInformation