{"id":510,"date":"2008-07-28T02:00:09","date_gmt":"2008-07-28T10:00:09","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/?p=510"},"modified":"2008-07-18T10:39:27","modified_gmt":"2008-07-18T18:39:27","slug":"searching-word","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2008\/07\/28\/searching-word\/","title":{"rendered":"Searching Word"},"content":{"rendered":"<p>One of the last tasks I needed to accomplish to get <a href=\"http:\/\/www.sapienpress.com\/ad.asp\" target=\"_blank\">Managing Active Directory with Windows PowerShell: TFM<\/a> ready for publication was to assemble all the script samples. They&#8217;ll be available for download from SAPIENPress when the book is published.&nbsp; I knew that most of the scripts were in my primary scripts directory, but probably not all.&nbsp; I usually copied scripts from my test environment to my scripts directory but I knew some were bound to be missing.&nbsp; I didn&#8217;t want to manually open 16 chapters, search for script names, check if I had the script and then move it to a new folder. I don&#8217;t have time for that. But I do have time to figure out how to do it in PowerShell.<\/p>\n<p>What I had going for me was that every script sample referenced in a chapter was formatted with a specific style. All I had to do was search for the style and retrieve the text. That would be the name of my script which I could then look for and move if found. I could also keep a log of scripts not found so I could go back to my test machine and retrieve them. Here&#8217;s the PowerShell script I developed.<\/p>\n<div style=\"border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; font-size: 8pt; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(244, 244, 244);\">\n<div style=\"border-style: none; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(244, 244, 244);\">\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: rgb(96, 96, 96);\">   1:<\/span> $word=New-Object -COM <span style=\"color: rgb(0, 96, 128);\">&quot;Word.Application&quot;<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(244, 244, 244);\"><span style=\"color: rgb(96, 96, 96);\">   2:<\/span>&nbsp; <\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: rgb(96, 96, 96);\">   3:<\/span> $errorlog=<span style=\"color: rgb(0, 96, 128);\">&quot;c:\\missing.csv&quot;<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(244, 244, 244);\"><span style=\"color: rgb(96, 96, 96);\">   4:<\/span> Set-Content $errorlog <span style=\"color: rgb(0, 96, 128);\">&quot;Chapter,Script&quot;<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: rgb(96, 96, 96);\">   5:<\/span> Get-ChildItem c:\\test\\*.doc | <span style=\"color: rgb(0, 0, 255);\">foreach<\/span> {<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(244, 244, 244);\"><span style=\"color: rgb(96, 96, 96);\">   6:<\/span>     $file=$_.fullname<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: rgb(96, 96, 96);\">   7:<\/span>     Write-Host $file<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(244, 244, 244);\"><span style=\"color: rgb(96, 96, 96);\">   8:<\/span>     $doc=$word.Documents.Open($file) <\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: rgb(96, 96, 96);\">   9:<\/span>     <\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(244, 244, 244);\"><span style=\"color: rgb(96, 96, 96);\">  10:<\/span>     $style=$word.ActiveDocument.Styles | <\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: rgb(96, 96, 96);\">  11:<\/span>     where {$_.namelocal <span style=\"color: rgb(204, 102, 51);\">-eq<\/span> <span style=\"color: rgb(0, 96, 128);\">&quot;code Title&quot;<\/span>}<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(244, 244, 244);\"><span style=\"color: rgb(96, 96, 96);\">  12:<\/span>     <\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: rgb(96, 96, 96);\">  13:<\/span>     $word.Selection.Find.Style = $style<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(244, 244, 244);\"><span style=\"color: rgb(96, 96, 96);\">  14:<\/span>     <\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: rgb(96, 96, 96);\">  15:<\/span>     <span style=\"color: rgb(0, 0, 255);\">while<\/span> ($word.Selection.Find.Execute()) {<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(244, 244, 244);\"><span style=\"color: rgb(96, 96, 96);\">  16:<\/span>         $text=($word.selection.sentences | select Text).text<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: rgb(96, 96, 96);\">  17:<\/span>         $<span style=\"color: rgb(0, 0, 255);\">script<\/span>=(Join-Path <span style=\"color: rgb(0, 96, 128);\">&quot;C:\\scripts\\posh&quot;<\/span> $text).Trim()<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(244, 244, 244);\"><span style=\"color: rgb(96, 96, 96);\">  18:<\/span>&nbsp; <\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: rgb(96, 96, 96);\">  19:<\/span>          <span style=\"color: rgb(0, 0, 255);\">if<\/span> ((Get-Item $<span style=\"color: rgb(0, 0, 255);\">script<\/span> -ea <span style=\"color: rgb(0, 96, 128);\">&quot;silentlycontinue&quot;<\/span>).exists) {<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(244, 244, 244);\"><span style=\"color: rgb(96, 96, 96);\">  20:<\/span>              Write-Host <span style=\"color: rgb(0, 96, 128);\">&quot;verified $script&quot;<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: rgb(96, 96, 96);\">  21:<\/span>              Move-Item $<span style=\"color: rgb(0, 0, 255);\">script<\/span> -destination <span style=\"color: rgb(0, 96, 128);\">&quot;c:\\scripts\\posh\\ad&quot;<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(244, 244, 244);\"><span style=\"color: rgb(96, 96, 96);\">  22:<\/span>          }<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: rgb(96, 96, 96);\">  23:<\/span>         <span style=\"color: rgb(0, 0, 255);\">else<\/span> {<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(244, 244, 244);\"><span style=\"color: rgb(96, 96, 96);\">  24:<\/span>             $msg=<span style=\"color: rgb(0, 96, 128);\">&quot;{0},{1}&quot;<\/span> -f $file,$<span style=\"color: rgb(0, 0, 255);\">script<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: rgb(96, 96, 96);\">  25:<\/span>             Add-Content $errorlog $msg<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(244, 244, 244);\"><span style=\"color: rgb(96, 96, 96);\">  26:<\/span>         }<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: rgb(96, 96, 96);\">  27:<\/span>         <\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(244, 244, 244);\"><span style=\"color: rgb(96, 96, 96);\">  28:<\/span>      } <\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: rgb(96, 96, 96);\">  29:<\/span>      <\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(244, 244, 244);\"><span style=\"color: rgb(96, 96, 96);\">  30:<\/span>      $doc.close()<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: rgb(96, 96, 96);\">  31:<\/span>  <\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(244, 244, 244);\"><span style=\"color: rgb(96, 96, 96);\">  32:<\/span>  }<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: rgb(96, 96, 96);\">  33:<\/span>&nbsp; <\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: rgb(244, 244, 244);\"><span style=\"color: rgb(96, 96, 96);\">  34:<\/span> $word.quit()<\/pre>\n<\/div>\n<\/div>\n<p>I use the New-Object cmdlet to instantiate an instance of Microsoft Word using the -COM parameter. On lines 3 and 4 I create a csv file for recording missing scripts. I decided to make it a CSV file so I could use Import-CSV if necessary to parse it.&nbsp; Although probably unnecessary, I copied all the Word docs to my Test directory and used Get-ChildItem to enumerate them (line 5).<\/p>\n<p>For every document, I set variable $file to its full name and path (line 6). I needed the fullpath so that I could open the file in line 8.<\/p>\n<p>Lines 10 and 11 define a variable for the style I want to search for, &quot;code title&quot;.&nbsp; In line 13 I define my search criteria. The Execute() method carries out the search and returns True if a match is made. So I used a While loop in line 15 to process all the found items. When a search is successful, the cursor or file pointer jumps to the found item just like it does when you manually search for something in Word.&nbsp; Because the script file name is the only text on the line, I can use an expression like line 16 to retrieve the text. To build a file name I use Join-Path on line 17 to concatenate my script directory and the script name pulled from the Word doc.<\/p>\n<p>At line 19 I need to see if the script exists using Get-Item. Notice the -ea parameter. By default if the file doesn&#8217;t exist PowerShell will raise an exception and the script will fail. By setting the -erroraction parameter to silentlycontinue, I suppress the exception. If the script doesn&#8217;t exist then the If expression will not be true and I can log information to my error file in lines 24 and 25. if the file exists, then it is moved to a new directory (line 21).&nbsp; After checking the Word doc, is closed (line 30) and the next file is checked. At the end of the script, I quit Word.<\/p>\n<p>I&#8217;m sure this would have taken me 30 minutes or more to accomplish all of this manually. With the script it took a few seconds. Sure there was a little time spent developing, but since I use the sample template for all my SAPIEN Press books I can reuse this script for future projects so it was time well spent, plus I learned a few new things about the Microsoft Word COM object.<\/p>\n<p>If you&#8217;re interested, you can download the script<a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2008\/07\/parse-docs.txt\" target=\"_blank\"> here<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the last tasks I needed to accomplish to get Managing Active Directory with Windows PowerShell: TFM ready for publication was to assemble all the script samples. They&#8217;ll be available for download from SAPIENPress when the book is published.  I knew that most of the scripts were in my primary scripts directory, but probably not all.  I usually copied scripts from my test environment to my scripts directory but I knew some were bound to be missing.  I didn&#8217;t want to manually open 16 chapters, search for script names, check if I had the script and then move it to a new folder. I don&#8217;t have time for that. But I do have time to figure out how to do it in PowerShell.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[19,25],"tags":[61,230,243,241,242,28],"class_list":["post-510","post","type-post","status-publish","format-standard","hentry","category-books-and-training","category-windows-powershell","tag-com","tag-get-childitem","tag-join-path","tag-microsoft-word","tag-new-object","tag-powershell"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/510","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=510"}],"version-history":[{"count":2,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/510\/revisions"}],"predecessor-version":[{"id":511,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/510\/revisions\/511"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=510"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=510"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=510"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}