tag:blogger.com,1999:blog-89030067434211463662024-02-19T03:19:36.635-08:00PauerSchellThis blog is about PowerShell Scripting, therefor it's name is just PowerShell. Ok using german orthografie.
I have a Python background and my office duties are databases and reporting related.Bernd Krisziohttp://www.blogger.com/profile/03294997372899922987noreply@blogger.comBlogger89125tag:blogger.com,1999:blog-8903006743421146366.post-48919383536236598942014-07-03T03:00:00.000-07:002014-07-03T03:00:47.414-07:00Two workarounds to the PowerShell Workflow ProblemI got two workarounds to the problem in previous post.<br />
<br />
Frank Peter Schultze @fpschultze proposed to use inlinescript<br />
<br />
<blockquote class="tr_bq">
Workflow Strange<br />{<br /> param(<br /> [string]$folder<br /> )<br /><br /> inlinescript<br /> {<br /> !(Test-Path -path $using:folder)<br /> }<br />}</blockquote>
<br />
and I found the following solution<br />
<br />
<blockquote class="tr_bq">
Workflow Strange<br />{<br /> param(<br /> [string]$folder<br /> )<br /> $a = Test-Path -path $folder<br /> ! $a<br />} </blockquote>
Bernd Krisziohttp://www.blogger.com/profile/03294997372899922987noreply@blogger.com0tag:blogger.com,1999:blog-8903006743421146366.post-27378948107945231862014-07-03T01:54:00.002-07:002014-07-03T01:54:30.303-07:00Test-Path in PowerShell Workflow ProblemHello PowerShell World.<br />
I'm back an my current focus is PowerShell WorkFlows.<br />
<br />
And I just found a strange example I can't explain.<br />
<br />
<blockquote class="tr_bq">
Workflow Strange<br />{<br /> param(<br /> [string]$folder<br /> )<br /><br /><br /> if (Test-Path -path $folder)<br /> {<br /> "Folder $folder exists"<br /> } <br /> if (!(Test-Path -path $folder))<br /> {<br /> "Folder $folder seenms not exists"<br /> "But Get-Item shows it is there"<br /> Get-item -path $folder<br /><br /> }<br /><br /> $a = (Test-Path -path $folder)<br /> ! $a <br />}</blockquote>
<br />Strange -folder 'C:\temp' -PSComputerName MAGV-2012AKTDE <br />
And here is its output<br />
<br />
<blockquote class="tr_bq">
Folder C:\temp exists<br />Folder C:\temp seenms not exists<br />But Get-Item shows it is there<br /><br /><br /> Verzeichnis: C:\<br /><br /><br />Mode LastWriteTime Length Name PSComputerName <br />---- ------------- ------ ---- -------------- <br />d---- 22.08.2013 10:53 temp MAGV-2012AKTDE <br />True</blockquote>
<br />
<br />
<br />
<br />
<br />
<br />
All involved machines running PowerShell V3.0. Bernd Krisziohttp://www.blogger.com/profile/03294997372899922987noreply@blogger.com0tag:blogger.com,1999:blog-8903006743421146366.post-22608544760353883372012-08-19T06:31:00.000-07:002012-08-19T06:44:26.256-07:00Screenshot of Windows 8 Pseudo Start Menu<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEioOrPtzcvORK0pd7fMdxsAf766bFo0r_-RJTU-xD6J1daRZPVj34u9iw5IzkIVG_9eHylnwuRCosoyBGso7_o7eFlslaKXqWewH7aJ34gs-FMY5qfhiBBVpr9JjTEO4TLDLE0EYGMJsu8/s1600/W8_with_PseudoStartMenu.jpeg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEioOrPtzcvORK0pd7fMdxsAf766bFo0r_-RJTU-xD6J1daRZPVj34u9iw5IzkIVG_9eHylnwuRCosoyBGso7_o7eFlslaKXqWewH7aJ34gs-FMY5qfhiBBVpr9JjTEO4TLDLE0EYGMJsu8/s1600/W8_with_PseudoStartMenu.jpeg" /></a></div>
<br />
Here is a screenshot of my Windows 8 RTM machine with the pseudo start menu.
Click into the picture to see the complete desktop of my machine.
And read my last <a href="http://pauerschell.blogspot.de/2012/08/how-to-build-windows-8-start-menu.html">post</a>, to see how it was done.Bernd Krisziohttp://www.blogger.com/profile/03294997372899922987noreply@blogger.com0tag:blogger.com,1999:blog-8903006743421146366.post-11099064811227505092012-08-19T03:39:00.001-07:002012-08-19T03:39:36.886-07:00How to build a Windows 8 Start Menu<div style="font-family: Arial,Helvetica,sans-serif;">
<span style="font-size: small;">Recently I read <a href="http://www.howtogeek.com/107711/how-to-get-the-classic-start-menu-back-in-windows-8/">How to Get the Classic Start Menu Back in Windows 8</a> from How-To-Geek.</span></div>
<div style="font-family: Arial,Helvetica,sans-serif;">
<span style="font-size: small;"><br /></span></div>
<div style="font-family: Arial,Helvetica,sans-serif;">
<span style="font-size: small;">Having my toolbar attached to the left it creates a menu near the left bottom corner. That is great.</span></div>
<div style="font-family: Arial,Helvetica,sans-serif;">
<span style="font-size: small;"><br /></span></div>
<div style="font-family: Arial,Helvetica,sans-serif;">
<span style="font-size: small;">The only problem is, that there are two start menu folder</span></div>
<div style="font-family: Arial,Helvetica,sans-serif;">
<span style="font-size: small;"><br /></span></div>
<div style="font-family: Arial,Helvetica,sans-serif;">
<span style="font-size: small;">%ProgramData%\Microsoft\Windows\Start Menu\Programs</span></div>
<div style="font-family: Arial,Helvetica,sans-serif;">
<span style="font-size: small;"><br /></span></div>
<div style="font-family: Arial,Helvetica,sans-serif;">
<span style="font-size: small;">and</span></div>
<div style="font-family: Arial,Helvetica,sans-serif;">
<span style="font-size: small;"><br /></span></div>
<div style="font-family: Arial,Helvetica,sans-serif;">
<span style="font-size: small;">%AppData%\Microsoft\Windows\Start Menu\Programs</span></div>
<div style="font-family: Arial,Helvetica,sans-serif;">
<span style="font-size: small;"><br /></span></div>
<div style="font-family: Arial,Helvetica,sans-serif;">
<span style="font-size: small;">To combine the two folders into one I create this little PowerShell function, which creates or updates a combined version at C:\Startmenu.</span></div>
<div style="font-family: Arial,Helvetica,sans-serif;">
<span style="font-size: small;"><br /></span></div>
<div style="font-family: Arial,Helvetica,sans-serif;">
<span style="font-size: small;">All to do to complete the trick is to use this path instead of the above pathes given by How-To-Geek to create the toolbar.</span></div>
<br />
PS.:<b> the new Windows 8 UI (formerly called Metro ) is just the backside of the desktop, it is nearly useless for Desktop Users.</b><br />
<br />
<br />
<br />
<script src="http://PoshCode.org/embed/3583" type="text/javascript"></script>
<br />
<br />Bernd Krisziohttp://www.blogger.com/profile/03294997372899922987noreply@blogger.com2tag:blogger.com,1999:blog-8903006743421146366.post-31619207412673536592012-07-22T07:45:00.001-07:002012-07-22T07:45:12.898-07:00Bookreview<div style="font-family: Verdana,sans-serif;">
<a href="http://akamaicovers.oreilly.com/images/9780735658141/s.gif" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="200" src="http://akamaicovers.oreilly.com/images/9780735658141/s.gif" style="color: black;" width="163" /></a><span style="font-size: x-small;"><span style="font-size: large;"> </span><span style="color: black;"></span></span><br />
<span style="font-size: large;">Microsoft® SQL Server® 2012 T-SQL Fundamentals</span><br />
<br />
<span style="font-size: x-small;"><span style="color: black;"></span></span><br />
<span style="font-size: x-small;"><span style="color: black;"></span></span><br />
<span style="font-size: x-small;"><span style="color: black;">By </span><a class="byLineLink" href="http://shop.oreilly.com/product/0790145321978.do?sortby=bestSellers#tab_03" style="color: black;">Itzik Ben-Gan</a></span><br />
<div>
<span style="font-size: x-small;">Publisher: Microsoft Press</span></div>
<div>
<span style="font-size: x-small;">Released: June 2012</span></div>
<div class="default">
<span style="font-size: x-small;">Pages: 448</span><br />
</div>
</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
</div>
<div style="font-family: Verdana,sans-serif;">
<b>A</b><b> succinct introduction for beginners and a profound reference for experienced users. </b></div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
This is a fresh book about T-SQL. Its title references SQL-Server version 2012, but it is well suited to work with older versions, say back to SQL-Server 2005. Features not present in all versions are clearly tagged.</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
The book targets at beginners, but I can can assure you, that even after more than 10 years practise with sql-scripts, it is an excellent reading.</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
Let us look at the contents, which is currently missing on O'Reilly's link below. <br />
<br />
Chapter 1 Background to T-SQL Querying and Programming</div>
<div style="font-family: Verdana,sans-serif;">
Beside some theoretical background, here you learn what is new, like the <b>ABC</b>. These are acronyms standing for the flavors of SQL Server: <b>A</b>ppliance, <b>B</b>ox and <b>C</b>loud. Box is the new name for what I knew as SQL Server from versions 6.0 to 2008. Now I know about A and C too.<br />
<br />
Chapter 2 Single-Table Queries.</div>
<div style="font-family: Verdana,sans-serif;">
I didn't learn new features, but a better presentation of the order of the logical steps, which are done, when a query is executed. This helps later to understand some restrictions, which I though of a bit artificial until now.<br />
<br />
Chapters 3 Joins</div>
<div style="font-family: Verdana,sans-serif;">
No new features since SQL Server 2005. But rather exhaustive, well suited for beginners.</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
Chapter 4 Subqueries</div>
<div style="font-family: Verdana,sans-serif;">
Here the section <i>Dealing with Misbehaving Subqueries</i> contains stuff I had learned the hard way, i.e. by doing.</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
Chapter 5 Table Expressions</div>
<span style="font-family: Verdana,sans-serif;">I got the confirmation that some coding variants perform equally well and it is best to choose the most readable. For example Common Table Expressions (CTEs) often increase the readability and no measurable impact on the performance.</span><br />
<div style="font-family: Verdana,sans-serif;">
<br />
Chapter 6 Set Operators </div>
<div style="font-family: Verdana,sans-serif;">
Here the understanding of the logical steps from chapter 2 bear fruits. </div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
Chapter 7 Beyond the Fundamentals of Querying</div>
<div style="font-family: Verdana,sans-serif;">
This starts with Window Functions. Obviously a new name, for something I knew as Analytical functions. And there are a few new functions here to learn with SQL Server 2012. You also find Pivoting, Unpivoting and Grouping Sets here. </div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
Chapter 8 Data Modification</div>
<div style="font-family: Verdana,sans-serif;">
Besides INSERT, UPDATE and DELETE, you find truncate, bulk insert and merge here too. <br />
<br />
Chapter 9 Transactions and Concurrency. </div>
<div style="font-family: Verdana,sans-serif;">
I have to reread this later again. Just found out, that I have some gaps here. </div>
<div style="font-family: Verdana,sans-serif;">
<br />
Chapter 10 Programmable Objects </div>
<div style="font-family: Verdana,sans-serif;">
This is about batches, functions and store procedures. </div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
And there is an appendix showing how to do an install, to try all the things out.</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
Let me resume in 10 chapters, you have all the basic stuff to write correct T-SQL scripts. Even some hints concerning performance are included. <br />
<br />
If T-SQL is new to you, you find lots of exercises and solutions in the book. </div>
<div style="font-family: Verdana,sans-serif;">
I only threw a short look on them.</div>
<div style="font-family: Verdana,sans-serif;">
<br />
To me this is a valuable reference book and a resource to fill some gaps.</div>
<div style="font-family: Verdana,sans-serif;">
<br />
I got my team leader to order a copy of the book for my two younger co-workers. I think it is good to have things in place in one book we all have read, than having to collect each bit from the web when need arises.</div>
<div style="font-family: Verdana,sans-serif;">
<br /></div>
<div style="font-family: Verdana,sans-serif;">
<br />
More info about the book you find here:</div>
<div style="font-family: Verdana,sans-serif;">
<a href="http://shop.oreilly.com/product/0790145321978.do?sortby=bestSellers#">Microsoft® SQL Server® 2012 T-SQL Fundamentals </a><br />
<br /></div>
<a href="http://oreilly.com/bloggers/"><img alt="I review for the O'Reilly Blogger Review Program" border="0" height="150" src="http://cdn.oreilly.com/bloggers/blogger-review-badge-200.png" width="200" /></a>Bernd Krisziohttp://www.blogger.com/profile/03294997372899922987noreply@blogger.com0tag:blogger.com,1999:blog-8903006743421146366.post-77154046574280083622012-05-20T08:03:00.000-07:002012-05-20T08:03:54.082-07:00Small Framework to load sql scripts simultaniously to several Servers<div style="font-family: Arial,Helvetica,sans-serif;">
I use the following framework to run scripts calling sqlcmd simultaneously as PowerShell jobs during some MsBuild Task. My Buildagents are running Server 2003 and there are some linitations <a href="http://stackoverflow.com/questions/10667524/sqlconnection-open-fails-in-powershell-jobs-on-server-2003">cf. StackOverflow</a>.</div>
<div style="font-family: Arial,Helvetica,sans-serif;">
But just calling sqlcmd works fine. In fact I'm dropping and recreating my test databases and running a lot of sql scripts completly within such background jobs. I didn't try it with the Oracle part using sqlplus which I run in the main script and after whose termination I wait for jobs to terminate in time or terminate them. </div>
<br />
<pre class="PowerShellColorizedScript"><span style="color: orangered;">$workdir</span> <span style="color: darkgrey;">=</span> <span style="color: blue;">Split-Path</span> <span style="color: orangered;">$MyInvocation</span><span style="color: darkgrey;">.</span><span style="color: black;">MyCommand</span><span style="color: darkgrey;">.</span><span style="color: black;">Path</span>
<span style="color: orangered;">$logdir</span> <span style="color: darkgrey;">=</span> <span style="color: darkred;">'C:\temp\log'</span>
<span style="color: orangered;">$server1</span> <span style="color: darkgrey;">=</span> <span style="color: darkred;">'my_sql2008'</span>
<span style="color: orangered;">$server2</span> <span style="color: darkgrey;">=</span> <span style="color: darkred;">'my_sql2005'</span>
<span style="color: orangered;">$datbase</span> <span style="color: darkgrey;">=</span> <span style="color: darkred;">'PowerShellTest'</span>
<span style="color: orangered;">$cmd1</span> <span style="color: darkgrey;">=</span> <span style="color: teal;">[scriptblock]</span><span style="color: darkgrey;">::</span><span style="color: black;">Create</span><span style="color: black;">(</span><span style="color: darkred;">". $workdir\script_containing_function_for_background_job.ps1
Some_function -server $server1 -database $datbase $logdir -asJob"</span> <span style="color: black;">)</span>
<span style="color: orangered;">$cmd1</span> <span style="color: darkgrey;">=</span> <span style="color: teal;">[scriptblock]</span><span style="color: darkgrey;">::</span><span style="color: black;">Create</span><span style="color: black;">(</span><span style="color: darkred;">". $workdir\script_containing_function_for_background_job.ps1
Some_function -server $server2 -database $datbase $logdir -asJob"</span> <span style="color: black;">)</span>
<span style="color: darkred;">"...Starting the background jobs"</span>
<span style="color: orangered;">$SQLJobStartTime</span> <span style="color: darkgrey;">=</span> <span style="color: blue;">Get-date</span>
<span style="color: orangered;">$jnr1</span> <span style="color: darkgrey;">=</span> <span style="color: black;">(</span><span style="color: blue;">Start-Job</span> <span style="color: navy;">-ScriptBlock</span> <span style="color: orangered;">$cmd1</span><span style="color: black;">)</span><span style="color: darkgrey;">.</span><span style="color: black;">Id</span>
<span style="color: orangered;">$jnr2</span> <span style="color: darkgrey;">=</span> <span style="color: black;">(</span><span style="color: blue;">Start-Job</span> <span style="color: navy;">-ScriptBlock</span> <span style="color: orangered;">$cmd2</span><span style="color: black;">)</span><span style="color: darkgrey;">.</span><span style="color: black;">Id</span>
<span style="color: darkgreen;"># *****************************************</span>
<span style="color: darkred;">' Doing something slow here (it happens to be with an Oracle Server'</span>
<span style="color: darkgreen;"><#
....
#></span>
<span style="color: darkblue;">function</span> <span style="color: blueviolet;">Get-WaitTime</span>
<span style="color: black;">{</span>
<span style="color: darkblue;">param</span><span style="color: black;">(</span>
<span style="color: teal;">[DateTime]</span><span style="color: orangered;">$start</span><span style="color: darkgrey;">,</span>
<span style="color: orangered;">$dauer</span>
<span style="color: black;">)</span>
<span style="color: orangered;">$rest</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$dauer</span> <span style="color: darkgrey;">-</span> <span style="color: black;">(</span><span style="color: black;">(</span><span style="color: blue;">Get-Date</span><span style="color: black;">)</span> <span style="color: darkgrey;">-</span> <span style="color: orangered;">$start</span><span style="color: black;">)</span><span style="color: darkgrey;">.</span><span style="color: black;">Totalseconds</span>
<span style="color: darkblue;">if</span> <span style="color: black;">(</span><span style="color: orangered;">$rest</span> <span style="color: darkgrey;">-lt</span> <span style="color: purple;">0</span><span style="color: black;">)</span> <span style="color: black;">{</span><span style="color: orangered;">$rest</span> <span style="color: darkgrey;">=</span> <span style="color: purple;">0</span><span style="color: black;">}</span>
<span style="color: orangered;">$rest</span>
<span style="color: black;">}</span>
<span style="color: darkred;">'...Waiting for job on SQL Server 2008'</span>
<span style="color: blue;">Wait-job</span> <span style="color: navy;">-id</span> <span style="color: orangered;">$jnr1</span> <span style="color: navy;">-timeout</span> <span style="color: black;">(</span> <span style="color: blue;">Get-WaitTime</span> <span style="color: orangered;">$SQLJobStartTime</span> <span style="color: purple;">960</span> <span style="color: black;">)</span> <span style="color: darkgreen;"># max 16 min</span>
<span style="color: darkblue;">if</span> <span style="color: black;">(</span><span style="color: black;">(</span><span style="color: blue;">Get-job</span> <span style="color: navy;">-id</span> <span style="color: orangered;">$jnr1</span><span style="color: black;">)</span><span style="color: darkgrey;">.</span><span style="color: black;">state</span> <span style="color: darkgrey;">-eq</span> <span style="color: darkred;">'Running'</span><span style="color: black;">)</span>
<span style="color: black;">{</span>
<span style="color: blue;">Stop-job</span> <span style="color: navy;">-id</span> <span style="color: orangered;">$jnr1</span>
<span style="color: black;">}</span>
<span style="color: blue;">Receive-Job</span> <span style="color: navy;">-id</span> <span style="color: orangered;">$jnr1</span>
<span style="color: orangered;">$state1</span> <span style="color: darkgrey;">=</span> <span style="color: black;">(</span><span style="color: blue;">Get-job</span> <span style="color: navy;">-id</span> <span style="color: orangered;">$jnr1</span><span style="color: black;">)</span><span style="color: darkgrey;">.</span><span style="color: black;">State</span>
<span style="color: orangered;">$dauer1</span> <span style="color: darkgrey;">=</span> <span style="color: black;">(</span><span style="color: black;">(</span><span style="color: blue;">get-date</span><span style="color: black;">)</span> <span style="color: darkgrey;">-</span> <span style="color: orangered;">$SQLJobStartTime</span><span style="color: black;">)</span><span style="color: darkgrey;">.</span><span style="color: black;">Totalseconds</span>
<span style="color: darkred;">"$(Get-date -f d) $(Get-date -f t) Release $release($build) Einspielen auf SQl-Server 2008 $state1 $dauer1 secs"</span>
<span style="color: darkred;">'...Waiting for job SQL Server 2005'</span>
<span style="color: blue;">Wait-job</span> <span style="color: navy;">-id</span> <span style="color: orangered;">$jnr2</span> <span style="color: navy;">-timeout</span> <span style="color: black;">(</span> <span style="color: blue;">Get-WaitTime</span> <span style="color: orangered;">$SQLJobStartTime</span> <span style="color: purple;">960</span> <span style="color: black;">)</span> <span style="color: darkgreen;"># max 16 min</span>
<span style="color: darkblue;">if</span> <span style="color: black;">(</span><span style="color: black;">(</span><span style="color: blue;">Get-job</span> <span style="color: navy;">-id</span> <span style="color: orangered;">$jnr2</span><span style="color: black;">)</span><span style="color: darkgrey;">.</span><span style="color: black;">state</span> <span style="color: darkgrey;">-eq</span> <span style="color: darkred;">'Running'</span><span style="color: black;">)</span>
<span style="color: black;">{</span>
<span style="color: blue;">Stop-job</span> <span style="color: navy;">-id</span> <span style="color: orangered;">$jnr2</span>
<span style="color: black;">}</span>
<span style="color: blue;">Receive-Job</span> <span style="color: navy;">-id</span> <span style="color: orangered;">$jnr2</span>
<span style="color: orangered;">$state2</span> <span style="color: darkgrey;">=</span> <span style="color: black;">(</span><span style="color: blue;">Get-job</span> <span style="color: navy;">-id</span> <span style="color: orangered;">$jnr2</span><span style="color: black;">)</span><span style="color: darkgrey;">.</span><span style="color: black;">State</span>
<span style="color: orangered;">$dauer2</span> <span style="color: darkgrey;">=</span> <span style="color: black;">(</span><span style="color: black;">(</span><span style="color: blue;">get-date</span><span style="color: black;">)</span> <span style="color: darkgrey;">-</span> <span style="color: orangered;">$SQLJobStartTime</span><span style="color: black;">)</span><span style="color: darkgrey;">.</span><span style="color: black;">Totalseconds</span>
<span style="color: darkred;">"$(Get-date -f d) $(Get-date -f t) Release $release($build) Einspielen auf SQl-Server 2005 $state2 $dauer2 secs"</span>
</pre>
<br />Bernd Krisziohttp://www.blogger.com/profile/03294997372899922987noreply@blogger.com0tag:blogger.com,1999:blog-8903006743421146366.post-42160881704133542662012-05-20T04:10:00.002-07:002012-05-20T04:10:31.961-07:00Displaying the builds of a given build-definition since the latest succesfull one<div style="font-family: Arial,Helvetica,sans-serif;">
When it comes to fixing broken builds, the first step is to determine when the last successful build run and which builds failed afterwards.</div>
<div style="font-family: Arial,Helvetica,sans-serif;">
In practice I'm looking for the state of 20 build definitions each morning, but here I reduce the script to a single one.</div>
<br />
<pre class="PowerShellColorizedScript"><span style="color: darkgreen;"># adapt the following 4 lines to your environment</span>
<span style="color: blue;">Add-Type</span> <span style="color: navy;">-Path</span> <span style="color: darkred;">"C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\ReferenceAssemblies\v2.0\Microsoft.TeamFoundation.Build.Client.dll"</span>
<span style="color: orangered;">$tfsuri</span> <span style="color: darkgrey;">=</span> <span style="color: darkred;">'your tfs server'</span>
<span style="color: orangered;">$project</span> <span style="color: darkgrey;">=</span> <span style="color: darkred;">"your prpject"</span>
<span style="color: orangered;">$buildDefinition</span> <span style="color: darkgrey;">=</span> <span style="color: darkred;">'Main'</span>
<span style="color: darkblue;">function</span> <span style="color: blueviolet;">Get-BuildInfo</span>
<span style="color: black;">{</span>
<span style="color: darkblue;">param</span><span style="color: black;">(</span>
<span style="color: orangered;">$buildDefinition</span><span style="color: darkgrey;">,</span>
<span style="color: orangered;">$MaxBuildsPerDefinition</span> <span style="color: darkgrey;">=</span> <span style="color: purple;">5</span><span style="color: darkgrey;">,</span>
<span style="color: teal;">[switch]</span><span style="color: orangered;">$DontBreakOnSuccess</span><span style="color: darkgrey;">,</span>
<span style="color: teal;">[switch]</span><span style="color: orangered;">$Starttime</span>
<span style="color: black;">)</span>
<span style="color: darkgreen;">#Write-Host "Using MaxBuildsPerDefinition $MaxBuildsPerDefinition"</span>
<span style="color: orangered;">$tfs</span> <span style="color: darkgrey;">=</span> <span style="color: teal;">[Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]</span><span style="color: darkgrey;">::</span><span style="color: black;">GetServer</span><span style="color: black;">(</span><span style="color: orangered;">$tfsuri</span><span style="color: black;">)</span>
<span style="color: orangered;">$buildServerType</span> <span style="color: darkgrey;">=</span> <span style="color: teal;">[Microsoft.TeamFoundation.Build.Client.IBuildServer]</span>
<span style="color: orangered;">$buildServer</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$tfs</span><span style="color: darkgrey;">.</span><span style="color: black;">GetService</span><span style="color: black;">(</span><span style="color: orangered;">$buildServerType</span><span style="color: black;">)</span><span style="color: black;">;</span>
<span style="color: orangered;">$buildDetailSpec</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$buildServer</span><span style="color: darkgrey;">.</span><span style="color: black;">CreateBuildDetailSpec</span><span style="color: black;">(</span><span style="color: orangered;">$project</span><span style="color: darkgrey;">,</span> <span style="color: orangered;">$buildDefinition</span><span style="color: black;">)</span><span style="color: black;">;</span>
<span style="color: darkblue;">if</span> <span style="color: black;">(</span><span style="color: orangered;">$Starttime</span><span style="color: black;">)</span>
<span style="color: black;">{</span>
<span style="color: orangered;">$buildDetailSpec</span><span style="color: darkgrey;">.</span><span style="color: black;">QueryOrder</span> <span style="color: darkgrey;">=</span> <span style="color: teal;">[Microsoft.TeamFoundation.Build.Client.BuildQueryOrder]</span><span style="color: darkgrey;">::</span><span style="color: black;">StartTimeDescending</span>
<span style="color: black;">}</span> <span style="color: darkblue;">else</span> <span style="color: black;">{</span>
<span style="color: orangered;">$buildDetailSpec</span><span style="color: darkgrey;">.</span><span style="color: black;">QueryOrder</span> <span style="color: darkgrey;">=</span> <span style="color: teal;">[Microsoft.TeamFoundation.Build.Client.BuildQueryOrder]</span><span style="color: darkgrey;">::</span><span style="color: black;">FinishTimeDescending</span>
<span style="color: black;">}</span>
<span style="color: orangered;">$buildDetailSpec</span><span style="color: darkgrey;">.</span><span style="color: black;">MaxBuildsPerDefinition</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$MaxBuildsPerDefinition</span>
<span style="color: darkblue;">try</span>
<span style="color: black;">{</span>
<span style="color: orangered;">$buildQueryResult</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$buildServer</span><span style="color: darkgrey;">.</span><span style="color: black;">QueryBuilds</span><span style="color: black;">(</span><span style="color: orangered;">$buildDetailSpec</span><span style="color: black;">)</span>
<span style="color: darkblue;">foreach</span> <span style="color: black;">(</span><span style="color: orangered;">$build</span> <span style="color: darkblue;">in</span> <span style="color: orangered;">$buildQueryResult</span><span style="color: darkgrey;">.</span><span style="color: black;">builds</span><span style="color: black;">)</span>
<span style="color: black;">{</span>
<span style="color: darkgreen;"># [Microsoft.TeamFoundation.Build.Client.IBuildDetail]$buildDetail = $buildQueryResult.builds[0] </span>
<span style="color: orangered;">$tfsBuildNumber</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$build</span><span style="color: darkgrey;">.</span><span style="color: black;">BuildNumber</span>
<span style="color: orangered;">$shortBuildNumber</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$tfsBuildNumber</span><span style="color: darkgrey;">.</span><span style="color: black;">Substring</span><span style="color: black;">(</span><span style="color: orangered;">$tfsBuildNumber</span><span style="color: darkgrey;">.</span><span style="color: black;">LastIndexOf</span><span style="color: black;">(</span><span style="color: darkred;">'_'</span><span style="color: black;">)</span><span style="color: darkgrey;">+</span><span style="color: purple;">1</span><span style="color: black;">)</span>
<span style="color: darkgreen;"># #$shortBuildNumber</span>
<span style="color: darkgreen;"># $buildQueryResult.failures.length</span>
<span style="color: darkgreen;"># $buildQueryResult.builds.length</span>
<span style="color: darkgreen;"># $buildQueryResult.builds[0].BuildNumber</span>
<span style="color: darkgreen;"># #$buildQueryResult.builds[0].Status</span>
<span style="color: darkgreen;"># #$buildQueryResult.builds[0].BuildDefinitionUri.AbsolutePath</span>
<span style="color: darkgreen;"># $buildQueryResult.builds[0].StartTime</span>
<span style="color: darkgreen;"># $buildQueryResult.builds[0].FinishTime</span>
<span style="color: orangered;">$buildAgent</span> <span style="color: darkgrey;">=</span> <span style="color: darkred;">'??'</span>
<span style="color: orangered;">$build</span><span style="color: darkgrey;">.</span><span style="color: black;">Information</span> <span style="color: darkgrey;">|</span> <span style="color: blue;">%</span> <span style="color: black;">{</span>
<span style="color: orangered;">$_</span><span style="color: darkgrey;">.</span><span style="color: black;">nodes</span> <span style="color: darkgrey;">|</span> <span style="color: blue;">%</span> <span style="color: black;">{</span>
<span style="color: darkblue;">try</span>
<span style="color: black;">{</span>
<span style="color: orangered;">$buildAgent</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$_</span><span style="color: darkgrey;">.</span><span style="color: black;">children</span><span style="color: darkgrey;">.</span><span style="color: black;">nodes</span><span style="color: darkgrey;">[</span><span style="color: purple;">2</span><span style="color: darkgrey;">]</span><span style="color: darkgrey;">.</span><span style="color: black;">fields</span><span style="color: darkgrey;">[</span><span style="color: darkred;">'ReservedAgentName'</span><span style="color: darkgrey;">]</span>
<span style="color: black;">}</span>
<span style="color: darkblue;">catch</span>
<span style="color: black;">{</span>
<span style="color: black;">}</span>
<span style="color: black;">}</span>
<span style="color: black;">}</span>
<span style="color: blue;">New-Object</span> <span style="color: blueviolet;">PSObject</span> <span style="color: navy;">-Property</span> <span style="color: black;">@{</span>
<span style="color: black;">Buildnumber</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$build</span><span style="color: darkgrey;">.</span><span style="color: black;">BuildNumber</span><span style="color: black;">;</span>
<span style="color: black;">Status</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$build</span><span style="color: darkgrey;">.</span><span style="color: black;">Status</span><span style="color: black;">;</span>
<span style="color: black;">CompilationStatus</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$build</span><span style="color: darkgrey;">.</span><span style="color: black;">CompilationStatus</span><span style="color: black;">;</span>
<span style="color: black;">Start</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$build</span><span style="color: darkgrey;">.</span><span style="color: black;">StartTime</span><span style="color: black;">;</span>
<span style="color: black;">End</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$build</span><span style="color: darkgrey;">.</span><span style="color: black;">FinishTime</span><span style="color: black;">;</span>
<span style="color: black;">DropLocation</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$build</span><span style="color: darkgrey;">.</span><span style="color: black;">DropLocation</span><span style="color: black;">;</span>
<span style="color: black;">SourceGetVersion</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$build</span><span style="color: darkgrey;">.</span><span style="color: black;">SourceGetVersion</span><span style="color: black;">;</span>
<span style="color: black;">BuildAgent</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$buildAgent</span>
<span style="color: black;">}</span>
<span style="color: darkblue;">if</span> <span style="color: black;">(</span><span style="color: orangered;">$build</span><span style="color: darkgrey;">.</span><span style="color: black;">Status</span> <span style="color: darkgrey;">-eq</span> <span style="color: darkred;">'Succeeded'</span> <span style="color: darkgrey;">-and</span> <span style="color: darkgrey;">!</span> <span style="color: orangered;">$DontBreakOnSuccess</span><span style="color: black;">)</span> <span style="color: black;">{</span> <span style="color: darkblue;">break</span> <span style="color: black;">}</span>
<span style="color: black;">}</span>
<span style="color: black;">}</span>
<span style="color: darkblue;">catch</span> <span style="color: teal;">[Exception]</span>
<span style="color: black;">{</span>
<span style="color: darkblue;">throw</span> <span style="color: darkred;">"TFS nicht erreichbar."</span>
<span style="color: black;">}</span>
<span style="color: black;">}</span>
<span style="color: orangered;">$a</span> <span style="color: darkgrey;">=</span> <span style="color: black;">@(</span><span style="color: blue;">Get-BuildInfo</span> <span style="color: orangered;">$buildDefinition</span><span style="color: black;">)</span>
<span style="color: orangered;">$a</span> <span style="color: darkgrey;">|</span> <span style="color: blue;">%</span> <span style="color: black;">{</span>
<span style="color: darkblue;">if</span> <span style="color: black;">(</span><span style="color: orangered;">$_</span><span style="color: darkgrey;">.</span><span style="color: black;">Status</span> <span style="color: darkgrey;">-eq</span> <span style="color: darkred;">'InProgress'</span><span style="color: black;">)</span> <span style="color: black;">{</span> <span style="color: orangered;">$end</span> <span style="color: darkgrey;">=</span> <span style="color: blue;">Get-Date</span> <span style="color: black;">}</span> <span style="color: darkblue;">else</span> <span style="color: black;">{</span> <span style="color: orangered;">$end</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$_</span><span style="color: darkgrey;">.</span><span style="color: black;">End</span> <span style="color: black;">}</span>
<span style="color: darkred;">"{0,-32} {1,18} {2:ddd} {3} - {4:T} {5,5:0} {6} {7,6} {8}"</span> <span style="color: darkgrey;">-f</span> <span style="color: orangered;">$_</span><span style="color: darkgrey;">.</span><span style="color: black;">Buildnumber</span><span style="color: darkgrey;">,</span> <span style="color: orangered;">$_</span><span style="color: darkgrey;">.</span><span style="color: black;">Status</span><span style="color: darkgrey;">,</span> <span style="color: orangered;">$_</span><span style="color: darkgrey;">.</span><span style="color: black;">Start</span><span style="color: darkgrey;">,</span> <span style="color: orangered;">$_</span><span style="color: darkgrey;">.</span><span style="color: black;">Start</span><span style="color: darkgrey;">,</span> <span style="color: orangered;">$End</span><span style="color: darkgrey;">,</span> <span style="color: black;">(</span><span style="color: orangered;">$End</span> <span style="color: darkgrey;">-</span><span style="color: orangered;">$_</span><span style="color: darkgrey;">.</span><span style="color: black;">Start</span><span style="color: black;">)</span><span style="color: darkgrey;">.</span><span style="color: black;">Totalminutes</span><span style="color: darkgrey;">,</span> <span style="color: orangered;">$_</span><span style="color: darkgrey;">.</span><span style="color: black;">SourceGetVersion</span><span style="color: darkgrey;">,</span> <span style="color: orangered;">$_</span><span style="color: darkgrey;">.</span><span style="color: black;">buildAgent</span><span style="color: darkgrey;">,</span> <span style="color: orangered;">$Droplocation</span>
<span style="color: black;">}</span></pre>
<br />
<div style="font-family: Arial,Helvetica,sans-serif;">
The result tells me start time, end time, duration, included change set and drop location.</div>
<div style="font-family: Arial,Helvetica,sans-serif;">
In the case of failed builds I some further checks on the log files not included here to dispatch the problem as quick as possible to the developer who caused the problem. </div>
<br />
<br />
<br />
<br />Bernd Krisziohttp://www.blogger.com/profile/03294997372899922987noreply@blogger.com0tag:blogger.com,1999:blog-8903006743421146366.post-62968104362162294052011-04-27T05:08:00.000-07:002011-04-27T05:08:38.967-07:00Search for Firefox Bookmarks with given combination of 1 to 3 keywordsWhen I found that Firefox stores its bookmarks in a sqlite database places.sqlite I wrote the following PowerShell script to search for bookmark by keyword combination.<br />
<br />
Here is the code:<br />
<br />
<pre class='PowerShellColorizedScript'><span style='color:#0000ff'>ipmo</span> <span style='color:#8a2be2'>WPK</span>
<span style='color:#00008b'>if</span> <span style='color:#000000'>(</span><span style='color:#a9a9a9'>!</span> <span style='color:#ff4500'>$sqlitedll</span><span style='color:#000000'>)</span>
<span style='color:#000000'>{</span>
<span style='color:#ff4500'>$sqlitedll</span> <span style='color:#a9a9a9'>=</span> <span style='color:#008080'>[System.Reflection.Assembly]</span><span style='color:#a9a9a9'>::</span><span style='color:#000000'>LoadFrom</span><span style='color:#000000'>(</span><span style='color:#8b0000'>"C:\Program Files\System.Data.SQLite\bin\System.Data.SQLite.dll"</span><span style='color:#000000'>)</span>
<span style='color:#000000'>}</span>
<span style='color:#ff4500'>$ConnectionString</span> <span style='color:#a9a9a9'>=</span> <span style='color:#8b0000'>"Data Source=C:\Var\sqlite_ff4\places.sqlite"</span>
<span style='color:#ff4500'>$conn</span> <span style='color:#a9a9a9'>=</span> <span style='color:#0000ff'>new-object</span> <span style='color:#8a2be2'>System.Data.SQLite.SQLiteConnection</span>
<span style='color:#ff4500'>$conn</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>ConnectionString</span> <span style='color:#a9a9a9'>=</span> <span style='color:#ff4500'>$ConnectionString</span>
<span style='color:#ff4500'>$conn</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>Open</span><span style='color:#000000'>(</span><span style='color:#000000'>)</span>
<span style='color:#00008b'>function</span> <span style='color:#8a2be2'>Invoke-sqlite</span>
<span style='color:#000000'>{</span>
<span style='color:#00008b'>param</span><span style='color:#000000'>(</span> <span style='color:#008080'>[string]</span><span style='color:#ff4500'>$sql</span><span style='color:#a9a9a9'>,</span>
<span style='color:#008080'>[System.Data.SQLite.SQLiteConnection]</span><span style='color:#ff4500'>$connection</span>
<span style='color:#000000'>)</span>
<span style='color:#ff4500'>$cmd</span> <span style='color:#a9a9a9'>=</span> <span style='color:#0000ff'>new-object</span> <span style='color:#8a2be2'>System.Data.SQLite.SQLiteCommand</span><span style='color:#000000'>(</span><span style='color:#ff4500'>$sql</span><span style='color:#a9a9a9'>,</span><span style='color:#ff4500'>$connection</span><span style='color:#000000'>)</span>
<span style='color:#ff4500'>$ds</span> <span style='color:#a9a9a9'>=</span> <span style='color:#0000ff'>New-Object</span> <span style='color:#8a2be2'>system.Data.DataSet</span>
<span style='color:#ff4500'>$da</span> <span style='color:#a9a9a9'>=</span> <span style='color:#0000ff'>New-Object</span> <span style='color:#8a2be2'>System.Data.SQLite.SQLiteDataAdapter</span><span style='color:#000000'>(</span><span style='color:#ff4500'>$cmd</span><span style='color:#000000'>)</span>
<span style='color:#ff4500'>$da</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>fill</span><span style='color:#000000'>(</span><span style='color:#ff4500'>$ds</span><span style='color:#000000'>)</span> <span style='color:#a9a9a9'>|</span> <span style='color:#0000ff'>Out-Null</span>
<span style='color:#00008b'>return</span> <span style='color:#ff4500'>$ds</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>tables</span><span style='color:#a9a9a9'>[</span><span style='color:#800080'>0</span><span style='color:#a9a9a9'>]</span>
<span style='color:#000000'>}</span>
<span style='color:#00008b'>function</span> <span style='color:#8a2be2'>Show-Bockmarks</span> <span style='color:#000000'>(</span><span style='color:#ff4500'>$resource</span><span style='color:#000000'>)</span> <span style='color:#000000'>{</span>
<span style='color:#0000ff'>New-Grid</span> <span style='color:#000080'>-Rows</span> <span style='color:#800080'>2</span> <span style='color:#000080'>-Columns</span> <span style='color:#800080'>1</span> <span style='color:#000080'>-width</span> <span style='color:#800080'>1400</span> <span style='color:#000080'>-hight</span> <span style='color:#800080'>1000</span> <span style='color:#000000'>{</span>
<span style='color:#0000ff'>New-StackPanel</span> <span style='color:#000080'>-Orientation</span> <span style='color:#8a2be2'>horizontal</span> <span style='color:#000080'>-column</span> <span style='color:#800080'>0</span> <span style='color:#000080'>-row</span> <span style='color:#800080'>0</span> <span style='color:#000080'>-Children</span> <span style='color:#000000'>{</span>
<span style='color:#0000ff'>New-Label</span> <span style='color:#8b0000'>'1. Keyword'</span>
<span style='color:#0000ff'>New-TextBox</span> <span style='color:#000080'>-Name</span> <span style='color:#8a2be2'>tag1</span> <span style='color:#000080'>-width</span> <span style='color:#800080'>200</span>
<span style='color:#0000ff'>New-Label</span> <span style='color:#8b0000'>'2. Keyword'</span>
<span style='color:#0000ff'>New-TextBox</span> <span style='color:#000080'>-Name</span> <span style='color:#8a2be2'>tag2</span> <span style='color:#000080'>-width</span> <span style='color:#800080'>200</span>
<span style='color:#0000ff'>New-Label</span> <span style='color:#8b0000'>'3. Keyword'</span>
<span style='color:#0000ff'>New-TextBox</span> <span style='color:#000080'>-Name</span> <span style='color:#8a2be2'>tag3</span> <span style='color:#000080'>-width</span> <span style='color:#800080'>200</span>
<span style='color:#0000ff'>New-Button</span> <span style='color:#000080'>-Name</span> <span style='color:#8a2be2'>Search</span> <span style='color:#8b0000'>"search"</span> <span style='color:#000080'>-On_Click</span> <span style='color:#000000'>{</span>
<span style='color:#ff4500'>$text1</span> <span style='color:#a9a9a9'>=</span> <span style='color:#ff4500'>$window</span> <span style='color:#a9a9a9'>|</span> <span style='color:#0000ff'>Get-ChildControl</span> <span style='color:#8a2be2'>Tag1</span>
<span style='color:#ff4500'>$tag1</span> <span style='color:#a9a9a9'>=</span> <span style='color:#ff4500'>$text1</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>Text</span>
<span style='color:#ff4500'>$text2</span> <span style='color:#a9a9a9'>=</span> <span style='color:#ff4500'>$window</span> <span style='color:#a9a9a9'>|</span> <span style='color:#0000ff'>Get-ChildControl</span> <span style='color:#8a2be2'>Tag2</span>
<span style='color:#ff4500'>$tag2</span> <span style='color:#a9a9a9'>=</span> <span style='color:#ff4500'>$text2</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>Text</span>
<span style='color:#ff4500'>$text3</span> <span style='color:#a9a9a9'>=</span> <span style='color:#ff4500'>$window</span> <span style='color:#a9a9a9'>|</span> <span style='color:#0000ff'>Get-ChildControl</span> <span style='color:#8a2be2'>Tag3</span>
<span style='color:#ff4500'>$tag3</span> <span style='color:#a9a9a9'>=</span> <span style='color:#ff4500'>$text3</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>Text</span>
<span style='color:#00008b'>if</span> <span style='color:#000000'>(</span> <span style='color:#ff4500'>$tag2</span> <span style='color:#a9a9a9'>-ne</span> <span style='color:#8b0000'>''</span><span style='color:#000000'>)</span> <span style='color:#000000'>{</span>
<span style='color:#ff4500'>$clause2</span> <span style='color:#a9a9a9'>=</span> <span style='color:#8b0000'>@"
join moz_bookmarks l2 on b.fk = l2.fk and b.id <> l2.id
join moz_bookmarks t2 on l2.parent = t2.id and t2.parent = 4 and upper(t2.title) = upper('$tag2')
"@</span>
<span style='color:#000000'>}</span> <span style='color:#00008b'>else</span> <span style='color:#000000'>{</span> <span style='color:#ff4500'>$clause2</span> <span style='color:#a9a9a9'>=</span> <span style='color:#8b0000'>''</span> <span style='color:#000000'>}</span>
<span style='color:#00008b'>if</span> <span style='color:#000000'>(</span> <span style='color:#ff4500'>$tag3</span> <span style='color:#a9a9a9'>-ne</span> <span style='color:#8b0000'>''</span><span style='color:#000000'>)</span> <span style='color:#000000'>{</span>
<span style='color:#ff4500'>$clause3</span> <span style='color:#a9a9a9'>=</span> <span style='color:#8b0000'>@"
join moz_bookmarks l3 on b.fk = l3.fk and b.id <> l3.id
join moz_bookmarks t3 on l3.parent = t3.id and t3.parent = 4 and upper(t3.title) = upper('$tag3')
"@</span>
<span style='color:#000000'>}</span> <span style='color:#00008b'>else</span> <span style='color:#000000'>{</span> <span style='color:#ff4500'>$clause3</span> <span style='color:#a9a9a9'>=</span> <span style='color:#8b0000'>''</span> <span style='color:#000000'>}</span>
<span style='color:#ff4500'>$ff_sql</span> <span style='color:#a9a9a9'>=</span> <span style='color:#8b0000'>@"
SELECT b.title, datetime (b.dateAdded / 1000000, 'unixepoch', 'localtime') dateAdded , p.url
from moz_bookmarks b
join moz_bookmarks l1 on b.fk = l1.fk and b.id <> l1.id
join moz_bookmarks t1 on l1.parent = t1.id and t1.parent = 4 and upper(t1.title) = upper('$tag1')
join moz_places p on b.fk = p.id $clause2 $clause3
where b.title is not null and b.type = 1
"@</span>
<span style='color:#ff4500'>$conn</span> <span style='color:#a9a9a9'>=</span> <span style='color:#ff4500'>$resource</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>conn</span>
<span style='color:#ff4500'>$window</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>Title</span> <span style='color:#a9a9a9'>=</span> <span style='color:#8b0000'>"$($conn.database) Database Browser"</span>
<span style='color:#ff4500'>$TableView</span> <span style='color:#a9a9a9'>=</span> <span style='color:#ff4500'>$window</span> <span style='color:#a9a9a9'>|</span> <span style='color:#0000ff'>Get-ChildControl</span> <span style='color:#8a2be2'>TableView</span>
<span style='color:#ff4500'>$TableView</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>ItemsSource</span> <span style='color:#a9a9a9'>=</span> <span style='color:#000000'>@(</span><span style='color:#0000ff'>Invoke-sqlite</span> <span style='color:#000080'>-sql</span> <span style='color:#ff4500'>$ff_sql</span> <span style='color:#000080'>-connection</span> <span style='color:#ff4500'>$conn</span><span style='color:#000000'>)</span>
<span style='color:#000000'>}</span>
<span style='color:#0000ff'>New-Button</span> <span style='color:#000080'>-Name</span> <span style='color:#8a2be2'>Cancel</span> <span style='color:#8b0000'>"Close"</span> <span style='color:#000080'>-On_Click</span> <span style='color:#000000'>{</span><span style='color:#ff4500'>$window</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>Close</span><span style='color:#000000'>(</span><span style='color:#000000'>)</span><span style='color:#000000'>}</span>
<span style='color:#000000'>}</span>
<span style='color:#0000ff'>New-ListView</span> <span style='color:#000080'>-Column</span> <span style='color:#800080'>0</span> <span style='color:#000080'>-Row</span> <span style='color:#800080'>1</span> <span style='color:#000080'>-Name</span> <span style='color:#8a2be2'>TableView</span> <span style='color:#000080'>-View</span> <span style='color:#000000'>{</span>
<span style='color:#0000ff'>New-GridView</span> <span style='color:#000080'>-AllowsColumnReorder</span> <span style='color:#000080'>-Columns</span> <span style='color:#000000'>{</span>
<span style='color:#0000ff'>New-GridViewColumn</span> <span style='color:#8b0000'>"title"</span>
<span style='color:#0000ff'>New-GridViewColumn</span> <span style='color:#8b0000'>"dateAdded"</span>
<span style='color:#0000ff'>New-GridViewColumn</span> <span style='color:#8b0000'>"url"</span>
<span style='color:#000000'>}</span>
<span style='color:#000000'>}</span> <span style='color:#000080'>-On_SelectionChanged</span> <span style='color:#000000'>{</span>
<span style='color:#0000ff'>start</span> <span style='color:#ff4500'>$this</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>selecteditem</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>url</span>
<span style='color:#000000'>}</span>
<span style='color:#006400'>#}</span>
<span style='color:#000000'>}</span> <span style='color:#000080'>-asjob</span> <span style='color:#000080'>-Resource</span> <span style='color:#ff4500'>$resource</span>
<span style='color:#000000'>}</span>
<span style='color:#0000ff'>Show-Bockmarks</span> <span style='color:#000080'>-resource</span> <span style='color:#000000'>@{</span><span style='color:#000000'>conn</span> <span style='color:#a9a9a9'>=</span> <span style='color:#ff4500'>$conn</span><span style='color:#000000'>}</span>
</pre><br />
You have to install <b>System.Data.SQLite</b><br />
from http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki<br />
<br />
I had problems when I checked the install in GAC option. Therefore I installed it without that option and supply the absolute path in my script.<br />
<br />
Note: You can't open places.sqlite while Firefox is running. I copied it and coded the path of the copy into my script.<br />
<br />
Now I can enter a keyword, press search and the grid fills. Double clicking starts the bookmark in the default browser.Bernd Krisziohttp://www.blogger.com/profile/03294997372899922987noreply@blogger.com0tag:blogger.com,1999:blog-8903006743421146366.post-39643498245904401712010-11-20T23:46:00.000-08:002010-11-21T04:00:09.049-08:00Eval-Selection in ISE editor, result in editor paneHere is a little demo showing, who to use ISEs editor pane to display untruncated, unwrapped results.<br />
<br />
Run the script in ISE.<br />
Open a new editor.<br />
Tpye dir and press F7:<br />
<br />
<br />
<br />
<pre class="PowerShellColorizedScript">
<span style="color: darkblue;">function</span> <span style="color: blueviolet;">Eval-Selection</span>
<span style="color: black;">{</span>
<span style="color: darkgreen;"># Bernd Kriszio 2010-11-21</span>
<span style="color: darkgreen;"># http://pauerschell.blogspot.com/</span>
<span style="color: darkgreen;"># twitter @bernd_k</span>
<span style="color: orangered;">$editor</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$psise</span><span style="color: darkgrey;">.</span><span style="color: black;">CurrentFile</span><span style="color: darkgrey;">.</span><span style="color: black;">Editor</span>
<span style="color: darkblue;">if</span> <span style="color: black;">(</span><span style="color: orangered;">$editor</span><span style="color: darkgrey;">.</span><span style="color: black;">SelectedText</span><span style="color: black;">)</span>
<span style="color: black;">{</span>
<span style="color: orangered;">$inputScript</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$editor</span><span style="color: darkgrey;">.</span><span style="color: black;">SelectedText</span>
<span style="color: orangered;">$editor</span><span style="color: darkgrey;">.</span><span style="color: black;">InsertText</span><span style="color: black;">(</span><span style="color: darkred;">''</span><span style="color: black;">)</span>
<span style="color: orangered;">$editor</span><span style="color: darkgrey;">.</span><span style="color: black;">InsertText</span><span style="color: black;">(</span><span style="color: orangered;">$inputScript</span><span style="color: black;">)</span>
<span style="color: orangered;">$result</span> <span style="color: darkgrey;">=</span> <span style="color: blue;">Invoke-expression</span> <span style="color: orangered;">$inputScript</span> <span style="color: darkgrey;">|</span> <span style="color: blue;">out-String</span>
<span style="color: orangered;">$editor</span><span style="color: darkgrey;">.</span><span style="color: black;">insertText</span><span style="color: black;">(</span><span style="color: darkred;">"`r`n"</span><span style="color: black;">)</span>
<span style="color: orangered;">$editor</span><span style="color: darkgrey;">.</span><span style="color: black;">InsertText</span><span style="color: black;">(</span><span style="color: orangered;">$result</span><span style="color: black;">)</span>
<span style="color: black;">}</span>
<span style="color: darkblue;">else</span>
<span style="color: black;">{</span>
<span style="color: orangered;">$inputScript</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$editor</span><span style="color: darkgrey;">.</span><span style="color: black;">Text</span>
<span style="color: orangered;">$EndLine</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$editor</span><span style="color: darkgrey;">.</span><span style="color: black;">LineCount</span>
<span style="color: orangered;">$EndColumn</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$editor</span><span style="color: darkgrey;">.</span><span style="color: black;">GetLineLength</span><span style="color: black;">(</span><span style="color: orangered;">$EndLine</span><span style="color: black;">)</span> <span style="color: darkgrey;">+</span> <span style="color: purple;">1</span>
<span style="color: orangered;">$editor</span><span style="color: darkgrey;">.</span><span style="color: black;">SetCaretPosition</span><span style="color: black;">(</span><span style="color: orangered;">$EndLine</span><span style="color: darkgrey;">,</span> <span style="color: orangered;">$EndColumn</span><span style="color: black;">)</span>
<span style="color: orangered;">$result</span> <span style="color: darkgrey;">=</span> <span style="color: blue;">Invoke-expression</span> <span style="color: orangered;">$inputScript</span> <span style="color: darkgrey;">|</span> <span style="color: blue;">out-String</span>
<span style="color: orangered;">$editor</span><span style="color: darkgrey;">.</span><span style="color: black;">insertText</span><span style="color: black;">(</span><span style="color: darkred;">"`r`n"</span><span style="color: black;">)</span>
<span style="color: orangered;">$editor</span><span style="color: darkgrey;">.</span><span style="color: black;">InsertText</span><span style="color: black;">(</span><span style="color: orangered;">$result</span><span style="color: black;">)</span>
<span style="color: black;">}</span>
<span style="color: black;">}</span>
<span style="color: orangered;">$psISE</span><span style="color: darkgrey;">.</span><span style="color: black;">CurrentPowerShellTab</span><span style="color: darkgrey;">.</span><span style="color: black;">AddOnsMenu</span><span style="color: darkgrey;">.</span><span style="color: black;">Submenus</span><span style="color: darkgrey;">.</span><span style="color: black;">Add</span><span style="color: black;">(</span><span style="color: darkred;">"Eval-Selection"</span><span style="color: darkgrey;">,</span> <span style="color: black;">{</span><span style="color: blue;">Eval-Selection</span><span style="color: black;">}</span> <span style="color: darkgrey;">,</span> <span style="color: darkred;">'f7'</span><span style="color: black;">)</span></pre><br />
<b>Edited</b><br />
After playing a while, I modified the rules for the case, when nothing is selected. Now I use just the single line, the caret is in. Further I added the -width 1000 parameter to make it realy an improvement about usual output-pane results.<br />
<br />
And I have a lot of crazy ideas, what to do if the caret is in an empty line. I don't implement them. Next month I wouldn't remember them myself.<br />
<br />
Keep things simple<br />
<br />
Bernd<br />
<br />
This is the version, I added to my profile:<br />
<br />
<pre class='PowerShellColorizedScript'><span style='color:#00008b'>function</span> <span style='color:#8a2be2'>Eval-Selection</span>
<span style='color:#000000'>{</span>
<span style='color:#006400'># Bernd Kriszio 2010-11-21</span>
<span style='color:#006400'># http://pauerschell.blogspot.com/</span>
<span style='color:#006400'># twitter @bernd_k</span>
<span style='color:#ff4500'>$editor</span> <span style='color:#a9a9a9'>=</span> <span style='color:#ff4500'>$psise</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>CurrentFile</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>Editor</span>
<span style='color:#006400'># if nothing is selected just use the line the caret is in</span>
<span style='color:#00008b'>if</span> <span style='color:#000000'>(</span><span style='color:#a9a9a9'>!</span><span style='color:#ff4500'>$editor</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>SelectedText</span><span style='color:#000000'>)</span>
<span style='color:#000000'>{</span>
<span style='color:#ff4500'>$caretLine</span> <span style='color:#a9a9a9'>=</span> <span style='color:#ff4500'>$editor</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>CaretLine</span>
<span style='color:#ff4500'>$caretLineEnd</span> <span style='color:#a9a9a9'>=</span> <span style='color:#ff4500'>$editor</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>GetLineLength</span><span style='color:#000000'>(</span><span style='color:#ff4500'>$caretLine</span><span style='color:#000000'>)</span> <span style='color:#a9a9a9'>+</span> <span style='color:#800080'>1</span>
<span style='color:#ff4500'>$editor</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>Select</span><span style='color:#000000'>(</span><span style='color:#ff4500'>$caretLine</span><span style='color:#a9a9a9'>,</span> <span style='color:#800080'>1</span><span style='color:#a9a9a9'>,</span> <span style='color:#ff4500'>$caretLine</span><span style='color:#a9a9a9'>,</span> <span style='color:#ff4500'>$caretLineEnd</span><span style='color:#000000'>)</span>
<span style='color:#000000'>}</span>
<span style='color:#006400'># if something is selected use it.</span>
<span style='color:#00008b'>if</span> <span style='color:#000000'>(</span><span style='color:#ff4500'>$editor</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>SelectedText</span><span style='color:#000000'>)</span>
<span style='color:#000000'>{</span>
<span style='color:#ff4500'>$inputScript</span> <span style='color:#a9a9a9'>=</span> <span style='color:#ff4500'>$editor</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>SelectedText</span>
<span style='color:#ff4500'>$editor</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>InsertText</span><span style='color:#000000'>(</span><span style='color:#8b0000'>''</span><span style='color:#000000'>)</span>
<span style='color:#ff4500'>$editor</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>InsertText</span><span style='color:#000000'>(</span><span style='color:#ff4500'>$inputScript</span><span style='color:#000000'>)</span>
<span style='color:#ff4500'>$result</span> <span style='color:#a9a9a9'>=</span> <span style='color:#0000ff'>Invoke-expression</span> <span style='color:#ff4500'>$inputScript</span> <span style='color:#a9a9a9'>|</span> <span style='color:#0000ff'>out-String</span> <span style='color:#000080'>-width</span> <span style='color:#800080'>1000</span>
<span style='color:#00008b'>if</span> <span style='color:#000000'>(</span><span style='color:#ff4500'>$editor</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>CaretColumn</span> <span style='color:#a9a9a9'>-ne</span> <span style='color:#800080'>1</span><span style='color:#000000'>)</span>
<span style='color:#000000'>{</span>
<span style='color:#ff4500'>$editor</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>insertText</span><span style='color:#000000'>(</span><span style='color:#8b0000'>"`r`n"</span><span style='color:#000000'>)</span>
<span style='color:#000000'>}</span>
<span style='color:#ff4500'>$editor</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>InsertText</span><span style='color:#000000'>(</span><span style='color:#ff4500'>$result</span><span style='color:#000000'>)</span>
<span style='color:#000000'>}</span>
<span style='color:#000000'>}</span>
</pre>Bernd Krisziohttp://www.blogger.com/profile/03294997372899922987noreply@blogger.com0tag:blogger.com,1999:blog-8903006743421146366.post-82279004947569185612010-11-16T22:33:00.000-08:002010-11-16T22:33:54.802-08:00Determine Start and End of a Selection in PowerShell IseHello, I' m just working on a new inline outpout mode for <a href="http://sqlpsx.codeplex.com/">SQLPSX</a> a very ambious project to access SQL databases based on PowerShell.<br />
<br />
There ISEs Output pane doesn't always satisfy my needs, because it truncates and wraps output but you can put your Output into the current or new Editor panes and that works fine. Today I focus on using the current editor pane. <br />
<br />
My goal is to insert the output exactly at the starting of the line below the selection if there is one or at the end of the editor.<br />
<br />
First I found it difficult to get the start and end of a selection, but than I found a clever work around. I can even determine, if a selection was done from left to right or from right to left.<br />
<br />
But don't use this possibility. It would be really confusing.<br />
<br />
Here is my test code, with which I designed (honestly redesigned) the logic to evaluate the output location in SQLPSX (next release).<br />
<br />
Have fun studying it. Just put the code below it into an ISE editor, select some text (or nothing) and press F5.<br />
<br />
Bernd<br />
<br />
<br />
<pre class="PowerShellColorizedScript"><span style="color: darkblue;">function</span> <span style="color: blueviolet;">Get-InfoAboutCurentFilesEditor</span>
<span style="color: black;">{</span>
<span style="color: orangered;">$editor</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$psIse</span><span style="color: darkgrey;">.</span><span style="color: black;">CurrentFile</span><span style="color: darkgrey;">.</span><span style="color: black;">Editor</span>
<span style="color: orangered;">$LineCount</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$editor</span><span style="color: darkgrey;">.</span><span style="color: black;">LineCount</span>
<span style="color: orangered;">$CaretLine</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$editor</span><span style="color: darkgrey;">.</span><span style="color: black;">CaretLine</span>
<span style="color: orangered;">$caretColumn</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$editor</span><span style="color: darkgrey;">.</span><span style="color: black;">CaretColumn</span>
<span style="color: orangered;">$CaretLineLength</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$editor</span><span style="color: darkgrey;">.</span><span style="color: black;">GetLineLength</span><span style="color: black;">(</span><span style="color: orangered;">$CaretLine</span><span style="color: black;">)</span>
<span style="color: orangered;">$CaretAtLineEnd</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$caretColumn</span> <span style="color: darkgrey;">-eq</span> <span style="color: orangered;">$CaretLineLength</span> <span style="color: darkgrey;">+</span> <span style="color: purple;">1</span>
<span style="color: orangered;">$CaretInLastLine</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$CaretLine</span> <span style="color: darkgrey;">-eq</span> <span style="color: orangered;">$LineCount</span>
<span style="color: orangered;">$SelectedText</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$editor</span><span style="color: darkgrey;">.</span><span style="color: black;">SelectedText</span>
<span style="color: orangered;">$hasSelection</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$SelectedText</span> <span style="color: darkgrey;">-ne</span> <span style="color: darkred;">''</span>
<span style="color: darkblue;">if</span> <span style="color: black;">(</span><span style="color: orangered;">$hasSelection</span><span style="color: black;">)</span>
<span style="color: black;">{</span>
<span style="color: darkgreen;"># delete selected text</span>
<span style="color: orangered;">$editor</span><span style="color: darkgrey;">.</span><span style="color: black;">InsertText</span><span style="color: black;">(</span><span style="color: darkred;">''</span><span style="color: black;">)</span>
<span style="color: orangered;">$StartLine</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$editor</span><span style="color: darkgrey;">.</span><span style="color: black;">CaretLine</span>
<span style="color: orangered;">$StartColumn</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$editor</span><span style="color: darkgrey;">.</span><span style="color: black;">CaretColumn</span>
<span style="color: darkgreen;"># reinsert the text</span>
<span style="color: orangered;">$editor</span><span style="color: darkgrey;">.</span><span style="color: black;">InsertText</span><span style="color: black;">(</span><span style="color: orangered;">$SelectedText</span><span style="color: black;">)</span>
<span style="color: orangered;">$EndLine</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$editor</span><span style="color: darkgrey;">.</span><span style="color: black;">CaretLine</span>
<span style="color: orangered;">$EndColumn</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$editor</span><span style="color: darkgrey;">.</span><span style="color: black;">CaretColumn</span>
<span style="color: darkgreen;"># restore the selection</span>
<span style="color: orangered;">$editor</span><span style="color: darkgrey;">.</span><span style="color: black;">select</span><span style="color: black;">(</span><span style="color: orangered;">$StartLine</span><span style="color: darkgrey;">,</span> <span style="color: orangered;">$StartColumn</span><span style="color: darkgrey;">,</span> <span style="color: orangered;">$EndLine</span><span style="color: darkgrey;">,</span> <span style="color: orangered;">$EndColumn</span><span style="color: black;">)</span>
<span style="color: orangered;">$reverse</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$CaretLine</span> <span style="color: darkgrey;">-eq</span> <span style="color: orangered;">$StartLine</span> <span style="color: darkgrey;">-and</span> <span style="color: orangered;">$caretColumn</span> <span style="color: darkgrey;">-eq</span> <span style="color: orangered;">$StartColumn</span>
<span style="color: black;">}</span>
<span style="color: darkblue;">else</span>
<span style="color: black;">{</span>
<span style="color: orangered;">$StartLine</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$EndLine</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$LineCount</span>
<span style="color: orangered;">$StartColumn</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$EndColumn</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$editor</span><span style="color: darkgrey;">.</span><span style="color: black;">GetLineLength</span><span style="color: black;">(</span><span style="color: orangered;">$EndLine</span><span style="color: black;">)</span> <span style="color: darkgrey;">+</span> <span style="color: purple;">1</span>
<span style="color: orangered;">$reverse</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$False</span>
<span style="color: black;">}</span>
<span style="color: darkblue;">if</span> <span style="color: black;">(</span> <span style="color: orangered;">$EndLine</span> <span style="color: darkgrey;">-lt</span> <span style="color: orangered;">$LineCount</span><span style="color: black;">)</span>
<span style="color: black;">{</span>
<span style="color: orangered;">$outputLine</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$EndLine</span> <span style="color: darkgrey;">+</span> <span style="color: purple;">1</span>
<span style="color: orangered;">$outputColumn</span> <span style="color: darkgrey;">=</span> <span style="color: purple;">1</span>
<span style="color: orangered;">$NeedsNewLine</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$False</span>
<span style="color: black;">}</span>
<span style="color: darkblue;">else</span>
<span style="color: black;">{</span>
<span style="color: orangered;">$outputLine</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$EndLine</span>
<span style="color: orangered;">$outputColumn</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$EndColumn</span>
<span style="color: darkblue;">if</span> <span style="color: black;">(</span><span style="color: orangered;">$EndColumn</span> <span style="color: darkgrey;">-ne</span> <span style="color: purple;">1</span><span style="color: black;">)</span>
<span style="color: black;">{</span>
<span style="color: orangered;">$NeedsNewLine</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$True</span>
<span style="color: orangered;">$outputColumn</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$editor</span><span style="color: darkgrey;">.</span><span style="color: black;">GetLineLength</span><span style="color: black;">(</span><span style="color: orangered;">$EndLine</span><span style="color: black;">)</span> <span style="color: darkgrey;">+</span> <span style="color: purple;">1</span>
<span style="color: black;">}</span>
<span style="color: darkblue;">else</span>
<span style="color: black;">{</span>
<span style="color: orangered;">$NeedsNewLine</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$False</span>
<span style="color: black;">}</span>
<span style="color: black;">}</span>
<span style="color: darkred;">@"
DisplayName: $($psIse.CurrentFile.DisplayName)
LineCount: $LineCount
CaretLine: $CaretLine
CaretColumn: $caretColumn
CaretLineLength: $CaretLineLength
CaretAtLineEnd: $CaretAtLineEnd
CaretInLastLine: $CaretInLastLine
hasSelection: $hasSelection
reverse: $reverse
outputLine: $outputLine
outputColumn: $outputColumn
NeedsNewLine: $NeedsNewLine
"@</span>
<span style="color: black;">}</span>
<span style="color: blue;">Get-InfoAboutCurentFilesEditor</span></pre>Bernd Krisziohttp://www.blogger.com/profile/03294997372899922987noreply@blogger.com0tag:blogger.com,1999:blog-8903006743421146366.post-23567646870925273222010-11-15T06:54:00.000-08:002010-11-15T06:54:07.707-08:00New ISE work arounds -- determine start and end of textselectionI just guess that ISE is made by some Trappist monks. One never hears anything about future versions.<br />
At most some closed Change Request at Microsoft Commit.<br />
I get a little tired hoping, they will release anything soon.<br />
If I need something, I'm going to write some dirty workaround.<br />
Current problem: you can select text within the editor from left to right or from right to left ending with the caret at different positions.<br />
Only the holy men didn't publish the API to get the start and the end of the selection. You can query only the caret position.<br />
What I want acchieve, is to be able to insert in the line just below the current selection.<br />
Well the following work around sets the caret to the beginning of the selection.<br />
Put the code into an ise editor select testa and press f5, than repeat it by selecting the tesxt in the oposite direction and press again f5:<br />
<br />
<br />
<pre class="PowerShellColorizedScript"><span style="color: orangered;">$editor</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$psise</span><span style="color: darkgrey;">.</span><span style="color: black;">CurrentFile</span><span style="color: darkgrey;">.</span><span style="color: black;">Editor</span>
<span style="color: orangered;">$currentselection</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$editor</span><span style="color: darkgrey;">.</span><span style="color: black;">SelectedText</span>
<span style="color: darkblue;">if</span> <span style="color: black;">(</span><span style="color: orangered;">$currentselection</span><span style="color: black;">)</span>
<span style="color: black;">{</span>
<span style="color: orangered;">$initialCaretLine</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$editor</span><span style="color: darkgrey;">.</span><span style="color: black;">CaretLine</span>
<span style="color: orangered;">$initialCaretColumn</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$editor</span><span style="color: darkgrey;">.</span><span style="color: black;">CaretColumn</span>
<span style="color: orangered;">$editor</span><span style="color: darkgrey;">.</span><span style="color: black;">InsertText</span><span style="color: black;">(</span><span style="color: darkred;">''</span><span style="color: black;">)</span>
<span style="color: orangered;">$selstartline</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$editor</span><span style="color: darkgrey;">.</span><span style="color: black;">CaretLine</span>
<span style="color: orangered;">$selstartColumn</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$editor</span><span style="color: darkgrey;">.</span><span style="color: black;">CaretColumn</span>
<span style="color: orangered;">$editor</span><span style="color: darkgrey;">.</span><span style="color: black;">InsertText</span><span style="color: black;">(</span><span style="color: orangered;">$currentselection</span><span style="color: black;">)</span>
<span style="color: orangered;">$selEndline</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$editor</span><span style="color: darkgrey;">.</span><span style="color: black;">CaretLine</span>
<span style="color: orangered;">$selEndColumn</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$editor</span><span style="color: darkgrey;">.</span><span style="color: black;">CaretColumn</span>
<span style="color: orangered;">$editor</span><span style="color: darkgrey;">.</span><span style="color: black;">Select</span><span style="color: black;">(</span><span style="color: orangered;">$selstartline</span><span style="color: darkgrey;">,</span> <span style="color: orangered;">$selstartColumn</span><span style="color: darkgrey;">,</span> <span style="color: orangered;">$selEndline</span><span style="color: darkgrey;">,</span> <span style="color: orangered;">$selEndColumn</span><span style="color: black;">)</span>
<span style="color: darkred;">"Selection is ($selstartline, $selstartColumn, $selEndline, $selEndColumn)"</span>
<span style="color: darkred;">"Initial Caret Position ($initialCaretLine, $initialCaretColumn)"</span>
<span style="color: black;">}</span>
<span style="color: darkgreen;"># testa</span>
<span style="color: darkgreen;"># testb</span> </pre><br />
<br />
<br />
Hope this really helps<br />
<br />
Bernd<br />
<span class="Apple-style-span" style="border-collapse: separate; color: black; font-family: sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><span class="Apple-style-span" style="line-height: 24px;"><h1 class="firstHeading" id="firstHeading" style="background-image: none; border-bottom: 1px solid rgb(170, 170, 170); color: black; font-size: 1.6em; font-weight: normal; line-height: 1.2em; margin: 0px 0px 0.1em; padding-bottom: 0px; padding-top: 0px; width: auto;"><br />
</h1></span></span>Bernd Krisziohttp://www.blogger.com/profile/03294997372899922987noreply@blogger.com0tag:blogger.com,1999:blog-8903006743421146366.post-31453702129504318322010-11-14T09:21:00.000-08:002010-11-14T09:21:53.844-08:00I can code Bad Influenced style tooI hope there is a better way to do this, but if PowerShell enforces the use of work arounds, I'll use them.<br />
Please, if you find something more elegant, please drop a note. <br />
<br />
Well to make <a href="http://sqlpsx.codeplex.com/">SQLPSX</a> one of the most powerfull tools to extract data from SQL-Server or Oracle databases I just want to transform a datarow into a tabular representation.<br />
<br />
<br />
$datarow | Format-Table -auto<br />
<br />
seems a good start, but as described in <a href="http://poshoholic.com/2010/11/11/powershell-quick-tip-creating-wide-tables-with-powershell/">http://poshoholic.com/2010/11/11/powershell-quick-tip-creating-wide-tables-with-powershell/</a><br />
<br />
you need to pass the result to Out-String -width 10000 or as many characters you want.<br />
Sorry can't say -1 for unlimited. (Perhaps in V3, if we cry load enough?) <br />
<br />
Oops not all columns included, lets add -property *<br />
<br />
$datarow | Format-Table -auto -property * | Out-String -width 10000<br />
<br />
Oops what happens now? At the end you get some unwanted addition columns:<br />
<br />
RowError, RowState, Table, ItemArray, HasErrors<br />
<br />
A problem well described in <a href="https://connect.microsoft.com/PowerShell/feedback/details/400549/dataset-tables-property-returns-additional-properties-in-datarow-in-v2-ctp-3-not-in-v1?wa=wsignin1.0">https://connect.microsoft.com.</a> With no work around supplied.<br />
<br />
Go there and vote for it. <br />
<br />
And here is the code, that gives me the wanted result. I agree, it is ugly, but its result is fine:<br />
<br />
<br />
<pre class="PowerShellColorizedScript"><span style="color: orangered;">$columns</span> <span style="color: darkgrey;">=</span> <span style="color: darkred;">''</span>
<span style="color: darkblue;">foreach</span> <span style="color: black;">(</span><span style="color: orangered;">$i</span> <span style="color: darkblue;">in</span> <span style="color: purple;">0</span><span style="color: darkgrey;">..</span> <span style="color: black;">(</span><span style="color: orangered;">$res</span><span style="color: darkgrey;">[</span><span style="color: purple;">0</span><span style="color: darkgrey;">]</span><span style="color: darkgrey;">.</span><span style="color: black;">Table</span><span style="color: darkgrey;">.</span><span style="color: black;">columns</span><span style="color: darkgrey;">.</span><span style="color: black;">count</span> <span style="color: darkgrey;">-</span><span style="color: purple;">1</span><span style="color: black;">)</span><span style="color: black;">)</span>
<span style="color: black;">{</span>
<span style="color: darkblue;">if</span> <span style="color: black;">(</span><span style="color: orangered;">$columns</span><span style="color: black;">)</span> <span style="color: black;">{</span> <span style="color: orangered;">$columns</span> <span style="color: darkgrey;">+=</span> <span style="color: darkred;">', '</span><span style="color: darkgrey;">+</span> <span style="color: orangered;">$res</span><span style="color: darkgrey;">[</span><span style="color: purple;">0</span><span style="color: darkgrey;">]</span><span style="color: darkgrey;">.</span><span style="color: black;">Table</span><span style="color: darkgrey;">.</span><span style="color: black;">Columns</span><span style="color: darkgrey;">[</span><span style="color: orangered;">$i</span><span style="color: darkgrey;">]</span><span style="color: darkgrey;">.</span><span style="color: black;">ColumnName</span> <span style="color: black;">}</span>
<span style="color: darkblue;">else</span> <span style="color: black;">{</span> <span style="color: orangered;">$columns</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$res</span><span style="color: darkgrey;">[</span><span style="color: purple;">0</span><span style="color: darkgrey;">]</span><span style="color: darkgrey;">.</span><span style="color: black;">Table</span><span style="color: darkgrey;">.</span><span style="color: black;">Columns</span><span style="color: darkgrey;">[</span><span style="color: orangered;">$i</span><span style="color: darkgrey;">]</span><span style="color: darkgrey;">.</span><span style="color: black;">ColumnName</span><span style="color: black;">}</span>
<span style="color: black;">}</span>
<span style="color: orangered;">$c</span> <span style="color: darkgrey;">=</span> <span style="color: darkred;">'($res | ft -Property '</span> <span style="color: darkgrey;">+</span> <span style="color: orangered;">$columns</span> <span style="color: darkgrey;">+</span> <span style="color: darkred;">' -auto | Out-string -width 10000 -stream ) -replace " *$", ""-replace "\.\.\.$", "" -join "`r`n" '</span>
<span style="color: orangered;">$text</span> <span style="color: darkgrey;">=</span> <span style="color: blue;">Invoke-expression</span> <span style="color: orangered;">$c</span> </pre><br />
I hope this helps<br />
<br />
BerndBernd Krisziohttp://www.blogger.com/profile/03294997372899922987noreply@blogger.com7tag:blogger.com,1999:blog-8903006743421146366.post-9143630617291040982010-11-13T06:16:00.000-08:002010-11-13T06:16:52.533-08:00SQLPSX Release 2.3 is real tool for SQL-ServerWell it took some time, until <a href="http://sev17.com/">Chad Miller</a> published the current version of SQLPSX.<br />
Concerning SQL-Server I dare say, it is a real tool to work with, with some unique features.<br />
<br />
You get a lot of options for output format, e.g. list, which showes you the contents of the columns above each other. That helps, if you have varchar fields containing multiple lines. While the usual grid format only displays the first line and the text format looses column alignment, here you get nearly the complete content.<br />
Nearly? Yes using ISE Output window, some wise man desided that truncating the output to current width of the Output window is what users have to want.<br />
<br />
OK. With the limitations of the output window you have two options. First you can tweet about this last millenium decision. Second you can pray to Microsoft Connect and send a change Request for an option to programmatically set the witdth of the output pane).<br />
Or you just ignore the two options and write a workaround.<br />
<br />
The workaround I invented is the output format isetab. (Yes you find it in SQLPSX 2.3)<br />
The trick is not using the Output pane for displaying the result, but to create a new editor and display it there in its whole witdh.<br />
<br />
Working with the new mode, I soon end with lots of ofen editors and nearly as a joke to a coworker I designed the idea of an inline output format. I put the result into the calling editor, just below the calling selection or at the end, if there is none.<br />
<br />
First trial was just 4 lines and it just felt good.<br />
<br />
You can adjust the princip, if you have a function, which you invoke by ISE menu, which produces some wide output. Instead of sending the output to the output pane, you can use something similar to:<br />
<br />
<br />
<pre class="PowerShellColorizedScript"><span style="color: orangered;">$text</span> <span style="color: darkgrey;">=</span> <span style="color: darkred;">"your code goes here>"</span>
<span style="color: orangered;">$editor</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$psise</span><span style="color: darkgrey;">.</span><span style="color: black;">CurrentFile</span><span style="color: darkgrey;">.</span><span style="color: black;">Editor</span>
<span style="color: orangered;">$caretLine</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$editor</span><span style="color: darkgrey;">.</span><span style="color: black;">CaretLine</span>
<span style="color: orangered;">$lineCount</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$editor</span><span style="color: darkgrey;">.</span><span style="color: black;">LineCount</span>
<span style="color: darkblue;">if</span> <span style="color: black;">(</span><span style="color: darkgrey;">-not</span> <span style="color: orangered;">$selectedEditor</span><span style="color: darkgrey;">.</span><span style="color: black;">SelectedText</span><span style="color: black;">)</span>
<span style="color: black;">{</span>
<span style="color: orangered;">$lineend</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$editor</span><span style="color: darkgrey;">.</span><span style="color: black;">GetLineLength</span><span style="color: black;">(</span><span style="color: orangered;">$lineCount</span><span style="color: black;">)</span> <span style="color: darkgrey;">+</span> <span style="color: purple;">1</span>
<span style="color: darkgreen;"># Write-Host "Nothing Selected Linecount: $lineCount LineEnd: $lineend"</span>
<span style="color: darkblue;">if</span> <span style="color: black;">(</span><span style="color: orangered;">$lineend</span> <span style="color: darkgrey;">-eq</span> <span style="color: purple;">0</span><span style="color: black;">)</span> <span style="color: black;">{</span><span style="color: orangered;">$lineend</span> <span style="color: darkgrey;">=</span> <span style="color: purple;">1</span><span style="color: black;">}</span>
<span style="color: darkgreen;"># set selection to the end of the editor</span>
<span style="color: orangered;">$editor</span><span style="color: darkgrey;">.</span><span style="color: black;">Select</span><span style="color: black;">(</span><span style="color: orangered;">$lineCount</span><span style="color: darkgrey;">,</span> <span style="color: orangered;">$lineend</span><span style="color: darkgrey;">,</span> <span style="color: orangered;">$lineCount</span><span style="color: darkgrey;">,</span> <span style="color: orangered;">$lineend</span><span style="color: black;">)</span>
<span style="color: black;">}</span>
<span style="color: darkblue;">elseif</span> <span style="color: black;">(</span><span style="color: orangered;">$caretLine</span> <span style="color: darkgrey;">-lt</span> <span style="color: orangered;">$lineCount</span><span style="color: black;">)</span>
<span style="color: black;">{</span>
<span style="color: darkgreen;"># Write-Host "Usual case"</span>
<span style="color: darkgreen;"># set selection to the start of the next line</span>
<span style="color: orangered;">$editor</span><span style="color: darkgrey;">.</span><span style="color: black;">Select</span><span style="color: black;">(</span><span style="color: orangered;">$caretLine</span> <span style="color: darkgrey;">+</span> <span style="color: purple;">1</span><span style="color: darkgrey;">,</span> <span style="color: purple;">1</span><span style="color: darkgrey;">,</span> <span style="color: orangered;">$caretLine</span> <span style="color: darkgrey;">+</span> <span style="color: purple;">1</span><span style="color: darkgrey;">,</span> <span style="color: purple;">1</span><span style="color: black;">)</span>
<span style="color: black;">}</span>
<span style="color: darkblue;">else</span>
<span style="color: black;">{</span>
<span style="color: orangered;">$lineend</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$editor</span><span style="color: darkgrey;">.</span><span style="color: black;">GetLineLength</span><span style="color: black;">(</span><span style="color: orangered;">$lineCount</span><span style="color: black;">)</span> <span style="color: darkgrey;">+</span> <span style="color: purple;">1</span>
<span style="color: darkgreen;"># Write-Host "At end Linecount: $lineCount LineEnd: $lineend"</span>
<span style="color: darkblue;">if</span> <span style="color: black;">(</span><span style="color: orangered;">$lineend</span> <span style="color: darkgrey;">-eq</span> <span style="color: purple;">0</span><span style="color: black;">)</span> <span style="color: black;">{</span><span style="color: orangered;">$lineend</span> <span style="color: darkgrey;">=</span> <span style="color: purple;">1</span><span style="color: black;">}</span>
<span style="color: darkgreen;"># set selection to the end of the editor</span>
<span style="color: orangered;">$editor</span><span style="color: darkgrey;">.</span><span style="color: black;">Select</span><span style="color: black;">(</span><span style="color: orangered;">$lineCount</span><span style="color: darkgrey;">,</span> <span style="color: orangered;">$lineend</span><span style="color: darkgrey;">,</span> <span style="color: orangered;">$lineCount</span><span style="color: darkgrey;">,</span> <span style="color: orangered;">$lineend</span><span style="color: black;">)</span>
<span style="color: black;">}</span>
<span style="color: orangered;">$editor</span><span style="color: darkgrey;">.</span><span style="color: black;">InsertText</span><span style="color: black;">(</span><span style="color: orangered;">$text</span><span style="color: black;">)</span> </pre><br />
<br />
<br />
You can load this into an ISE editor and run it using F5. You see the line your code goes here added at the end.<br />
<br />
BerndBernd Krisziohttp://www.blogger.com/profile/03294997372899922987noreply@blogger.com0tag:blogger.com,1999:blog-8903006743421146366.post-20885052804592356212010-05-28T00:09:00.000-07:002010-05-28T00:09:05.758-07:00A Problem with Remove-Item $folder -recurse -forceI'm working with some testsenario, where I have repeatedly to remove a folder structure of about 3500 files of depth 8.<br />
<br />
Remove-Item $folder -recurse -force<br />
<br />
sometimes does not remove all the files. In some cases it yields the message, that a folder can't be deleted, because it is not empty, but when checking the folder is empty. In other not all files are removed.<br />
<br />
To reach a clean state, I wrote the following script:<br />
<br />
<br />
<pre class="PowerShellColorizedScript"><span style="color: darkblue;">function</span> <span style="color: blueviolet;">Remove-ItemRecursiveBruteForce</span>
<span style="color: black;">{</span>
<span style="color: darkblue;">param</span><span style="color: black;">(</span>
<span style="color: darkgrey;">[</span><span style="color: lightblue;">Parameter</span><span style="color: black;">(</span><span style="color: black;">Position</span> <span style="color: darkgrey;">=</span> <span style="color: purple;">0</span><span style="color: darkgrey;">,</span> <span style="color: black;">Mandatory</span><span style="color: darkgrey;">=</span><span style="color: orangered;">$true</span><span style="color: darkgrey;">,</span> <span style="color: black;">ValueFromPipeline</span><span style="color: darkgrey;">=</span><span style="color: orangered;">$false</span><span style="color: black;">)</span><span style="color: darkgrey;">]</span>
<span style="color: teal;">[string]</span><span style="color: orangered;">$folder</span><span style="color: darkgrey;">,</span>
<span style="color: darkgrey;">[</span><span style="color: lightblue;">Parameter</span><span style="color: black;">(</span><span style="color: black;">Position</span> <span style="color: darkgrey;">=</span> <span style="color: purple;">1</span><span style="color: darkgrey;">,</span> <span style="color: black;">Mandatory</span><span style="color: darkgrey;">=</span><span style="color: orangered;">$False</span><span style="color: darkgrey;">,</span> <span style="color: black;">ValueFromPipeline</span><span style="color: darkgrey;">=</span><span style="color: orangered;">$false</span><span style="color: black;">)</span><span style="color: darkgrey;">]</span>
<span style="color: orangered;">$maxiterations</span> <span style="color: darkgrey;">=</span> <span style="color: purple;">20</span>
<span style="color: black;">)</span>
<span style="color: orangered;">$iteration</span> <span style="color: darkgrey;">=</span> <span style="color: purple;">0</span>
<span style="color: darkblue;">while</span> <span style="color: black;">(</span> <span style="color: orangered;">$iteration</span><span style="color: darkgrey;">++</span> <span style="color: darkgrey;">-lt</span> <span style="color: orangered;">$maxiterations</span><span style="color: black;">)</span>
<span style="color: black;">{</span>
<span style="color: darkblue;">if</span> <span style="color: black;">(</span><span style="color: blue;">Test-Path</span> <span style="color: orangered;">$folder</span><span style="color: black;">)</span>
<span style="color: black;">{</span>
<span style="color: blue;">Remove-Item</span> <span style="color: orangered;">$folder</span> <span style="color: navy;">-recurse</span> <span style="color: navy;">-force</span> <span style="color: navy;">-EA</span> <span style="color: blueviolet;">SilentlyContinue</span>
<span style="color: black;">}</span>
<span style="color: darkblue;">else</span>
<span style="color: black;">{</span>
<span style="color: darkred;">"$folder deleted in $iteration iterations"</span>
<span style="color: darkblue;">break</span>
<span style="color: black;">}</span>
<span style="color: black;">}</span>
<span style="color: darkblue;">if</span> <span style="color: black;">(</span><span style="color: blue;">Test-Path</span> <span style="color: orangered;">$folder</span><span style="color: black;">)</span>
<span style="color: black;">{</span>
<span style="color: darkred;">"$folder not empty after $iteration iterations"</span>
<span style="color: black;">}</span>
<span style="color: black;">}</span>
</pre><br />
<br />
I observe that it needs randomly between 2 and 11 iterations to remove my folder.<br />
<br />
I'm using W7-32 bit and use PowerShell-ISE to run the script.<br />
<br />
Is this problem just me, or is it OS specific. Any explanations?Bernd Krisziohttp://www.blogger.com/profile/03294997372899922987noreply@blogger.com4tag:blogger.com,1999:blog-8903006743421146366.post-66591110394496018652010-05-19T01:12:00.000-07:002010-05-19T01:12:35.553-07:001. Attemp to use .NET isolated storage with modeless WPK windows <br />
<br />
The following attempt is not working with modeless windows (replace -show #-asjob by -asjob). Using -show it works. Please run the code, do some selection within GUI, finally call Get-value to check the result.<br />
<br />
<br />
<pre class="PowerShellColorizedScript"><span style="color: blue;">ipmo</span> <span style="color: blueviolet;">WPK</span>
<span style="color: orangered;">$env:WPKResult</span> <span style="color: darkgrey;">=</span> <span style="color: darkred;">"auto"</span>
<span style="color: darkblue;">function</span> <span style="color: blueviolet;">action</span>
<span style="color: black;">{</span>
<span style="color: darkblue;">param</span> <span style="color: black;">(</span>
<span style="color: orangered;">$value</span>
<span style="color: black;">)</span>
<span style="color: orangered;">$env:WPKResult</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$value</span>
<span style="color: orangered;">$changedProperty</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$value</span>
<span style="color: blue;">Write-Host</span> <span style="color: darkred;">"Value of $changedProperty changed to $value"</span>
<span style="color: darkblue;">try</span> <span style="color: black;">{</span>
<span style="color: orangered;">$useriStorage</span> <span style="color: darkgrey;">=</span> <span style="color: teal;">[System.IO.IsolatedStorage.IsolatedStorageFile]</span><span style="color: darkgrey;">::</span><span style="color: black;">GetUserStoreForAssembly</span><span style="color: black;">(</span><span style="color: black;">)</span>
<span style="color: orangered;">$file</span> <span style="color: darkgrey;">=</span> <span style="color: blue;">New-Object</span> <span style="color: blueviolet;">System.IO.IsolatedStorage.IsolatedStorageFileStream</span><span style="color: black;">(</span><span style="color: darkred;">"SQLSettigs.xml"</span><span style="color: darkgrey;">,</span><span style="color: teal;">[System.IO.FileMode]</span><span style="color: darkgrey;">::</span><span style="color: black;">Create</span><span style="color: darkgrey;">,</span><span style="color: orangered;">$useriStorage</span><span style="color: black;">)</span>
<span style="color: orangered;">$xml</span> <span style="color: darkgrey;">=</span> <span style="color: blue;">New-Object</span> <span style="color: blueviolet;">System.Xml.Serialization.XmlSerializer</span><span style="color: black;">(</span><span style="color: orangered;">$changedProperty</span><span style="color: darkgrey;">.</span><span style="color: black;">GetType</span><span style="color: black;">(</span><span style="color: black;">)</span><span style="color: black;">)</span>
<span style="color: orangered;">$xml</span><span style="color: darkgrey;">.</span><span style="color: black;">Serialize</span><span style="color: black;">(</span><span style="color: orangered;">$file</span><span style="color: darkgrey;">,</span><span style="color: orangered;">$changedProperty</span><span style="color: black;">)</span>
<span style="color: orangered;">$file</span><span style="color: darkgrey;">.</span><span style="color: black;">Close</span><span style="color: black;">(</span><span style="color: black;">)</span>
<span style="color: orangered;">$useriStorage</span><span style="color: darkgrey;">.</span><span style="color: black;">Close</span><span style="color: black;">(</span><span style="color: black;">)</span>
<span style="color: black;">}</span>
<span style="color: darkblue;">catch</span> <span style="color: black;">{</span>
<span style="color: orangered;">$env:WPKResult</span> <span style="color: darkgrey;">=</span> <span style="color: darkred;">'error'</span>
<span style="color: black;">}</span>
<span style="color: black;">}</span>
<span style="color: blue;">New-StackPanel</span> <span style="color: black;">{</span>
<span style="color: blue;">New-RadioButton</span> <span style="color: navy;">-Content</span> <span style="color: darkred;">"auto"</span> <span style="color: navy;">-GroupName</span> <span style="color: blueviolet;">Results</span> <span style="color: navy;">-IsChecked</span> <span style="color: orangered;">$True</span> <span style="color: navy;">-On_Click</span> <span style="color: black;">{</span> <span style="color: blue;">action</span> <span style="color: darkred;">"auto"</span> <span style="color: black;">}</span>
<span style="color: blue;">New-RadioButton</span> <span style="color: navy;">-Content</span> <span style="color: darkred;">"list"</span> <span style="color: navy;">-GroupName</span> <span style="color: blueviolet;">Results</span> <span style="color: navy;">-On_Click</span> <span style="color: black;">{</span> <span style="color: blue;">action</span> <span style="color: darkred;">"list"</span> <span style="color: black;">}</span>
<span style="color: blue;">New-RadioButton</span> <span style="color: navy;">-Content</span> <span style="color: darkred;">"table"</span> <span style="color: navy;">-GroupName</span> <span style="color: blueviolet;">Results</span> <span style="color: navy;">-On_Click</span> <span style="color: black;">{</span> <span style="color: blue;">action</span> <span style="color: darkred;">"table"</span> <span style="color: black;">}</span>
<span style="color: blue;">New-RadioButton</span> <span style="color: navy;">-Content</span> <span style="color: darkred;">"grid"</span> <span style="color: navy;">-GroupName</span> <span style="color: blueviolet;">Results</span> <span style="color: navy;">-On_Click</span> <span style="color: black;">{</span> <span style="color: blue;">action</span> <span style="color: darkred;">"grid"</span> <span style="color: black;">}</span>
<span style="color: blue;">New-RadioButton</span> <span style="color: navy;">-Content</span> <span style="color: darkred;">"variable"</span> <span style="color: navy;">-GroupName</span> <span style="color: blueviolet;">Results</span> <span style="color: navy;">-On_Click</span> <span style="color: black;">{</span> <span style="color: blue;">action</span> <span style="color: darkred;">"variable"</span> <span style="color: black;">}</span>
<span style="color: blue;">New-RadioButton</span> <span style="color: navy;">-Content</span> <span style="color: darkred;">"csv"</span> <span style="color: navy;">-GroupName</span> <span style="color: blueviolet;">Results</span> <span style="color: navy;">-On_Click</span> <span style="color: black;">{</span> <span style="color: blue;">action</span> <span style="color: darkred;">"csv"</span> <span style="color: black;">}</span>
<span style="color: blue;">New-RadioButton</span> <span style="color: navy;">-Content</span> <span style="color: darkred;">"file"</span> <span style="color: navy;">-GroupName</span> <span style="color: blueviolet;">Results</span> <span style="color: navy;">-On_Click</span> <span style="color: black;">{</span> <span style="color: blue;">action</span> <span style="color: darkred;">"file"</span> <span style="color: black;">}</span>
<span style="color: black;">}</span> <span style="color: navy;">-show</span> <span style="color: darkgreen;">#-asjob </span>
<span style="color: darkblue;">function</span> <span style="color: blueviolet;">Get-value</span>
<span style="color: black;">{</span>
<span style="color: orangered;">$useriStorage</span> <span style="color: darkgrey;">=</span> <span style="color: teal;">[System.IO.IsolatedStorage.IsolatedStorageFile]</span><span style="color: darkgrey;">::</span><span style="color: black;">GetUserStoreForAssembly</span><span style="color: black;">(</span><span style="color: black;">)</span>
<span style="color: darkblue;">try</span> <span style="color: black;">{</span>
<span style="color: orangered;">$file</span> <span style="color: darkgrey;">=</span> <span style="color: blue;">New-Object</span> <span style="color: blueviolet;">System.IO.IsolatedStorage.IsolatedStorageFileStream</span><span style="color: black;">(</span><span style="color: darkred;">"SQLSettigs.xml"</span><span style="color: darkgrey;">,</span><span style="color: teal;">[System.IO.FileMode]</span><span style="color: darkgrey;">::</span><span style="color: black;">Open</span><span style="color: darkgrey;">,</span><span style="color: orangered;">$useriStorage</span><span style="color: black;">)</span>
<span style="color: black;">}</span>
<span style="color: darkblue;">catch</span> <span style="color: black;">{</span>
<span style="color: blue;">Write-Host</span> <span style="color: darkred;">"UISettings.xml is not found"</span>
<span style="color: darkblue;">return</span>
<span style="color: black;">}</span>
<span style="color: orangered;">$xmlReader</span> <span style="color: darkgrey;">=</span> <span style="color: blue;">New-Object</span> <span style="color: blueviolet;">System.Xml.XmlTextReader</span><span style="color: black;">(</span><span style="color: orangered;">$file</span><span style="color: black;">)</span>
<span style="color: orangered;">$xml</span> <span style="color: darkgrey;">=</span> <span style="color: blue;">New-Object</span> <span style="color: blueviolet;">System.Xml.Serialization.XmlSerializer</span><span style="color: black;">(</span><span style="color: darkred;">'abc'</span><span style="color: darkgrey;">.</span><span style="color: black;">GetType</span><span style="color: black;">(</span><span style="color: black;">)</span><span style="color: black;">)</span>
<span style="color: orangered;">$newISEOptions</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$xml</span><span style="color: darkgrey;">.</span><span style="color: black;">Deserialize</span><span style="color: black;">(</span><span style="color: orangered;">$xmlReader</span><span style="color: black;">)</span>
<span style="color: orangered;">$newISEOptions</span>
<span style="color: black;">}</span></pre>Bernd Krisziohttp://www.blogger.com/profile/03294997372899922987noreply@blogger.com0tag:blogger.com,1999:blog-8903006743421146366.post-53993645153626177652010-05-16T08:03:00.000-07:002010-05-16T08:03:28.294-07:00Please help - I want to send back a keystroke to the ISE windowHello WPF friends, I need a little help. I want to add a button which sends F7 keystroke to the ISE-window from which I run this little WPK script.<br />
<br />
<br />
<pre class="PowerShellColorizedScript"><span style="color: blue;">ipmo</span> <span style="color: blueviolet;">WPK</span>
<span style="color: orangered;">$env:WPKResult</span> <span style="color: darkgrey;">=</span> <span style="color: darkred;">"auto"</span>
<span style="color: blue;">New-StackPanel</span> <span style="color: black;">{</span>
<span style="color: blue;">New-RadioButton</span> <span style="color: navy;">-Content</span> <span style="color: darkred;">"auto"</span> <span style="color: navy;">-GroupName</span> <span style="color: blueviolet;">Results</span> <span style="color: navy;">-IsChecked</span> <span style="color: orangered;">$True</span> <span style="color: navy;">-On_Click</span> <span style="color: black;">{</span> <span style="color: orangered;">$env:WPKResult</span> <span style="color: darkgrey;">=</span> <span style="color: darkred;">"auto"</span> <span style="color: black;">}</span>
<span style="color: blue;">New-RadioButton</span> <span style="color: navy;">-Content</span> <span style="color: darkred;">"list"</span> <span style="color: navy;">-GroupName</span> <span style="color: blueviolet;">Results</span> <span style="color: navy;">-On_Click</span> <span style="color: black;">{</span> <span style="color: orangered;">$env:WPKResult</span> <span style="color: darkgrey;">=</span> <span style="color: darkred;">"list"</span> <span style="color: black;">}</span>
<span style="color: blue;">New-RadioButton</span> <span style="color: navy;">-Content</span> <span style="color: darkred;">"table"</span> <span style="color: navy;">-GroupName</span> <span style="color: blueviolet;">Results</span> <span style="color: navy;">-On_Click</span> <span style="color: black;">{</span> <span style="color: orangered;">$env:WPKResult</span> <span style="color: darkgrey;">=</span> <span style="color: darkred;">"table"</span> <span style="color: black;">}</span>
<span style="color: blue;">New-RadioButton</span> <span style="color: navy;">-Content</span> <span style="color: darkred;">"grid"</span> <span style="color: navy;">-GroupName</span> <span style="color: blueviolet;">Results</span> <span style="color: navy;">-On_Click</span> <span style="color: black;">{</span> <span style="color: orangered;">$env:WPKResult</span> <span style="color: darkgrey;">=</span> <span style="color: darkred;">"grid"</span> <span style="color: black;">}</span>
<span style="color: blue;">New-RadioButton</span> <span style="color: navy;">-Content</span> <span style="color: darkred;">"variable"</span> <span style="color: navy;">-GroupName</span> <span style="color: blueviolet;">Results</span> <span style="color: navy;">-On_Click</span> <span style="color: black;">{</span> <span style="color: orangered;">$env:WPKResult</span> <span style="color: darkgrey;">=</span> <span style="color: darkred;">"variable"</span> <span style="color: black;">}</span>
<span style="color: blue;">New-RadioButton</span> <span style="color: navy;">-Content</span> <span style="color: darkred;">"csv"</span> <span style="color: navy;">-GroupName</span> <span style="color: blueviolet;">Results</span> <span style="color: navy;">-On_Click</span> <span style="color: black;">{</span> <span style="color: orangered;">$env:WPKResult</span> <span style="color: darkgrey;">=</span> <span style="color: darkred;">"csv"</span> <span style="color: black;">}</span>
<span style="color: blue;">New-RadioButton</span> <span style="color: navy;">-Content</span> <span style="color: darkred;">"file"</span> <span style="color: navy;">-GroupName</span> <span style="color: blueviolet;">Results</span> <span style="color: navy;">-On_Click</span> <span style="color: black;">{</span> <span style="color: orangered;">$env:WPKResult</span> <span style="color: darkgrey;">=</span> <span style="color: darkred;">"file"</span> <span style="color: black;">}</span>
<span style="color: blue;">New-Button</span> <span style="color: navy;">-Content</span> <span style="color: darkred;">"execute Sql"</span> <span style="color: navy;">-On_click</span> <span style="color: black;">{</span> <span style="color: blue;">New-Button</span> <span style="color: darkred;">"I want to send F7 key-stroke to calling ISE"</span> <span style="color: navy;">-show</span> <span style="color: black;">}</span>
<span style="color: black;">}</span> <span style="color: navy;">-asjob</span>
</pre><br />
This script is intended to be run in ISE and to add some cool GUI to SQLPSX.<br />
<br />
Thanks<br />
<br />
BerndBernd Krisziohttp://www.blogger.com/profile/03294997372899922987noreply@blogger.com0tag:blogger.com,1999:blog-8903006743421146366.post-61928399289099108482010-05-12T11:54:00.000-07:002010-05-12T11:54:32.411-07:00A try in WPK (one way and only once)The best I found in WPK seems to be the the following:<br />
<br />
<pre class="PowerShellColorizedScript"><span style="color: blue;">ipmo</span> <span style="color: blueviolet;">WPK</span>
<span style="color: orangered;">$option</span> <span style="color: darkgrey;">=</span> <span style="color: darkred;">"To File"</span> <span style="color: darkgreen;"># this value might be prompted by the user</span>
<span style="color: blue;">New-StackPanel</span> <span style="color: black;">{</span>
<span style="color: blue;">New-RadioButton</span> <span style="color: navy;">-Content</span> <span style="color: darkred;">"To Text"</span> <span style="color: navy;">-GroupName</span> <span style="color: blueviolet;">Results</span> <span style="color: navy;">-IsChecked</span> <span style="color: orangered;">$True</span> <span style="color: navy;">-On_Click</span> <span style="color: black;">{</span>
<span style="color: orangered;">$Resource</span><span style="color: darkgrey;">.</span><span style="color: black;">Result</span> <span style="color: darkgrey;">=</span> <span style="color: darkred;">"To Text"</span>
<span style="color: orangered;">$info</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$window</span> <span style="color: darkgrey;">|</span> <span style="color: blue;">Get-ChildControl</span> <span style="color: blueviolet;">info</span>
<span style="color: orangered;">$info</span><span style="color: darkgrey;">.</span><span style="color: black;">text</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$Resource</span><span style="color: darkgrey;">.</span><span style="color: black;">Result</span>
<span style="color: orangered;">$env:WPKResult</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$Resource</span><span style="color: darkgrey;">.</span><span style="color: black;">Result</span>
<span style="color: black;">}</span>
<span style="color: blue;">New-RadioButton</span> <span style="color: navy;">-Content</span> <span style="color: darkred;">"To Grid"</span> <span style="color: navy;">-GroupName</span> <span style="color: blueviolet;">Results</span> <span style="color: navy;">-On_Click</span> <span style="color: black;">{</span>
<span style="color: orangered;">$Resource</span><span style="color: darkgrey;">.</span><span style="color: black;">Result</span> <span style="color: darkgrey;">=</span> <span style="color: darkred;">"To Grid"</span>
<span style="color: orangered;">$info</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$window</span> <span style="color: darkgrey;">|</span> <span style="color: blue;">Get-ChildControl</span> <span style="color: blueviolet;">info</span>
<span style="color: orangered;">$info</span><span style="color: darkgrey;">.</span><span style="color: black;">text</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$Resource</span><span style="color: darkgrey;">.</span><span style="color: black;">Result</span>
<span style="color: orangered;">$env:WPKResult</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$Resource</span><span style="color: darkgrey;">.</span><span style="color: black;">Result</span>
<span style="color: black;">}</span>
<span style="color: blue;">New-RadioButton</span> <span style="color: navy;">-Content</span> <span style="color: darkred;">"To File"</span> <span style="color: navy;">-GroupName</span> <span style="color: blueviolet;">Results</span> <span style="color: navy;">-On_Click</span> <span style="color: black;">{</span>
<span style="color: orangered;">$Resource</span><span style="color: darkgrey;">.</span><span style="color: black;">Result</span> <span style="color: darkgrey;">=</span> <span style="color: darkred;">"To File"</span>
<span style="color: orangered;">$info</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$window</span> <span style="color: darkgrey;">|</span> <span style="color: blue;">Get-ChildControl</span> <span style="color: blueviolet;">info</span>
<span style="color: orangered;">$info</span><span style="color: darkgrey;">.</span><span style="color: black;">text</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$Resource</span><span style="color: darkgrey;">.</span><span style="color: black;">Result</span>
<span style="color: orangered;">$env:WPKResult</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$Resource</span><span style="color: darkgrey;">.</span><span style="color: black;">Result</span>
<span style="color: black;">}</span>
<span style="color: blue;">New-RadioButton</span> <span style="color: navy;">-Content</span> <span style="color: darkred;">"To CSV"</span> <span style="color: navy;">-GroupName</span> <span style="color: blueviolet;">Results</span> <span style="color: navy;">-On_Click</span> <span style="color: black;">{</span>
<span style="color: orangered;">$Resource</span><span style="color: darkgrey;">.</span><span style="color: black;">Result</span> <span style="color: darkgrey;">=</span> <span style="color: darkred;">"To CSV"</span>
<span style="color: orangered;">$info</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$window</span> <span style="color: darkgrey;">|</span> <span style="color: blue;">Get-ChildControl</span> <span style="color: blueviolet;">info</span>
<span style="color: orangered;">$info</span><span style="color: darkgrey;">.</span><span style="color: black;">text</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$Resource</span><span style="color: darkgrey;">.</span><span style="color: black;">Result</span>
<span style="color: orangered;">$env:WPKResult</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$Resource</span><span style="color: darkgrey;">.</span><span style="color: black;">Result</span>
<span style="color: black;">}</span>
<span style="color: blue;">New-textBox</span> <span style="color: navy;">-Name</span> <span style="color: blueviolet;">info</span>
<span style="color: black;">}</span> <span style="color: navy;">-On_Loaded</span> <span style="color: black;">{</span>
<span style="color: orangered;">$info</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$window</span> <span style="color: darkgrey;">|</span> <span style="color: blue;">Get-ChildControl</span> <span style="color: blueviolet;">info</span>
<span style="color: orangered;">$info</span><span style="color: darkgrey;">.</span><span style="color: black;">text</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$Resource</span><span style="color: darkgrey;">.</span><span style="color: black;">Result</span>
<span style="color: black;">}</span><span style="color: navy;">-asjob</span> <span style="color: navy;">-resource</span> <span style="color: black;">@{</span><span style="color: black;">Result</span> <span style="color: darkgrey;">=</span> <span style="color: orangered;">$option</span><span style="color: black;">}</span>
</pre><br />
I can modify the value within GUI and query it outside<br />
<br />
<pre class="PowerShellColorizedScript"><span style="color: orangered;">$env:WPKResult</span></pre><pre class="PowerShellColorizedScript"><span style="color: orangered;"> </span></pre>I misuse the processes environment, which is shared between the runspaces. Hmm better than pidgeon carrying USB sticks, but it is restricted to strings.<br />
<br />
I guess I have to really dive into PowerBoots (and that is the polite version ;-) of my opinion about WPK)<br />
<br />
Have some fun trying to find further work arounds.<br />
<br />
Bernd<br />
<br />
<pre class="PowerShellColorizedScript"><span style="color: orangered;"> </span></pre><pre class="PowerShellColorizedScript"><span style="color: orangered;"> </span></pre><pre class="PowerShellColorizedScript"><span style="color: orangered;"> </span></pre>Bernd Krisziohttp://www.blogger.com/profile/03294997372899922987noreply@blogger.com0tag:blogger.com,1999:blog-8903006743421146366.post-79512756556605087042010-05-12T10:04:00.000-07:002010-05-12T10:04:04.902-07:00A small Async PowerBootsWindowHello,<br />
here I'll show a little async PowerBoots Gui, which is able to modify variables in the main thread. <br />
<br />
<pre class='PowerShellColorizedScript'><span style='color:#0000ff'>ipmo</span> <span style='color:#8a2be2'>PowerBoots</span>
<span style='color:#ff4500'>$options</span> <span style='color:#a9a9a9'>=</span> <span style='color:#000000'>@{</span> <span style='color:#8b0000'>"Results"</span> <span style='color:#a9a9a9'>=</span> <span style='color:#8b0000'>"To Text"</span> <span style='color:#000000'>}</span>
<span style='color:#0000ff'>Boots</span> <span style='color:#000000'>{</span>
<span style='color:#0000ff'>StackPanel</span> <span style='color:#000000'>{</span>
<span style='color:#0000ff'>RadioButton</span> <span style='color:#000080'>-Content</span> <span style='color:#8b0000'>"To Text"</span> <span style='color:#000080'>-GroupName</span> <span style='color:#8a2be2'>Results</span> <span style='color:#000080'>-IsChecked</span> <span style='color:#ff4500'>$True</span> <span style='color:#000080'>-On_Click</span> <span style='color:#000000'>{</span> <span style='color:#ff4500'>$options</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>Results</span> <span style='color:#a9a9a9'>=</span> <span style='color:#8b0000'>"To Text"</span> <span style='color:#000000'>}</span>
<span style='color:#0000ff'>RadioButton</span> <span style='color:#000080'>-Content</span> <span style='color:#8b0000'>"To Grid"</span> <span style='color:#000080'>-GroupName</span> <span style='color:#8a2be2'>Results</span> <span style='color:#000080'>-On_Click</span> <span style='color:#000000'>{</span> <span style='color:#ff4500'>$options</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>Results</span> <span style='color:#a9a9a9'>=</span> <span style='color:#8b0000'>"To Grid"</span> <span style='color:#000000'>}</span>
<span style='color:#0000ff'>RadioButton</span> <span style='color:#000080'>-Content</span> <span style='color:#8b0000'>"To File"</span> <span style='color:#000080'>-GroupName</span> <span style='color:#8a2be2'>Results</span> <span style='color:#000080'>-On_Click</span> <span style='color:#000000'>{</span> <span style='color:#ff4500'>$options</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>Results</span> <span style='color:#a9a9a9'>=</span> <span style='color:#8b0000'>"To File"</span> <span style='color:#000000'>}</span>
<span style='color:#0000ff'>RadioButton</span> <span style='color:#000080'>-Content</span> <span style='color:#8b0000'>"To CSV"</span> <span style='color:#000080'>-GroupName</span> <span style='color:#8a2be2'>Results</span> <span style='color:#000080'>-On_Click</span> <span style='color:#000000'>{</span> <span style='color:#ff4500'>$options</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>Results</span> <span style='color:#a9a9a9'>=</span> <span style='color:#8b0000'>"To CSV"</span> <span style='color:#000000'>}</span>
<span style='color:#000000'>}</span>
<span style='color:#000000'>}</span> <span style='color:#000080'>-async</span>
</pre><br />
Is it possible to do the same thing with WPK?Bernd Krisziohttp://www.blogger.com/profile/03294997372899922987noreply@blogger.com0tag:blogger.com,1999:blog-8903006743421146366.post-70381173557034491732010-05-12T00:15:00.000-07:002010-05-12T00:15:24.637-07:00A viewer for Crystal Reports with PowerShell (partially working)Hello,<br />
this time I tried to build a small Report Viewer using the components from Visual Studio 2008 with PowerShell. It shows that using PowerShell here makes some things rather easy.<br />
<br />
I'm using this to display old Reports made with Crystal Reports 8.5 using ODBC Datasources to SQL-Server.<br />
<br />
Reports based on views works fine, I can set database and formulars.<br />
<br />
But I have still a problem with reports based on stored procedures. Setting $table.location doesn't work anymore as in the past. I go the error table not found. Any hints welcome. <br />
<br />
And here is the code if you want to try something like this:<br />
<br />
<pre class='PowerShellColorizedScript'><span style='color:#00008b'>Function</span> <span style='color:#8a2be2'>Show-CrystalReport</span>
<span style='color:#000000'>{</span>
<span style='color:#00008b'>param</span><span style='color:#000000'>(</span>
<span style='color:#ff4500'>$reportPath</span><span style='color:#a9a9a9'>,</span>
<span style='color:#ff4500'>$servername</span><span style='color:#a9a9a9'>,</span>
<span style='color:#ff4500'>$databasename</span><span style='color:#a9a9a9'>,</span>
<span style='color:#ff4500'>$userId</span><span style='color:#a9a9a9'>,</span>
<span style='color:#ff4500'>$password</span><span style='color:#a9a9a9'>,</span>
<span style='color:#ff4500'>$RecordSelectionFormula</span><span style='color:#a9a9a9'>,</span>
<span style='color:#ff4500'>$formulas</span><span style='color:#a9a9a9'>,</span>
<span style='color:#ff4500'>$parameters</span>
<span style='color:#000000'>)</span>
<span style='color:#008080'>[reflection.assembly]</span><span style='color:#a9a9a9'>::</span><span style='color:#000000'>LoadWithPartialName</span><span style='color:#000000'>(</span><span style='color:#8b0000'>'CrystalDecisions.Shared'</span><span style='color:#000000'>)</span>
<span style='color:#008080'>[reflection.assembly]</span><span style='color:#a9a9a9'>::</span><span style='color:#000000'>LoadWithPartialName</span><span style='color:#000000'>(</span><span style='color:#8b0000'>'CrystalDecisions.CrystalReports.Engine'</span><span style='color:#000000'>)</span>
<span style='color:#008080'>[reflection.assembly]</span><span style='color:#a9a9a9'>::</span><span style='color:#000000'>LoadWithPartialName</span><span style='color:#000000'>(</span><span style='color:#8b0000'>'CrystalDecisions.Windows.Forms'</span><span style='color:#000000'>)</span>
<span style='color:#ff4500'>$report</span> <span style='color:#a9a9a9'>=</span> <span style='color:#0000ff'>New-Object</span> <span style='color:#8a2be2'>CrystalDecisions.CrystalReports.Engine.ReportDocument</span>
<span style='color:#ff4500'>$report</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>load</span><span style='color:#000000'>(</span><span style='color:#ff4500'>$reportPath</span><span style='color:#000000'>)</span>
<span style='color:#ff4500'>$report</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>ParameterFields</span> <span style='color:#a9a9a9'>|</span> <span style='color:#0000ff'>%</span> <span style='color:#000000'>{</span>
<span style='color:#00008b'>if</span> <span style='color:#000000'>(</span><span style='color:#ff4500'>$parameters</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>keys</span> <span style='color:#a9a9a9'>-contains</span> <span style='color:#ff4500'>$_</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>Name</span><span style='color:#000000'>)</span>
<span style='color:#000000'>{</span>
<span style='color:#ff4500'>$report</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>SetParameterValue</span><span style='color:#000000'>(</span><span style='color:#ff4500'>$_</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>Name</span><span style='color:#a9a9a9'>,</span> <span style='color:#ff4500'>$parameters</span><span style='color:#a9a9a9'>[</span><span style='color:#ff4500'>$_</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>Name</span><span style='color:#a9a9a9'>]</span><span style='color:#000000'>)</span>
<span style='color:#000000'>}</span>
<span style='color:#000000'>}</span>
<span style='color:#ff4500'>$report</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>DataDefinition</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>FormulaFields</span> <span style='color:#a9a9a9'>|</span> <span style='color:#0000ff'>%</span> <span style='color:#000000'>{</span>
<span style='color:#00008b'>if</span> <span style='color:#000000'>(</span><span style='color:#ff4500'>$formulas</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>keys</span> <span style='color:#a9a9a9'>-contains</span> <span style='color:#ff4500'>$_</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>Name</span><span style='color:#000000'>)</span>
<span style='color:#000000'>{</span>
<span style='color:#ff4500'>$_</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>Text</span> <span style='color:#a9a9a9'>=</span> <span style='color:#ff4500'>$formulas</span><span style='color:#a9a9a9'>[</span><span style='color:#ff4500'>$_</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>Name</span><span style='color:#a9a9a9'>]</span>
<span style='color:#000000'>}</span>
<span style='color:#000000'>}</span>
<span style='color:#00008b'>if</span> <span style='color:#000000'>(</span><span style='color:#ff4500'>$RecordSelectionFormula</span><span style='color:#000000'>)</span> <span style='color:#000000'>{</span>
<span style='color:#ff4500'>$report</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>RecordSelectionFormula</span> <span style='color:#a9a9a9'>=</span> <span style='color:#ff4500'>$RecordSelectionFormula</span>
<span style='color:#000000'>}</span>
<span style='color:#00008b'>foreach</span> <span style='color:#000000'>(</span><span style='color:#ff4500'>$Table</span> <span style='color:#00008b'>in</span> <span style='color:#ff4500'>$report</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>Database</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>Tables</span><span style='color:#000000'>)</span>
<span style='color:#000000'>{</span>
<span style='color:#ff4500'>$table</span>
<span style='color:#ff4500'>$tli</span> <span style='color:#a9a9a9'>=</span> <span style='color:#ff4500'>$Table</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>LogonInfo</span>
<span style='color:#ff4500'>$li</span> <span style='color:#a9a9a9'>=</span> <span style='color:#ff4500'>$tli</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>ConnectionInfo</span>
<span style='color:#0000ff'>Write-host</span> <span style='color:#8b0000'>"location : $($table.location)"</span>
<span style='color:#ff4500'>$li</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>ServerName</span> <span style='color:#a9a9a9'>=</span> <span style='color:#ff4500'>$servername</span>
<span style='color:#ff4500'>$li</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>DatabaseName</span> <span style='color:#a9a9a9'>=</span> <span style='color:#ff4500'>$databasename</span>
<span style='color:#ff4500'>$li</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>UserID</span> <span style='color:#a9a9a9'>=</span> <span style='color:#ff4500'>$userId</span>
<span style='color:#ff4500'>$li</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>Password</span> <span style='color:#a9a9a9'>=</span> <span style='color:#ff4500'>$password</span>
<span style='color:#ff4500'>$Table</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>ApplyLogOnInfo</span><span style='color:#000000'>(</span><span style='color:#ff4500'>$tli</span><span style='color:#000000'>)</span>
<span style='color:#006400'># the following doesn't work as in the past</span>
<span style='color:#00008b'>if</span> <span style='color:#000000'>(</span> <span style='color:#ff4500'>$table</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>location</span> <span style='color:#a9a9a9'>-contains</span> <span style='color:#8b0000'>'.'</span><span style='color:#000000'>)</span>
<span style='color:#000000'>{</span>
<span style='color:#ff4500'>$table</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>location</span> <span style='color:#a9a9a9'>-match</span> <span style='color:#8b0000'>'(.*)\.(.*)'</span>
<span style='color:#ff4500'>$table</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>location</span> <span style='color:#a9a9a9'>=</span> <span style='color:#8b0000'>"$($databasename).dbo.$($matches[2])"</span>
<span style='color:#000000'>}</span>
<span style='color:#ff4500'>$table</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>location</span>
<span style='color:#000000'>}</span>
<span style='color:#ff4500'>$rv</span> <span style='color:#a9a9a9'>=</span> <span style='color:#0000ff'>New-Object</span> <span style='color:#8a2be2'>CrystalDecisions.Windows.Forms.CrystalReportViewer</span>
<span style='color:#ff4500'>$rv</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>ReportSource</span> <span style='color:#a9a9a9'>=</span> <span style='color:#ff4500'>$report</span>
<span style='color:#ff4500'>$rv</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>Dock</span> <span style='color:#a9a9a9'>=</span> <span style='color:#008080'>[System.Windows.Forms.DockStyle]</span><span style='color:#a9a9a9'>::</span><span style='color:#000000'>Fill</span>
<span style='color:#ff4500'>$form</span> <span style='color:#a9a9a9'>=</span> <span style='color:#0000ff'>New-Object</span> <span style='color:#8a2be2'>Windows.Forms.Form</span>
<span style='color:#ff4500'>$form</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>Height</span> <span style='color:#a9a9a9'>=</span> <span style='color:#800080'>810</span>
<span style='color:#ff4500'>$form</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>Width</span><span style='color:#a9a9a9'>=</span> <span style='color:#800080'>1210</span>
<span style='color:#ff4500'>$form</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>Controls</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>Add</span><span style='color:#000000'>(</span><span style='color:#ff4500'>$rv</span><span style='color:#000000'>)</span>
<span style='color:#ff4500'>$rv</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>Show</span><span style='color:#000000'>(</span><span style='color:#000000'>)</span>
<span style='color:#ff4500'>$form</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>ShowDialog</span><span style='color:#000000'>(</span><span style='color:#000000'>)</span>
<span style='color:#ff4500'>$rv</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>Dispose</span><span style='color:#000000'>(</span><span style='color:#000000'>)</span>
<span style='color:#ff4500'>$report</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>Dispose</span><span style='color:#000000'>(</span><span style='color:#000000'>)</span>
<span style='color:#ff4500'>$form</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>Dispose</span><span style='color:#000000'>(</span><span style='color:#000000'>)</span>
<span style='color:#000000'>}</span>
</pre>Bernd Krisziohttp://www.blogger.com/profile/03294997372899922987noreply@blogger.com0tag:blogger.com,1999:blog-8903006743421146366.post-34589367283286699502010-04-24T04:03:00.000-07:002010-04-24T07:30:07.113-07:00WPF Linkcollection for PowerShellDuring the last time I played with some ways to present GUI from PowerShell. I spend some time collection links, which I want to share now.<br /><br />First there are two toolkits between you can select.<br /><br /><a href="http://code.msdn.microsoft.com/PowerShellPack">WPK which is part of PowerShellPack</a> and <a href="http://powerboots.codeplex.com/">Powerboots</a>.<br /><br />WPK is created by <a href="http://blogs.msdn.com/mediaandmicrocode/">James brundage</a><br />and it comes with some examples in the <span style="font-weight:bold;">Modules\WPK\Examples</span> folder.<br /><br />Videos:<br /><a href="http://channel9.msdn.com/posts/philpenn/Windows-PowerShell--A-Brief-Introduction-to-using-WPF-Containers-in-WPK/">A Brief Introduction to using WPF-Containers</a><br /><a href="http://channel9.msdn.com/posts/LarryLarsen/Multitouch-Fingerpaint-in-30-Lines-of-PowerShell-Script/">Multitouch Fingerpaint in 30 Lines of PowerShell Script</a><br /><br />Some blog postings:<br /><a href="http://blogs.technet.com/heyscriptingguy/archive/2010/03/24/hey-scripting-guy-march-24-2010.aspx">Hey, Scripting Guy! Using the Windows Presentation Foundation PowerShell Kit to Create a GUI</a><br /><a href="http://dougfinke.com/blog/index.php/2009/10/18/powershell-picture-viewer-using-wpf-and-wpk/">PowerShell Picture Viewer using WPF and WPK</a><br /><a href="http://blogs.msdn.com/mediaandmicrocode/archive/2009/12/28/write-progress-wpk.aspx">Write-Progress & WPK<br /></a><br /><a href="http://blogs.msdn.com/mediaandmicrocode/archive/2009/12/09/test-spelling.aspx">Test-Spelling</a><br /><br />Projects using WPK:<br /><a href="http://sqlpsx.codeplex.com/">SQLPSX</a><br /><a href="http://trypowershell.codeplex.com/">Try PowerShell (currently only in source not released)</a><br /><br /><br />PowerBoots is created by <a href="http://huddledmasses.org/">Joel Bennett (Jaykul) of HuddledMasses.org</a><br />and when installed, you find the examples in ...\Modules\PowerBoots\Samples\Samples.ps1<br /><br />Some Blogs about it:<br /><a href="http://blogs.msdn.com/powershell/archive/2009/06/18/powerboots-0-2-is-now-available.aspx">PowerBoots 0.2</a><br /><a href="http://dougfinke.com/blog/index.php/2009/10/18/powershell-picture-viewer-using-wpf-and-wpk/">PowerShell Picture Viewer using PowerBoots</a><br /><br /><a href="http://huddledmasses.org/tag/wpf/">Jaykuls tagged WPF</a><br /><br /><a href="http://huddledmasses.org/wpf-in-powershell-powerboots-02/ ">Posts Tagged ‘WPF’</a><br /><a href="http://huddledmasses.org/powerboots-tutorial-walkthrough">PowerBoots: The tutorial walkthrough</a><br /><a href="http://huddledmasses.org/powerboots-loading-xaml-windows-in-powershell-10-or-20/">PowerBoots: Loading XAML Windows in PowerShell 1.0 or 2.0</a><br /><br />Creating GUI with Powershell and WPF was possible without the aid of toolkits, even in the dark adges of CTP 2 and CTP 3. Code from that early time needs to be adopted to run today, but for the curious, you find interessting stuff in it too.<br />Thanks to <a href="http://max-pit.spaces.live.com/blog/cns!A034D6A0DDC4E64E!1048.entry?sa=78780462">Max Trinidad</a> for his post containing a link to the old trasures.<br /><br />and here is th eearly serie from James Brundage:<br /><a href="http://blogs.msdn.com/powershell/archive/2008/05/22/wpf-powershell-part-1-hello-world-welcome-to-the-week-of-wpf.aspx">Part 1</a><br /><a href="http://blogs.msdn.com/powershell/archive/2008/05/23/wpf-powershell-part-2-exploring-wpf-and-the-rest-of-net-with-scripts.aspx">Part 2</a><br /><a href="http://blogs.msdn.com/powershell/archive/2008/05/24/wpf-powershell-part-3-handling-events.aspx">Part 3</a><br /><a href="http://blogs.msdn.com/powershell/archive/2008/05/25/wpf-powershell-part-4-xaml-show-control.aspx">Part 4</a><br /><a href="http://blogs.msdn.com/powershell/archive/2008/05/26/wpf-powershell-part-5-using-wpf-powershell-modules.aspx">Part 5</a><br /><a href="http://blogs.msdn.com/powershell/archive/2008/05/27/wpf-powershell-part-6-running-functions-in-the-background.aspx">Part 6</a><br /><a href="http://blogs.msdn.com/powershell/archive/2008/05/28/wpf-powershell-part-7-sharing-hosts.aspx">Part 7</a><br /><br />To make the examples run in PowerShell V2 you need help from the following posts<br /><a href="http://blogs.msdn.com/powershell/archive/2008/12/24/windows-powershell-ctp2-to-ctp3-conversion-guide.aspx">Windows PowerShell CTP2 to CTP3 Conversion Guide by James Brundage</a> <br /><a href="http://www.nivot.org/2008/12/23/PowerShell20CTP3HasArrived.aspx">PowerShell 2.0 CTP3 has arrived! by Oisin Grehan</a><br /><br /><br /><a href="http://blogs.msdn.com/powershell/archive/tags/WPF/default.aspx">Look for WPF tag in PowerShell Team Blog</a><br /><br />finally you may want to look at some general WPF links <br /><a href="http://wpftutorial.net/Home.html">WPF Tutorial by Christian Mosers</a><br /><a href="http://weblogs.asp.net/pleloup/archive/2007/05/22/wpf-and-xaml-tutorials.aspx">WPF and XAML Tutorials by Josh Smith</a><br /><br />Now have some fun, exploring WPF by yourself.<br /><br />BerndBernd Krisziohttp://www.blogger.com/profile/03294997372899922987noreply@blogger.com1tag:blogger.com,1999:blog-8903006743421146366.post-61074441251740894082010-04-02T01:26:00.000-07:002010-04-02T01:51:49.369-07:00PowerShell modules are funny guys (III)Today I continue to write about some advanced features, you only touch when you are importing some modules and than to remove some of them again.<br /><br />To demonstate I create two modules, the first to be used as submodule by the second. I call it mod_s<br /><pre class='PowerShellColorizedScript'><span style='color:#ff4500'>$script:state_s</span> <span style='color:#a9a9a9'>=</span> <span style='color:#800080'>0</span> <br /><span style='color:#0000ff'>Write-host</span> <span style='color:#8b0000'>"Executing mod_s"</span> <br /> <br /><span style='color:#ff4500'>$mInfo</span> <span style='color:#a9a9a9'>=</span> <span style='color:#ff4500'>$MyInvocation</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>MyCommand</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>ScriptBlock</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>Module</span> <br /><span style='color:#ff4500'>$mInfo</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>OnRemove</span> <span style='color:#a9a9a9'>=</span> <span style='color:#000000'>{</span> <span style='color:#0000ff'>Write-Host</span> <span style='color:#8b0000'>"Removing module mod_s"</span> <span style='color:#000000'>}</span> <br /> <br /><span style='color:#00008b'>function</span> <span style='color:#8a2be2'>Get-State</span> <br /><span style='color:#000000'>{</span> <br /> <span style='color:#00008b'>return</span> <span style='color:#ff4500'>$script:state_s</span> <br /><span style='color:#000000'>}</span> <br /> <br /><span style='color:#00008b'>function</span> <span style='color:#8a2be2'>Set-State</span> <span style='color:#000000'>(</span><span style='color:#ff4500'>$x</span><span style='color:#000000'>)</span> <br /><span style='color:#000000'>{</span> <br /> <span style='color:#ff4500'>$script:state_s</span> <span style='color:#a9a9a9'>=</span> <span style='color:#000000'>(</span><span style='color:#ff4500'>$x</span><span style='color:#000000'>)</span> <br /><span style='color:#000000'>}</span> <br /> <br /><span style='color:#00008b'>function</span> <span style='color:#8a2be2'>Set-ModSUnremovable</span> <br /><span style='color:#000000'>{</span> <br /> <span style='color:#ff4500'>$mInfo</span> <span style='color:#a9a9a9'>=</span> <span style='color:#ff4500'>$MyInvocation</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>MyCommand</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>ScriptBlock</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>Module</span> <br /> <span style='color:#ff4500'>$mInfo</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>AccessMode</span> <span style='color:#a9a9a9'>=</span> <span style='color:#8b0000'>"readonly"</span> <br /><span style='color:#000000'>}</span> <br /> <br /><span style='color:#00008b'>function</span> <span style='color:#8a2be2'>Set-ModSremovable</span> <br /><span style='color:#000000'>{</span> <br /> <span style='color:#ff4500'>$mInfo</span> <span style='color:#a9a9a9'>=</span> <span style='color:#ff4500'>$MyInvocation</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>MyCommand</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>ScriptBlock</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>Module</span> <br /> <span style='color:#ff4500'>$mInfo</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>AccessMode</span> <span style='color:#a9a9a9'>=</span> <span style='color:#8b0000'>"ReadWrite"</span> <br /><span style='color:#000000'>}</span> <br /></pre><br />Se second is used to import this module, I call it mod_a:<br /><pre class='PowerShellColorizedScript'><span style='color:#0000ff'>Write-host</span> <span style='color:#8b0000'>"Executing mod_a"</span> <br /><span style='color:#0000ff'>Import-Module</span> <span style='color:#8a2be2'>mod_s.psm1</span> <br /><span style='color:#0000ff'>Write-host</span> <span style='color:#8b0000'>"Old state $(Get-state)"</span> <br /><span style='color:#0000ff'>Set-state</span> <span style='color:#800080'>11</span> <br /> <br /><span style='color:#00008b'>function</span> <span style='color:#8a2be2'>Get-StateA</span> <br /><span style='color:#000000'>{</span> <br /> <span style='color:#00008b'>return</span> <span style='color:#0000ff'>Get-State</span> <br /><span style='color:#000000'>}</span> <br /> <br /><span style='color:#00008b'>function</span> <span style='color:#8a2be2'>Set-StateA</span> <span style='color:#000000'>(</span><span style='color:#ff4500'>$x</span><span style='color:#000000'>)</span> <br /><span style='color:#000000'>{</span> <br /> <span style='color:#0000ff'>Set-State</span> <span style='color:#ff4500'>$x</span> <br /><span style='color:#000000'>}</span> <br /></pre><br /><br />Now let us see what happens, when I first import mod_s and than mod_a and finally remove mod_s<br /><br /><blockquote><br />1]10:33:45 C:\Var\bin (FileSystem)<br />ipmo mod_s<br />Executing mod_s<br /><br />_____________________________________________________________________________________________________<br />[2]10:33:50 C:\Var\bin (FileSystem)<br />ipmo mod_a<br />Executing mod_a<br />Old state 0<br /><br />_____________________________________________________________________________________________________<br />[3]10:33:57 C:\Var\bin (FileSystem)<br />rmo mod_a<br />Removing module mod_s<br /><br />_____________________________________________________________________________________________________<br />[4]10:34:02 C:\Var\bin (FileSystem)<br />gmo<br /><br />ModuleType Name ExportedCommands <br />---------- ---- ---------------- <br />Script ISECreamBasic {get-AvailableModuleList, Remove-IseMenu, Add-IseMenu} <br /><br /><br /><br />_____________________________________________________________________________________________________<br />[5]</blockquote><br /><br />Let us remember, that mod_s is removed and the global reference to mod_s is removed.<br /><br />Next let us keep a referenc to mod_s before removing mod_a:<br /><blockquote>[1]10:39:04 C:\Var\bin (FileSystem)<br />ipmo mod_s<br />Executing mod_s<br /><br />_____________________________________________________________________________________________________<br />[2]10:40:52 C:\Var\bin (FileSystem)<br />$a = gmo mod_s<br /><br />_____________________________________________________________________________________________________<br />[3]10:40:58 C:\Var\bin (FileSystem)<br />ipmo mod_a<br />Executing mod_a<br />Old state 0<br /><br />_____________________________________________________________________________________________________<br />[4]10:41:03 C:\Var\bin (FileSystem)<br />rmo mod_a<br />Removing module mod_s<br /><br />_____________________________________________________________________________________________________<br />[5]10:41:07 C:\Var\bin (FileSystem)<br />$a<br /><br />ModuleType Name ExportedCommands <br />---------- ---- ---------------- <br />Script mod_s {Set-ModSUnremovable, Set-State, Set-ModSremovable, Get-State} <br /><br /><br /><br />_____________________________________________________________________________________________________<br />[6]10:41:12 C:\Var\bin (FileSystem)<br />gmo<br /><br />ModuleType Name ExportedCommands <br />---------- ---- ---------------- <br />Script ISECreamBasic {get-AvailableModuleList, Remove-IseMenu, Add-IseMenu} <br /><br /><br /><br />_____________________________________________________________________________________________________<br />[7]<br /></blockquote><br /><br />Here you see, that mod_s doesn't execute its termination code. I get the impression, that they use some sort of reference count to modules and when it drops down to 0. The module gets unloaded from memory.<br /><br />The fact that the reference to the global scope is removed, by removing a submodul makes it difficult to ensure that some modules are imported.<br /><br />Lets study <a href="http://dotnetslackers.com/articles/net/Converting-a-PowerShell-Script-into-a-Module-part2.aspx">Bruce Payette</a>. He has a section Controlling when modules can be unloaded.<br /><br />Let's try:<br /><blockquote>[1]10:49:09 C:\Var\bin (FileSystem)<br />ipmo mod_s<br />Executing mod_s<br /><br />_____________________________________________________________________________________________________<br />[2]10:49:15 C:\Var\bin (FileSystem)<br />Set-ModSUnremovable<br /><br />_____________________________________________________________________________________________________<br />[3]10:49:23 C:\Var\bin (FileSystem)<br />Set-ModSremovable<br /><br />_____________________________________________________________________________________________________<br />[4]10:49:30 C:\Var\bin (FileSystem)<br />Set-ModSUnremovable<br /><br />_____________________________________________________________________________________________________<br />[5]10:49:39 C:\Var\bin (FileSystem)<br />ipmo mod_a<br />Executing mod_a<br />Old state 0<br /><br />_____________________________________________________________________________________________________<br />[6]10:49:45 C:\Var\bin (FileSystem)<br />rmo mod_a<br />Remove-Module : Unable to remove module 'mod_s' because it is read-only. Use the -force flag to remove r<br />ead-only modules.<br />At line:1 char:4<br />+ rmo <<<< mod_a<br /> + CategoryInfo : PermissionDenied: (mod_s:PSModuleInfo) [Remove-Module], InvalidOperationE <br /> xception<br /> + FullyQualifiedErrorId : Modules_ModuleIsReadOnly,Microsoft.PowerShell.Commands.RemoveModuleComman <br /> d<br /> <br /><br />_____________________________________________________________________________________________________<br />[7]10:49:49 C:\Var\bin (FileSystem)<br />Set-ModSremovable<br />The term 'Set-ModSremovable' is not recognized as the name of a cmdlet, function, script file, or operab<br />le program. Check the spelling of the name, or if a path was included, verify that the path is correct a<br />nd try again.<br />At line:1 char:18<br />+ Set-ModSremovable <<<< <br /> + CategoryInfo : ObjectNotFound: (Set-ModSremovable:String) [], CommandNotFoundException<br /> + FullyQualifiedErrorId : CommandNotFoundException<br /> <br /><br />_____________________________________________________________________________________________________<br />[8]<br /></blockquote><br /><br />Anyone not calling this a bug around ?<br /><br />BerndBernd Krisziohttp://www.blogger.com/profile/03294997372899922987noreply@blogger.com0tag:blogger.com,1999:blog-8903006743421146366.post-7534826751652004342010-03-30T23:00:00.000-07:002010-03-30T23:07:18.884-07:00PowerShell modules are funny guys (II)Here I'll focus on the irritating feature. The following script creates 2 modules in your user modul folder.<br />The second module imports the first module.<br /><br />Import the first module, then the second. Both modules are imported.<br />Now remove the second module (that which imports the other too) and both are gone.<br /><br /><pre class='PowerShellColorizedScript'><span style='color:#ff4500'>$modpath1</span> <span style='color:#a9a9a9'>=</span> <span style='color:#000000'>(</span><span style='color:#ff4500'>$env:PSModulepath</span> <span style='color:#a9a9a9'>-split</span> <span style='color:#8b0000'>';'</span><span style='color:#000000'>)</span><span style='color:#a9a9a9'>[</span><span style='color:#800080'>0</span><span style='color:#a9a9a9'>]</span> <br /><span style='color:#0000ff'>mkdir</span> <span style='color:#8a2be2'>$modpath1\submod</span> <span style='color:#000080'>-force</span> <br /><span style='color:#0000ff'>mkdir</span> <span style='color:#8a2be2'>$modpath1\testmod</span> <span style='color:#000080'>-force</span> <br /> <br /> <br /> <br /><span style='color:#8b0000'>@'<br />$script:state_s = 0<br /><br />function Get-State<br />{<br /> return $script:state_s<br />}<br /><br />function Set-State ($x)<br />{<br /> $script:state_s = ($x)<br />}<br />'@</span> <span style='color:#a9a9a9'>></span> <span style='color:#8b0000'>"$modpath1\submod\submod.psm1"</span> <br /> <br /> <br /><span style='color:#8b0000'>@'<br />Import-Module submod.psm1<br /><br /><br />function Get-StateY<br />{<br /> return Get-State<br />}<br /><br />function Set-StateY ($x)<br />{<br /> Set-State $x<br />}<br />'@</span> <span style='color:#a9a9a9'>></span> <span style='color:#8b0000'>"$modpath1\testmod\testmod.psm1"</span> <br /> <br /><span style='color:#0000ff'>ipmo</span> <span style='color:#8a2be2'>submod</span> <br /><span style='color:#0000ff'>ipmo</span> <span style='color:#8a2be2'>testmod</span> <br /><span style='color:#0000ff'>Write-host</span> <span style='color:#8b0000'>'------------'</span> <br /><span style='color:#0000ff'>gmo</span> <br /><span style='color:#0000ff'>Write-host</span> <span style='color:#8b0000'>'------------'</span> <br /><span style='color:#0000ff'>rmo</span> <span style='color:#8a2be2'>testmod</span> <br /><span style='color:#0000ff'>gmo</span> <br /></pre><br /><br />Do you agree, that is to be called a bug?<br /><br />BerndBernd Krisziohttp://www.blogger.com/profile/03294997372899922987noreply@blogger.com0tag:blogger.com,1999:blog-8903006743421146366.post-75130983613386991222010-03-29T02:51:00.000-07:002010-03-29T03:48:20.057-07:00How to Organize Modules you Develop in ProjectsToday I want to tell about the ways I organise my modul development.<br /><br />When you start to develop PowerShell modules it is relative easy. Just add a folder under <br /><pre class='PowerShellColorizedScript'><span style='color:#000000'>(</span><span style='color:#0000ff'>Split-path</span> <span style='color:#ff4500'>$profile</span><span style='color:#000000'>)</span> <span style='color:#a9a9a9'>+</span> <span style='color:#8b0000'>'\Modules'</span> <br /></pre><br />say MyMod.<br />Then put a file MyMod.psm1 into it<br /><br /><pre class='PowerShellColorizedScript'><span style='color:#0000ff'>Write-host</span> <span style='color:#8b0000'>"mymod loaded"</span></pre><br /><br />When you type <span style="font-weight:bold;">ipmo Mymod</span>, which is the alias for Import-Module you get<br /><br /><br /><blockquote>mymod loaded<br /></blockquote><br /><br />With <span style="font-weight:bold;">rmo Mymod</span> , which is the short for Remove-Module you get rid of your module.<br /><br />To check that it is removed, you can type <br /><span style="font-weight:bold;">gmo</span> or <span style="font-weight:bold;">gmo Mymod</span>, which is the alias for Get-Module<br /><br />Now you shouldn't see it anymore.<br /><br />Here I'm not going to tell you, what to put into your module file.<br /><br />Best link for this I know is this post from <a href="http://dotnetslackers.com/articles/net/Converting-a-PowerShell-Script-into-a-Module.aspx">Bruce Payette</a>.<br /><br />My focus here is, how to organise your system, when you have written something usefull and uploaded your module to some project.<br /><br />I guess that you checkout your module from the project to some place, which is not in your modules folders. To ckeck where your system searches for modules just type<br /><br /><pre class='PowerShellColorizedScript'><span style='color:#ff4500'>$env:PSModulePath</span> <span style='color:#a9a9a9'>-split</span> <span style='color:#8b0000'>';'</span></pre><br /><br />Next I tried to be clever. I put the following into my profile:<br /><br /><pre class='PowerShellColorizedScript'><span style='color:#ff4500'>$env:PSModulePath</span> <span style='color:#a9a9a9'>+=</span> <span style='color:#8b0000'>";\D:\MysharedProject\modules"</span></pre><br /><br />Now I can load the modules from this project by simple calles of ipmo myModule.<br /><br />It really took some time to discover that I was too clever. <br /><br />I'm using ISE for nearly all my PowerShell task and some time ago I discovered the menu <span style="font-weight:bold;">New PowerShell Tab</span> with the short-cut CTRL+T.<br /><br />Each time you invoke this, you get a new PowerShell session and your PowerShell Profiles are executed. <br /><br />But all these PowerShell session live in your current Windows session and each time the PSModulePath variable of the Windows environment will be modified. It gets longer and longer.<br /><br />OK here now my final solution, this is the start of my profile.ps1<br /><br /><pre class='PowerShellColorizedScript'><span style='color:#0000ff'>write-host</span> <span style='color:#8b0000'>"================== generall ($env:USERDOMAIN.$env:username) ==========================="</span> <br /> <br /><span style='color:#ff4500'>$AdditionalModulPathes</span> <span style='color:#a9a9a9'>=</span> <span style='color:#000000'>@(</span> <br /><span style='color:#8b0000'>"D:\someProjectBleedingEdge\Modules"</span> <br /><span style='color:#8b0000'>"D:\someProjectRelease\Modules"</span> <br /><span style='color:#006400'># "D:\MyCopyOfSomeProject\Modules"</span> <br /><span style='color:#000000'>)</span> <br /> <br /> <br /><span style='color:#00008b'>foreach</span> <span style='color:#000000'>(</span><span style='color:#ff4500'>$path</span> <span style='color:#00008b'>in</span> <span style='color:#000000'>(</span><span style='color:#ff4500'>$AdditionalModulPathes</span><span style='color:#a9a9a9'>|</span> <span style='color:#0000ff'>Get-unique</span><span style='color:#000000'>)</span> <span style='color:#000000'>)</span> <br /><span style='color:#000000'>{</span> <br /> <span style='color:#00008b'>if</span> <span style='color:#000000'>(</span><span style='color:#000000'>(</span><span style='color:#ff4500'>$env:PSModulePath</span> <span style='color:#a9a9a9'>-split</span> <span style='color:#8b0000'>';'</span><span style='color:#000000'>)</span> <span style='color:#a9a9a9'>-notcontains</span> <span style='color:#ff4500'>$path</span><span style='color:#000000'>)</span> <br /> <span style='color:#000000'>{</span> <br /> <span style='color:#ff4500'>$env:PSModulePath</span> <span style='color:#a9a9a9'>+=</span> <span style='color:#8b0000'>";"</span> <span style='color:#a9a9a9'>+</span> <span style='color:#ff4500'>$path</span> <br /> <span style='color:#000000'>}</span> <br /><span style='color:#000000'>}</span> <br /> <br /><span style='color:#006400'># $env:PSModulePath -split ';'</span> <br /></pre><br /><br /><br />Now I modify the evironment only once. <br />And here is the location, where I select whether I use the stable release, the current development version or my own unpublished work.<br /><br />I hope this give you some ideas.<br /><br />Bernd<br /><br />BTW: ISE-Cream has reached <a href="http://psisecream.codeplex.com/releases/view/42680">release 0.1</a> <br />If you are thinking about extending ISE, take a look at itBernd Krisziohttp://www.blogger.com/profile/03294997372899922987noreply@blogger.com0tag:blogger.com,1999:blog-8903006743421146366.post-57695663512349628432010-03-25T03:19:00.000-07:002010-03-25T03:44:21.149-07:00PowerShell modules are funny guysEd Wilson says Windows PowerShell 1.0 was the vision, Windows PowerShell 2.0 is the reality [ from the preface of LeeHolmes <a href="http://powershell.labs.oreilly.com/pr01.html">Windows PowerShell Cookbook</a><br />I dare say PowerShell 2.0 ISE and Modules are still a vision.<br /><br />When a module depends on a another module like SQLISE on ICECreamBasic or WPK it seems natural<br />to use within the .psm1 file<br /><pre class='PowerShellColorizedScript'><span style='color:#0000ff'>import-module</span> <span style='color:#8a2be2'>ISECreamBasic</span> <br /><span style='color:#0000ff'>import-module</span> <span style='color:#8a2be2'>SQLParser</span> <br /><span style='color:#0000ff'>import-module</span> <span style='color:#8a2be2'>adolib</span> <br /><span style='color:#0000ff'>import-module</span> <span style='color:#8a2be2'>WPK</span> <br /></pre><br /><br />When I write a second module OracleIse, I can do similar<br /><br /><pre class='PowerShellColorizedScript'><span style='color:#0000ff'>import-module</span> <span style='color:#8a2be2'>ISECreamBasic</span> <br /><span style='color:#0000ff'>import-module</span> <span style='color:#8a2be2'>OracleClient</span> <br /><span style='color:#0000ff'>import-module</span> <span style='color:#8a2be2'>WPK</span> <br /></pre><br /><br />Seems OK so far.<br /><br />Next modules can be <span style="font-weight:bold;">removed </span>using Remove-module and that fires even an event to do some clean up as removing ISE addons menu items<br /><br /><pre class='PowerShellColorizedScript'><span style='color:#ff4500'>$mInfo</span> <span style='color:#a9a9a9'>=</span> <span style='color:#ff4500'>$MyInvocation</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>MyCommand</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>ScriptBlock</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>Module</span> <br /><span style='color:#ff4500'>$mInfo</span><span style='color:#a9a9a9'>.</span><span style='color:#000000'>OnRemove</span> <span style='color:#a9a9a9'>=</span> <span style='color:#000000'>{</span> <br /> <span style='color:#0000ff'>Write-Host</span> <span style='color:#8b0000'>"$($MyInvocation.MyCommand.ScriptBlock.Module.name) removed on $(Get-Date)"</span> <br /> <span style='color:#0000ff'>Remove-IseMenu</span> <span style='color:#8a2be2'>OracleIse</span> <br /><span style='color:#000000'>}</span> <br /></pre><br /><br />Now emove on of these modules an see, which modules are left using<br /><br /><pre class='PowerShellColorizedScript'><span style='color:#0000ff'>get-module</span> <br /></pre><br /><br />You see, that the removed modul removed the modules imported within too, without tribute to the fact that the other module depends on WPK and ISECreamBasic too.<br /><br />Conclusion. Better do not use import-module to import module from within your modules. Import the needed modules in advance.<br /><br />Have some fun trying to find work arounds for this issue.<br /><br />BerndBernd Krisziohttp://www.blogger.com/profile/03294997372899922987noreply@blogger.com2tag:blogger.com,1999:blog-8903006743421146366.post-71180107989621534622010-03-25T02:50:00.000-07:002010-03-25T03:02:26.437-07:00Removing a specific Item from a Powershell ISE Addon menu<span style="font-family: arial;">Well in ISECreamBasic I had written the function Remove-IseMenu to remove Toplevel menus.</span><br /><span style="font-family: arial;">Thanks to Shay Levi and Jaykul it became a nice advanced function with parametersets.</span><br /><br /><span style="font-family: arial;">You can find the code in the </span><a style="font-family: arial;" href="http://psisecream.codeplex.com/SourceControl/changeset/view/41069#812074">ISE-Cream project</a><br /><br /><span style="font-family: arial;">Just when I finished writing the header comments Ravi wanted to use the function not restricted to toplevel menu items.</span><br /><br /><span style="font-family: arial;">First attemps tried to search the tree, but there can be more subitems with the same name hidden in the menu structure. To get it clear I designed the following function, which requires the complete path to the item to delete:</span><br /><br /><span style="font-family: arial;"><pre class="'PowerShellColorizedScript'"><span style="'color:#00008b'">function</span> <span style="'color:#8a2be2'">Remove-IseMenuItem</span> <span style="'color:#000000'">{</span> </span><br /><span style="font-family: arial;"> </span><br /><span style="font-family: arial;"> <span style="'color:#ff4500'">$submenu</span> <span style="'color:#a9a9a9'">=</span> <span style="'color:#ff4500'">$psise</span><span style="'color:#a9a9a9'">.</span><span style="'color:#000000'">CurrentPowerShellTab</span><span style="'color:#a9a9a9'">.</span><span style="'color:#000000'">AddOnsMenu</span><span style="'color:#a9a9a9'">.</span><span style="'color:#000000'">Submenus</span> </span><br /><span style="font-family: arial;"> </span><br /><span style="font-family: arial;"> <span style="'color:#00008b'">for</span> <span style="'color:#000000'">(</span><span style="'color:#ff4500'">$j</span> <span style="'color:#a9a9a9'">=</span> <span style="'color:#800080'">0</span><span style="'color:#000000'">;</span> <span style="'color:#ff4500'">$j</span> <span style="'color:#a9a9a9'">-lt</span> <span style="'color:#ff4500'">$args</span><span style="'color:#a9a9a9'">.</span><span style="'color:#000000'">count</span><span style="'color:#000000'">;</span> <span style="'color:#ff4500'">$j</span><span style="'color:#a9a9a9'">++</span><span style="'color:#000000'">)</span> </span><br /><span style="font-family: arial;"> <span style="'color:#000000'">{</span> </span><br /><span style="font-family: arial;"> <span style="'color:#ff4500'">$name</span> <span style="'color:#a9a9a9'">=</span> <span style="'color:#ff4500'">$args</span><span style="'color:#a9a9a9'">[</span><span style="'color:#ff4500'">$j</span><span style="'color:#a9a9a9'">]</span> </span><br /><span style="font-family: arial;"> <span style="'color:#ff4500'">$count</span> <span style="'color:#a9a9a9'">=</span> <span style="'color:#ff4500'">$submenu</span><span style="'color:#a9a9a9'">.</span><span style="'color:#000000'">count</span> </span><br /><span style="font-family: arial;"> <span style="'color:#006400'">#Write-host "arg0: $name $count"</span> </span><br /><span style="font-family: arial;"> <span style="'color:#00008b'">for</span> <span style="'color:#000000'">(</span><span style="'color:#ff4500'">$i</span> <span style="'color:#a9a9a9'">=</span> <span style="'color:#800080'">0</span><span style="'color:#000000'">;</span> <span style="'color:#ff4500'">$i</span> <span style="'color:#a9a9a9'">-lt</span> <span style="'color:#ff4500'">$submenu</span><span style="'color:#a9a9a9'">.</span><span style="'color:#000000'">count</span><span style="'color:#000000'">;</span> <span style="'color:#ff4500'">$i</span><span style="'color:#a9a9a9'">++</span><span style="'color:#000000'">)</span> </span><br /><span style="font-family: arial;"> <span style="'color:#000000'">{</span> </span><br /><span style="font-family: arial;"> <span style="'color:#ff4500'">$found</span> <span style="'color:#a9a9a9'">=</span> <span style="'color:#ff4500'">$false</span> </span><br /><span style="font-family: arial;"> <span style="'color:#00008b'">if</span> <span style="'color:#000000'">(</span><span style="'color:#ff4500'">$Submenu</span><span style="'color:#a9a9a9'">[</span><span style="'color:#ff4500'">$i</span><span style="'color:#a9a9a9'">]</span><span style="'color:#a9a9a9'">.</span><span style="'color:#000000'">DisplayName</span> <span style="'color:#a9a9a9'">-eq</span> <span style="'color:#ff4500'">$name</span><span style="'color:#000000'">)</span> </span><br /><span style="font-family: arial;"> <span style="'color:#000000'">{</span> </span><br /><span style="font-family: arial;"> <span style="'color:#006400'">#Write-host "found toplevel menu $name"</span> </span><br /><span style="font-family: arial;"> <span style="'color:#ff4500'">$found</span> <span style="'color:#a9a9a9'">=</span> <span style="'color:#ff4500'">$True</span> </span><br /><span style="font-family: arial;"> <span style="'color:#00008b'">if</span> <span style="'color:#000000'">(</span> <span style="'color:#ff4500'">$j</span> <span style="'color:#a9a9a9'">-eq</span> <span style="'color:#000000'">(</span><span style="'color:#ff4500'">$args</span><span style="'color:#a9a9a9'">.</span><span style="'color:#000000'">count</span> <span style="'color:#a9a9a9'">-</span> <span style="'color:#800080'">1</span><span style="'color:#000000'">)</span><span style="'color:#000000'">)</span> </span><br /><span style="font-family: arial;"> <span style="'color:#000000'">{</span> </span><br /><span style="font-family: arial;"> <span style="'color:#0000ff'">Write-host</span> <span style="'color:#8b0000'">'Removing'</span> </span><br /><span style="font-family: arial;"> <span style="'color:#ff4500'">$null</span> <span style="'color:#a9a9a9'">=</span> <span style="'color:#ff4500'">$submenu</span><span style="'color:#a9a9a9'">.</span><span style="'color:#000000'">remove</span><span style="'color:#000000'">(</span><span style="'color:#ff4500'">$Submenu</span><span style="'color:#a9a9a9'">[</span><span style="'color:#ff4500'">$i</span><span style="'color:#a9a9a9'">]</span><span style="'color:#000000'">)</span> </span><br /><span style="font-family: arial;"> <span style="'color:#000000'">}</span> </span><br /><span style="font-family: arial;"> <span style="'color:#00008b'">else</span> </span><br /><span style="font-family: arial;"> <span style="'color:#000000'">{</span> </span><br /><span style="font-family: arial;"> <span style="'color:#ff4500'">$submenu</span> <span style="'color:#a9a9a9'">=</span> <span style="'color:#ff4500'">$Submenu</span><span style="'color:#a9a9a9'">[</span><span style="'color:#ff4500'">$i</span><span style="'color:#a9a9a9'">]</span><span style="'color:#a9a9a9'">.</span><span style="'color:#000000'">submenus</span> </span><br /><span style="font-family: arial;"> <span style="'color:#000000'">}</span> </span><br /><span style="font-family: arial;"> <span style="'color:#00008b'">break</span> </span><br /><span style="font-family: arial;"> <span style="'color:#000000'">}</span> </span><br /><span style="font-family: arial;"> <span style="'color:#000000'">}</span> </span><br /><span style="font-family: arial;"> <span style="'color:#00008b'">if</span> <span style="'color:#000000'">(</span><span style="'color:#a9a9a9'">!</span> <span style="'color:#ff4500'">$found</span><span style="'color:#000000'">)</span> </span><br /><span style="font-family: arial;"> <span style="'color:#000000'">{</span> </span><br /><span style="font-family: arial;"> <span style="'color:#0000ff'">Write-host</span> <span style="'color:#8b0000'">"Menuitem not found"</span> </span><br /><span style="font-family: arial;"> <span style="'color:#00008b'">break</span> </span><br /><span style="font-family: arial;"> <span style="'color:#000000'">}</span> </span><br /><span style="font-family: arial;"> </span><br /><span style="font-family: arial;"> <span style="'color:#000000'">}</span> </span><br /><span style="font-family: arial;"><span style="'color:#000000'">}</span> </span><br /><span style="font-family: arial;"> </span><br /><span style="font-family: arial;"><span style="'color:#0000ff'">Remove-IseMenuItem</span> <span style="'color:#8a2be2'">Edit</span> <span style="'color:#8a2be2'">Pad</span> </span><br /><span style="font-family: arial;"> </span><br /><span style="font-family: arial;"><span style="'color:#006400'"># Remove-Child $Submenu[$i] $Name | out-null</span> </span><br /><span style="font-family: arial;"></pre></span><br /><br /><br /><span style="font-family: arial;">But this looks like an old PowerShell V1 coding style. It doesn't check that all arguments are strings. Please can someone show me, how to set up a PowerShell V2 advanced function parameter declaration for this.</span>Bernd Krisziohttp://www.blogger.com/profile/03294997372899922987noreply@blogger.com5