{"id":425,"date":"2018-02-19T03:23:18","date_gmt":"2018-02-19T03:23:18","guid":{"rendered":"http:\/\/www.ciraltos.com\/?p=425"},"modified":"2018-02-19T03:23:18","modified_gmt":"2018-02-19T03:23:18","slug":"write-data-from-powershell-to-azure-table-storage","status":"publish","type":"post","link":"https:\/\/www.ciraltos.com\/staging2\/write-data-from-powershell-to-azure-table-storage\/","title":{"rendered":"Write Data from PowerShell to Azure Table Storage"},"content":{"rendered":"<p><iframe loading=\"lazy\" src=\"https:\/\/www.youtube.com\/embed\/Q7eJa-Hy6Rk\" width=\"560\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><span data-mce-type=\"bookmark\" style=\"display: inline-block; width: 0px; overflow: hidden; line-height: 0;\" class=\"mce_SELRES_start\">\ufeff<\/span><\/iframe><\/p>\n<p>I worked on a project recently that wrote data from PowerShell into a CSV file.\u00a0 The goal was to do real time trending based on the output, but I ran into an issue with file locks as PowerShell and the other program competed for access to the CSV.\u00a0 That\u2019s when I got the idea to write to Azure Table Storage instead of to a CSV.\u00a0 The project didn\u2019t work out for other reasons, but I did work out how to write data into Azure Table Storage instead from PowerShell.\u00a0 This post is about how I did that.<!--more--><\/p>\n<p>The key to writing data to Table Storage is that it requires a Primary Key (pun intended.). This key consists of a time stamp (added on the server side), a partition key and a row key.\u00a0 The partition key is used for load balancing as partitions from the same table can be spread across different resources.\u00a0 The row key is a unique value added to the row.\u00a0 The data I\u2019m writing doesn\u2019t contain a unique value so instead I simply generate a new GUID and use that for the row key.\u00a0 More information can be found <a href=\"https:\/\/docs.microsoft.com\/en-us\/rest\/api\/storageservices\/understanding-the-table-service-data-model\" target=\"_blank\" rel=\"noopener\">here<\/a>.<\/p>\n<p>With that, let\u2019s configure the prerequisites.\u00a0 First, you will need an Azure Subscription and a storage account.\u00a0 Create a new table in the storage account, that is where the data will be written.\u00a0 You will also need Azure Storage Explore.\u00a0 This will be used to view date written to the Table.<\/p>\n<p>Azure Storage Explorer requires an Access Key from the storage account. Keep this secure, this is essentially a full control password for the storage account.\u00a0 Use this key and the storage account name to log into Azure Storage Explorer.<\/p>\n<p>Next, generate a new Shared Access Signature (SAS) for the Storage Account.\u00a0 Unlike the Storage Access Key. The SAS can be scoped to the type of storage, access rights, source IP and can have an expiration date.\u00a0 At a minimum, the key will need to be scoped for Table storage with write access.<\/p>\n<p>Now that prerequisites are set, let\u2019s move onto the script.\u00a0 The full version of the script can be found at my GitHub site <a href=\"https:\/\/github.com\/tsrob50\/PowerShellToTblStrg\" target=\"_blank\" rel=\"noopener\">here<\/a>.\u00a0 Start by defining the variables for your environment.\u00a0 This includes the data gathered previously from the Storage Account.\u00a0 There is also a Partition Key defined.\u00a0 This is a requirement for Azure Table Storage.\u00a0 This allows for load balancing as partitions can be split between resources.\u00a0 I also defined the array that will be used for the data writing into table storage.<\/p>\n<pre># Step 1, Set variables\r\n# Enter Table Storage location data \r\n$storageAccountName = '&lt;Enter Storage Account Here&gt;'\r\n$tableName = '&lt;Enter Table Name Here&gt;'\r\n$sasToken = '&lt;Enter SAS Token Here&gt;'\r\n$dateTime = get-date\r\n$partitionKey = 'Svr1PerfData'\r\n$processes = @()<\/pre>\n<p>Next create the connection context and connection string.\u00a0 This is what PowerShell will use to log in and update Table Storage<\/p>\n<pre># Step 2, Connect to Azure Table Storage\r\n$storageCtx = New-AzureStorageContext -StorageAccountName $storageAccountName -SasToken $sasToken\r\n$table = Get-AzureStorageTable -Name $tableName -Context $storageCtx<\/pre>\n<p>In this step I gather the data that will be written to the tale.\u00a0 I used a simple get-process command that returns the top 10 processes by CPU usage.\u00a0 The output from the command is added to the array.<\/p>\n<pre># Step 3, get the data \r\n$processes = get-process | Sort-Object CPU -descending | select-object -first 10<\/pre>\n<p>The last step loops through the array, writing data into the table.<\/p>\n<pre>foreach ($process in $processes) {\r\n Add-StorageTableRow -table $table -partitionKey $partitionKey -rowKey ([guid]::NewGuid().tostring()) -property @{\r\n 'Time' = $dateTime.ToString(\"yyyymmdd:hhmmss\")\r\n 'ProcessName' = $process.Name\r\n 'ID' = $process.Id\r\n 'CPUTime' = $process.TotalProcessorTime.Minutes\r\n 'Memory' = $process.WS \r\n } | Out-Null\r\n}<\/pre>\n<p>Run the script and go back into Storage Explorer.\u00a0 After refreshing the view, you should be able to see the data written into your Storage Table.<\/p>\n<p>That\u2019s all there is to it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\ufeff I worked on a project recently that wrote data from PowerShell into a CSV file.\u00a0 The goal was to do real time trending based on the output, but I ran into an issue with file locks as PowerShell and the other program competed for access to the CSV.\u00a0 That\u2019s when I got the idea &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"https:\/\/www.ciraltos.com\/staging2\/write-data-from-powershell-to-azure-table-storage\/\"> <span class=\"screen-reader-text\">Write Data from PowerShell to Azure Table Storage<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-global-header-display":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":""},"categories":[2,170],"tags":[192,9,191,190,20,189,39,188],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Write Data from PowerShell to Azure Table Storage - ciraltos<\/title>\n<meta name=\"description\" content=\"In this post and video I go over how I used PowerShell to write data into Azure Table Storage. I cover items such as setting up a storage account and a table to write data to, configure Azure Storage Explorer to connect to the storage account and view the data. I also go over generating a Shared Access Signature (SAS) to authenticate the script.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.ciraltos.com\/staging2\/write-data-from-powershell-to-azure-table-storage\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Write Data from PowerShell to Azure Table Storage - ciraltos\" \/>\n<meta property=\"og:description\" content=\"In this post and video I go over how I used PowerShell to write data into Azure Table Storage. I cover items such as setting up a storage account and a table to write data to, configure Azure Storage Explorer to connect to the storage account and view the data. I also go over generating a Shared Access Signature (SAS) to authenticate the script.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.ciraltos.com\/staging2\/write-data-from-powershell-to-azure-table-storage\/\" \/>\n<meta property=\"og:site_name\" content=\"ciraltos\" \/>\n<meta property=\"article:published_time\" content=\"2018-02-19T03:23:18+00:00\" \/>\n<meta name=\"author\" content=\"Travis Roberts\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@ciraltos\" \/>\n<meta name=\"twitter:site\" content=\"@ciraltos\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Travis Roberts\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.ciraltos.com\/staging2\/write-data-from-powershell-to-azure-table-storage\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.ciraltos.com\/staging2\/write-data-from-powershell-to-azure-table-storage\/\"},\"author\":{\"name\":\"Travis Roberts\",\"@id\":\"http:\/\/www.ciraltos.com\/staging2\/#\/schema\/person\/25391996d6cddfecd4d257162b7e373a\"},\"headline\":\"Write Data from PowerShell to Azure Table Storage\",\"datePublished\":\"2018-02-19T03:23:18+00:00\",\"dateModified\":\"2018-02-19T03:23:18+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.ciraltos.com\/staging2\/write-data-from-powershell-to-azure-table-storage\/\"},\"wordCount\":540,\"commentCount\":2,\"publisher\":{\"@id\":\"http:\/\/www.ciraltos.com\/staging2\/#\/schema\/person\/25391996d6cddfecd4d257162b7e373a\"},\"keywords\":[\"add-storagetablerow\",\"Azure\",\"get-azurestoragetable\",\"New-AzureStorageContext\",\"Powershell\",\"SAS\",\"storage\",\"Table Storage\"],\"articleSection\":[\"Azure\",\"PowerShell\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.ciraltos.com\/staging2\/write-data-from-powershell-to-azure-table-storage\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.ciraltos.com\/staging2\/write-data-from-powershell-to-azure-table-storage\/\",\"url\":\"https:\/\/www.ciraltos.com\/staging2\/write-data-from-powershell-to-azure-table-storage\/\",\"name\":\"Write Data from PowerShell to Azure Table Storage - ciraltos\",\"isPartOf\":{\"@id\":\"http:\/\/www.ciraltos.com\/staging2\/#website\"},\"datePublished\":\"2018-02-19T03:23:18+00:00\",\"dateModified\":\"2018-02-19T03:23:18+00:00\",\"description\":\"In this post and video I go over how I used PowerShell to write data into Azure Table Storage. I cover items such as setting up a storage account and a table to write data to, configure Azure Storage Explorer to connect to the storage account and view the data. I also go over generating a Shared Access Signature (SAS) to authenticate the script.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.ciraltos.com\/staging2\/write-data-from-powershell-to-azure-table-storage\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.ciraltos.com\/staging2\/write-data-from-powershell-to-azure-table-storage\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.ciraltos.com\/staging2\/write-data-from-powershell-to-azure-table-storage\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/www.ciraltos.com\/staging2\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Write Data from PowerShell to Azure Table Storage\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/www.ciraltos.com\/staging2\/#website\",\"url\":\"http:\/\/www.ciraltos.com\/staging2\/\",\"name\":\"ciraltos\",\"description\":\"cloud, technology and trends\",\"publisher\":{\"@id\":\"http:\/\/www.ciraltos.com\/staging2\/#\/schema\/person\/25391996d6cddfecd4d257162b7e373a\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/www.ciraltos.com\/staging2\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"http:\/\/www.ciraltos.com\/staging2\/#\/schema\/person\/25391996d6cddfecd4d257162b7e373a\",\"name\":\"Travis Roberts\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/www.ciraltos.com\/staging2\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/www.ciraltos.com\/staging2\/wp-content\/uploads\/2023\/03\/Logo-1.png\",\"contentUrl\":\"https:\/\/www.ciraltos.com\/staging2\/wp-content\/uploads\/2023\/03\/Logo-1.png\",\"width\":5657,\"height\":3563,\"caption\":\"Travis Roberts\"},\"logo\":{\"@id\":\"http:\/\/www.ciraltos.com\/staging2\/#\/schema\/person\/image\/\"},\"sameAs\":[\"http:\/\/www.ciraltos.com\",\"https:\/\/twitter.com\/ciraltos\"],\"url\":\"https:\/\/www.ciraltos.com\/staging2\/author\/admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Write Data from PowerShell to Azure Table Storage - ciraltos","description":"In this post and video I go over how I used PowerShell to write data into Azure Table Storage. I cover items such as setting up a storage account and a table to write data to, configure Azure Storage Explorer to connect to the storage account and view the data. I also go over generating a Shared Access Signature (SAS) to authenticate the script.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.ciraltos.com\/staging2\/write-data-from-powershell-to-azure-table-storage\/","og_locale":"en_US","og_type":"article","og_title":"Write Data from PowerShell to Azure Table Storage - ciraltos","og_description":"In this post and video I go over how I used PowerShell to write data into Azure Table Storage. I cover items such as setting up a storage account and a table to write data to, configure Azure Storage Explorer to connect to the storage account and view the data. I also go over generating a Shared Access Signature (SAS) to authenticate the script.","og_url":"https:\/\/www.ciraltos.com\/staging2\/write-data-from-powershell-to-azure-table-storage\/","og_site_name":"ciraltos","article_published_time":"2018-02-19T03:23:18+00:00","author":"Travis Roberts","twitter_card":"summary_large_image","twitter_creator":"@ciraltos","twitter_site":"@ciraltos","twitter_misc":{"Written by":"Travis Roberts","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.ciraltos.com\/staging2\/write-data-from-powershell-to-azure-table-storage\/#article","isPartOf":{"@id":"https:\/\/www.ciraltos.com\/staging2\/write-data-from-powershell-to-azure-table-storage\/"},"author":{"name":"Travis Roberts","@id":"http:\/\/www.ciraltos.com\/staging2\/#\/schema\/person\/25391996d6cddfecd4d257162b7e373a"},"headline":"Write Data from PowerShell to Azure Table Storage","datePublished":"2018-02-19T03:23:18+00:00","dateModified":"2018-02-19T03:23:18+00:00","mainEntityOfPage":{"@id":"https:\/\/www.ciraltos.com\/staging2\/write-data-from-powershell-to-azure-table-storage\/"},"wordCount":540,"commentCount":2,"publisher":{"@id":"http:\/\/www.ciraltos.com\/staging2\/#\/schema\/person\/25391996d6cddfecd4d257162b7e373a"},"keywords":["add-storagetablerow","Azure","get-azurestoragetable","New-AzureStorageContext","Powershell","SAS","storage","Table Storage"],"articleSection":["Azure","PowerShell"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.ciraltos.com\/staging2\/write-data-from-powershell-to-azure-table-storage\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.ciraltos.com\/staging2\/write-data-from-powershell-to-azure-table-storage\/","url":"https:\/\/www.ciraltos.com\/staging2\/write-data-from-powershell-to-azure-table-storage\/","name":"Write Data from PowerShell to Azure Table Storage - ciraltos","isPartOf":{"@id":"http:\/\/www.ciraltos.com\/staging2\/#website"},"datePublished":"2018-02-19T03:23:18+00:00","dateModified":"2018-02-19T03:23:18+00:00","description":"In this post and video I go over how I used PowerShell to write data into Azure Table Storage. I cover items such as setting up a storage account and a table to write data to, configure Azure Storage Explorer to connect to the storage account and view the data. I also go over generating a Shared Access Signature (SAS) to authenticate the script.","breadcrumb":{"@id":"https:\/\/www.ciraltos.com\/staging2\/write-data-from-powershell-to-azure-table-storage\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.ciraltos.com\/staging2\/write-data-from-powershell-to-azure-table-storage\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.ciraltos.com\/staging2\/write-data-from-powershell-to-azure-table-storage\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/www.ciraltos.com\/staging2\/"},{"@type":"ListItem","position":2,"name":"Write Data from PowerShell to Azure Table Storage"}]},{"@type":"WebSite","@id":"http:\/\/www.ciraltos.com\/staging2\/#website","url":"http:\/\/www.ciraltos.com\/staging2\/","name":"ciraltos","description":"cloud, technology and trends","publisher":{"@id":"http:\/\/www.ciraltos.com\/staging2\/#\/schema\/person\/25391996d6cddfecd4d257162b7e373a"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/www.ciraltos.com\/staging2\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"http:\/\/www.ciraltos.com\/staging2\/#\/schema\/person\/25391996d6cddfecd4d257162b7e373a","name":"Travis Roberts","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/www.ciraltos.com\/staging2\/#\/schema\/person\/image\/","url":"https:\/\/www.ciraltos.com\/staging2\/wp-content\/uploads\/2023\/03\/Logo-1.png","contentUrl":"https:\/\/www.ciraltos.com\/staging2\/wp-content\/uploads\/2023\/03\/Logo-1.png","width":5657,"height":3563,"caption":"Travis Roberts"},"logo":{"@id":"http:\/\/www.ciraltos.com\/staging2\/#\/schema\/person\/image\/"},"sameAs":["http:\/\/www.ciraltos.com","https:\/\/twitter.com\/ciraltos"],"url":"https:\/\/www.ciraltos.com\/staging2\/author\/admin\/"}]}},"_links":{"self":[{"href":"https:\/\/www.ciraltos.com\/staging2\/wp-json\/wp\/v2\/posts\/425"}],"collection":[{"href":"https:\/\/www.ciraltos.com\/staging2\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ciraltos.com\/staging2\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ciraltos.com\/staging2\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ciraltos.com\/staging2\/wp-json\/wp\/v2\/comments?post=425"}],"version-history":[{"count":10,"href":"https:\/\/www.ciraltos.com\/staging2\/wp-json\/wp\/v2\/posts\/425\/revisions"}],"predecessor-version":[{"id":435,"href":"https:\/\/www.ciraltos.com\/staging2\/wp-json\/wp\/v2\/posts\/425\/revisions\/435"}],"wp:attachment":[{"href":"https:\/\/www.ciraltos.com\/staging2\/wp-json\/wp\/v2\/media?parent=425"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ciraltos.com\/staging2\/wp-json\/wp\/v2\/categories?post=425"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ciraltos.com\/staging2\/wp-json\/wp\/v2\/tags?post=425"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}