Sometimes you pull you hairs out because the execution of a command just does not seem to work the way you want from PowerShell.
A good example of this is the following case:
Given a folder on the filesystem, I want to determine if the folder is under TFS source control, and if it is, what is the server name of the TFS server and the path of folder in TFS.
If you don’t use integrated security (some development machines are not domain joined) you can determine this using the command tf.exe workfold c:\projects\myproject /login:domain\username,password
From PowerShell I execute this command as follows:
$tfExe = "C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\Tf.exe"
$projectFolder = "D:\Projects\Macaw.SolutionsFactory\TEST\Macaw.TestTfs"
$username = "domain\username"
$password = "password"
& $tfExe workfold $projectFolder /login:$username,$password
But I got the the following error:
TF10125: The path 'D:\Projects\MyProject' must start with $/
I just couldn’t get it working, so I created a small batch file ExecTfWorkprodCommand.bat with the following content:
@echo off
rem This is a kind of strange batch file, needed because execution of this command in PowerShell gives an error.
rem This script retrieves TFS sourcecontrol information about a local folder using the following command:
rem tf workfold <localpath> /login:domain\user,password
rem %1 is path to the tf.exe executable
rem %2 is the local path
rem %3 is the domain\user
rem %4 is the password
rem Output is in format:
rem ===============================================================================
rem Workspace: MyProject@VS-D-SVDOMOSS-1 (Serge)
rem Server : tfs.yourcompany.nl
rem $/MyProject/trunk: C:\Projects\MyProject
if [%3]==[] goto integratedsecurity
%1 workfold "%2" /login:%3,%4
goto end
:integratedsecurity
%1 workfold "%2"
:end
And called this script file from PowerShell as follows:
$helperScript = "ExecTfWorkprodCommand.bat"
$tfExe = "C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\Tf.exe"
$projectFolder = "D:\Projects\Macaw.SolutionsFactory\TEST\Macaw.TestTfs"
$username = "macaw\serge"
$password = "Trasimeno9"
$helperScript $tfExe "`"$projectFolder`"" $username $password
This is way to much work, but I just couldn’t get it working.
Today I read a post that mentioned the tool EchoArgs.exe, available in the PowerShell Community Extensions (http://pscx.codeplex.com), which echo’s the arguments as the executed application receives them from PowerShell.
I changed my script code to:
$tfExe = "C:\Program Files\PowerShell Community Extensions\EchoArgs.exe"
$projectFolder = "D:\Projects\MyProject"
$username = "domain\username"
$password = "password"
& $tfExe workfold $projectFolder /login:$username,$password
Which resulted in:
Arg 0 is <workfold>
Arg 1 is <D:\Projects\MyProject>
Arg 2 is </login:domain\username>
Arg 3 is <password>
And this directly resolved my issue! the “,” in “/login:$username,$password” did split the argument!
The issue was simple resolved by using the following command from PowerShell:
& $tfExe workfold $projectFolder /login:"$username,$password"
Which results in:
Arg 0 is <workfold>
Arg 1 is <D:\Projects\MyProject>
Arg 2 is </login:domain\username,password>
Conclusion: issues with executing programs from PowerShell, check out EchoArgs.exe!
My second article on using WPF from PowerShell. You can download WPK as part of the Windows 7 Resource Kit PowerShell Pack. When you can navigate to the folder <My Documents>\WindowsPowerShell\Modules you see the modules that are installed. The folder WPK contains the WPK module.
On the Modules level a few documents are installed:
- Readme1st.txt – information on installing, using, uninstalling the PowerShellPack
- About the Windows 7 Resource Kit PowerShell Pack.docx – an overview of the available modules
- Writing User Interfaces with WPK.docx - the first place to get started when working with WPK
Especially the Readme1st.txt contains two interesting pieces of information:
- The file starts with the following text:
Readme for the Windows 7 Resource Kit PowerShell Pack
by James Brundage
Copyright (c) 2009 by Microsoft Corporation
Portions copyright (c) 2009 James Brundage
All Rights Reserved
I thought James Brundage is an employee of Microsoft, why does he own portions of the copyright?
- The disclaimer contains the following text:
The Windows 7 Resource Kit PowerShell Pack included on the companion CD is
unsupported by Microsoft and is provided to you as-is, with no warranty or
guarantee concerning its functionality. For the latest news and usage tips
concerning this PowerShell Pack, see the Windows PowerShell Team Blog at
http://blogs.msdn.com/powershell/.
So no support from Microsoft’s side. I wonder how issues will be resolved and new releases will be published.
Ok, and now on to some programming. In the document Writing User Interfaces with WPK.docx we find a nice example of a process viewer that updates the list of processes every 15 seconds.
A simple process viewer
- New-ListView -Width 350 -Height 350 -DataBinding @{
- ItemsSource = New-Binding -IsAsync -UpdateSourceTrigger PropertyChanged -Path Output
- } -View {
- New-GridView -AllowsColumnReorder -Columns {
- New-GridViewColumn "Name"
- New-GridViewColumn "Id"
- }
- } -DataContext {
- Get-PowerShellDataSource -Script {
- Get-Process | ForEach-Object { $_ ; Start-Sleep -Milliseconds 25 }
- }
- } -On_Loaded {
- Register-PowerShellCommand -Run -In "0:0:15" -ScriptBlock {
- $window.Content.DataContext.Script = $window.Content.DataContext.Script
- }
- } -asjob
See the document for a great explanation on how it works.
When looking at this example I had a few questions:
- What if I don’t want to do a timed update, but just bind to some existing data?
- All examples do the data collection in the Get-PowerShellDataSource script block, is it possible to have the data already somewhere in a variable?
- Can I skip the binding stuff, I know its really powerful, but I want to start simple?
- Retrieve data in a background job is really cool, but what if we just want to load data and go?
My first simple try after a lot of testing and tweaking is the following:
Some names and ages
- New-ListView -Show -DataBinding @{
- ItemsSource = New-Binding -Path Output
- } -View {
- New-GridView -Columns {
- New-GridViewColumn "Name"
- New-GridViewColumn "Age"
- }
- } -DataContext {
- Get-PowerShellDataSource -Script {
- $list = @()
- $list += New-Object Object |
- Add-Member NoteProperty Name "Serge" -passthru |
- Add-member NoteProperty Age "43" -passthru
- $list += New-Object Object |
- Add-Member NoteProperty Name "Dinah" -passthru |
- Add-member NoteProperty Age "42" -passthru
- $list += New-Object Object |
- Add-Member NoteProperty Name "Scott" -passthru |
- Add-member NoteProperty Age "8" -passthru
- $list += New-Object Object |
- Add-Member NoteProperty Name "Dean" -passthru |
- Add-member NoteProperty Age "4" -passthru
- $list += New-Object Object |
- Add-Member NoteProperty Name "Tahne" -passthru |
- Add-member NoteProperty Age "1" -passthru
- $list
- }
- }
I still use binding to the output, and in the datacontext script block I write the elements to bind to to the output. I still don’t bind to pre-calculated data in a variable.
After a lot more testing I came to the following code:
Names and ages from variable
- $list = @()
- $list += New-Object Object |
- Add-Member NoteProperty Name "Serge" -passthru |
- Add-member NoteProperty Age "43" -passthru
- $list += New-Object Object |
- Add-Member NoteProperty Name "Dinah" -passthru |
- Add-member NoteProperty Age "42" -passthru
- $list += New-Object Object |
- Add-Member NoteProperty Name "Scott" -passthru |
- Add-member NoteProperty Age "8" -passthru
- $list += New-Object Object |
- Add-Member NoteProperty Name "Dean" -passthru |
- Add-member NoteProperty Age "4" -passthru
- $list += New-Object Object |
- Add-Member NoteProperty Name "Tahne" -passthru |
- Add-member NoteProperty Age "1" -passthru
-
- New-ListView -Name mylistview -Show -View {
- New-GridView -Columns {
- New-GridViewColumn "Name"
- New-GridViewColumn "Age"
- }
- } -On_Loaded {
- $mylistview = $window | Get-ChildControl mylistview
- $mylistview.ItemsSource = $list
- }
In the above code I create a variable with a list of objects with two properties, Name and Age, and bind the ItemsSource property of the ListView to this variable.
I bind the data in the Loaded event, the complete control tree is in place when this event fires.
I have named the ListView control ‘mylistview’, and with the code in line 24 I can find the control by name. The $window variable points to the implicitly created Window control surrounding the ListView, and is always available.
Note that if we add the –AsJob parameter to the New-ListView command, the creation and binding is done in a background job on another thread, and the $list variable is not visible, not even if it is defined as a global variable (as $global:list)
Diving into this simplification kept me busy for a while and gave me as a WPF nono some insights in how things are working in WPF when doing this from PowerShell.
One question I still have in this simple example: is there an easier way to fill the $list variable so its still useful for databinding.I tried $list = @{ Name="Serge"; Age="43" }, @{ Name="Dinah"; Age="42" } but that does not work:-(
Let me know if this is of any help to you.
Happy coding!
My first article on using WPF from PowerShell. What is WPK? Were can you get it? How to get started?
I have been programming in PowerShell since 2006. At Macaw we use PowerShell for most of the development on the Macaw Solutions Factory. I have written thousands and thousands of lines of code in PowerShell 1.0. Some of the GUI tools in the Macaw Solutions Factory are even written completely in PowerShell. We use PrimalForms for the generation of the PowerShell code to render the GUI.
PrimalForms: PrimalForms Community Edition is a free GUI builder tool for PowerShell users. It edits and stores Windows Forms in a native XML format and generates PowerShell code on demand. Sample forms included.
More complex GUI tools are written in standard C#/WinForms. I prefer to have the tools in the Macaw Solutions Factory to be written completely in PowerShell. The reason is that most innovations to the Macaw Solutions Factory are done in real world projects. Because the Macaw Solutions Factory almost completely consists of script code, it is possible to add new features on any development machine that checked out the Factory code together with the source code of the project. No special development environment is needed. Good innovations are merged into the trunk of the Factory. Also fixing issues or making project specific modifications is a breeze.
Writing WinForms applications using PowerShell never really worked well for us. Writing WinForms applications without designer is just terrible. PrimalForms makes life better, but still…
Enter WPF! I have been playing with WPF and PowerShell a few years ago. Problem was that PowerShell had to be executed in a Single Threaded Apartment (STA) instead of the default Multi Threaded Apartment (MTA). My first discussion on this with Bruce Payette never resulted into a good working solution.
A few days ago I ran across an interesting project at CodePlex: PowerBoots. This tooling provides WPF from both PowerShell 1.0 and PowerShell 2.0. I did some tests with it, and had some trouble, partly due to complete lack of knowledge of WPF. While searching the web I also stumbled upon WPK, the Windows Presentation Foundation PowerShell Kit. It is part of the just released Windows 7 Resource Kit PowerShell Pack (most things work on any OS with PowerShell 2.0 installed). WPK takes a very similar approach as PowerBoots. Check them both out!
It is Christmas time. This means: two weeks no real company work, a bit of free time to dive into something new. I have been running around the last three days in PowerBoots and WPK, and I must say: the demo’s look great and most of them work, but even the most simple baby steps completely fail on me, especially due to my complete ignorance of what happened in the WPF space for the last years. Yes, I am ashamed of myself. Time to catch up… baby steps at a time. There are actually two things to dive into: the new features of PowerShell 2.0 and WPK. So don’t expect much of the next posts, it is all really basic stuff, but I see on my blog that the baby step posts are the most popular posts. Posts like how to call a PowerShell function with arguments (fn –arg1 a –arg2 b instead of fn(a,b)). So expect some post at this level… is just write down the things I go through myself.
To get yourself started on the possibilities of WPK, have a look at the WPK video’s available on Channel 9. James Brundage, part of the Microsoft PowerShell Test team, does a good job on explaining WPK. There are a few videos there now, with more to come. For questions have a look at the discussion thread on the PowerShellPack site.
Happy WPK’ing..
When I was at the SharePoint Conference 2009 in Vegas I was sitting in the hallway working on my little white Mac Book writing a blog post on SharePoint 2010 when a guy passed by. “Can I ask you some questions?” “Sure”, I said. “If I did anything with SharePoint?” he asked me… Ok, sitting with a Mac on a Microsoft conference can be strange, but hey: VMware Fusion did let me run the Technical Preview of SharePoint 2010 on my little Mac Book with 4GB, which couldn’t be said of my Dell running Windows XP at the time, not supporting 64 bits virtualization with Microsoft tools. We talked for a few minutes, he made some audio recordings, and off he was.
It resulted in a nice article in InfoWorld with some quotes by “van den Oever”. Never knew I said such smart things;-)
Read it at http://www.infoworld.com/d/developer-world/why-developers-sharepoint-2010-224
And…. when you want the just release public beta of SharePoint 2010, download it:
This link is provided by Microsoft The Netherlands to a group of people called the “Wave 14” ambassadors. We have a small competition between the ambassadors: the one who gets the most clicks gets an XBox!! So help me out, click it… often! And I will make sure that I blog a lot about SharePoint 2010!
An old Dutch phrase… translated into bad English! But it is going to happen: the first public beta of the Office tools… including: SharePoint 2010!
And where can you download it… I know it… download it
This link is provided by Microsoft The Netherlands to a group of people called the “Wave 14” ambassadors. We have a small competition between the ambassadors: the one who gets the most clicks gets an XBox!! So help me out, click it… often! And I will make sure that I blog a lot about SharePoint 2010!
Notes from the SharePoint Conference 2009 session "Introduction to Service Applications and Topology". This is my personal interpretation of what has been said during the presentation. Don't shoot me if I interpreted something wrong:-)
In SharePoint 2010 Shared Service Providers (SSP's) are replaced by Service Applications. Services are no longer combined into a SSP. Services are running independent as a service application.
So in MOSS 2007:
SSP: combines services like Search, Excel Services, User Profiles, ... into a shared service provider.
In SharePoint 2010:
Service Applications: services like Search, Managed Meta Data, .., your service (20 services in SharePoint Server) are running "unboxed" and independent.
So SharePoint 2010 provides a la carte unboxed services. You can configure which services are running on an application server. Per web application you can configure which services are consumed.
When migrating MOSS 2007 to SharePoint 2010 SSPs will upgrade into Service Applications.
SharePoint Foundation 2010 (WSS 4.0) provides the SharePoint Service Application Framework.
New products like Office Web Apps, Project Server, Gemini (PowerPivot) use this application framework, and this platform can also be used by third parties or you to create custom services.
You can plug your management UI for your service into the Service Management page.
A web application does not communicate directly to a service application, but does this through a proxy:
Web Application <-> Service Application Proxy <-> Service Application
So a general workflow can be:
Browser -> Web Front End ->(Request) Application Server ->(Result) Web Front End -> Browser
SharePoint 2010 does contain a fault tolerant round-robin software load balancer with support for hardware load balancing, so it is possible to have multiple application servers.
The Service Application infrastructure provides application isolation: each service application can use separate databases if needed and optionally run in separate app pool. There is support for multiple service apps for a service with different accounts and databases ==> Great for multi-tenancy (hosting for multiple customers on same platform)
Services are flexible, secure and provide cross-farm federation:
- Trust based security between farms, claims based authorization within the farm
- Share to anyone, consume from anywhere
- WCF based web services for communication
- No direct DB Access
For example: Taxonomy, has cross farm federation. Probably same for content types?
Administration:
You can manage which services are running on a server.
In Central Administration UI: list of services, indented under a service you see the proxy.
Through the wizards you get database names with guids at the end. Better to create manually form Central Administration, or create services through PowerShell.
Per web application you can configure which services apps you want to be available. By default all web applications use all service applications available. You can change this into a custom configuration. Use the Manage Service Associations page for this.
Service applications can be published to make them available outside the current farm. It allows you to select the connection type, for example https or net.tcp. Note that there must be a trust relationship with the farm that wants to consume your service. The service is published on a url. Through this url an other farm can find the published services. Url is in the following format: https://myfarm/Topology/topology.svc
The other farm can connect to your farm through a remote service connection.
Although manual adminstration and configuration of SharePoint 2010 can be done through Central Admin, the future of SharePoint administration is PowerShell.
With respect to Services:
Get-SPServiceApplication returns the set of service applications.
Do
Get-SPServiceApplication-name yourservice to get the service object. Do
Get-SPServiceApplication -name yourservice | fl to see all properties of the service object.
There are almost a hundred Cmdlets to manage your services.
Side note: It now really becomes time that all administrators learn PowerShell. In my company (Macaw) we use PowerShell extensively for our Macaw Solutions Factory. Everything from configuration, build and deploy through DTAP is done with PowerShell.
It is possible to delegate management of a particular service to someone, that person then has only access to that the management UI in Central Administration for that particular service.
Access security: specified claims principals have access to a service application. By default the "farm claim" has access, but this can be removed ad more detailed claims can be configured for more granular access rights, or example read versus read-write.
Service applications can spawn their own timer jobs.
Generally ISV's will build service applications on the SharePoint Service Application Framework, but for large organizations it could be interesting for SI's to create services to specialized functionality and farm-to-farm fedaration .
For repeatable configuration over your DTAP configuration, use PowerShell to create and manage the services.
You can create complex farm configurations where farms can share service applications. For example: two farms can share the user profile service.
Never knew this: you can have multiple startup projects in a solution. Was there already in Visual Studio 2005!
See http://msdn.microsoft.com/en-us/library/ms165413%28VS.80%29.aspx for more info.
Note: this blog post is based on experiences with the SharePoint 2010 Technical Preview version.
SharePoint 2010 now extends the object model to the client. A remote object model proxy is available for C# development (including Silverlight) and a Javascript client library which can be found at C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\SP.js, accessible at /_layouts/SP.js.
I tried to understand what happens in the Javascript code, did some document formatting on it to get it readable. But not really a Javascript wizard myself I didn't really got the hang on it. But when I scrolled to the end of the SP.js file I found the following lines:
// ---- Do not remove this footer ----
// Generated using Script# v0.5.0.0 (http://projects.nikhilk.net)
// -----------------------------------
Now I understand why some of the code is not that readable: it is generated code. Script# is used for creating the client side object model API!
Have a look at http://projects.nikhilk.net/ScriptSharp for more info on Script#.
I never dared to use Script# i a real project going into production, especially because the last version came out in August 2008. But Microsoft does not seem to have a problem with it. The Microsoft team is even running an older version that available for download (version 0.5.1.0).
As far as I know the Office Web Applications (online Word, Access and PowerPoint) are written with Script# as well. See http://www.nikhilk.net/ScriptSharp-Large-Projects.aspx. So maybe it is time now to really dive into Script#! Anyone dare to it for production code in their projects already?
Disclaimer: All information in this blog post is based on my personal interpretation of information collected at the SharePoint Conference 2009 and experiences with SharePoint 2010 Technical Preview version provided to my company in the PEP program.
Note: this blog post is based onexperiences with the SharePoint 2010 Technical Preview version.
In the good old days of SharePoint 2003 and 2007 it was possible to save a site as a template. These sites were saved as .stp files, I assume this acronym stands for SiteTemPlate, a non-documented closed format that did not allow for modification in the saved template files. so new sites could be created based on the template. SharePoint 2010 promises the possibility to save a site as a WSP package, the Windows SharePoint Services Package format that we all love in the development of our SharePoint solutions, because it promises seamless deployments through the farm.
In this series of blog posts I will investigate the power of this new functionality, and take you, the reader, along the way in trying to answer the following questions that directly pop up into my mind:
- Is the site really exported as a WSP? And how does it look like at the inside?
- If we create a new site based on the template, do changes to content types at the site collection level propagate to the content types in the new instance of the site template?
- In Moss2007 it was not possible to export a publishing site as a site template. Well, actually you could, but it was not supported. Probably because pages and content a site publishing site depends on, like master pages, pages layouts, the style library and site collection images are managed at the site collection level (in the root site of the site collection). Did this change in 2010, and how is it handled?
- What is exported. The complete configuration of the site, or only changes to the site with respect to the initial site definition?
- Can we learn some new stuff on authoring WSP’s from the generated WSP’s?
- Visual Studio SharePoint Support has a project type “Import SharePoint Solution Package”, what does that do? Can we use the WSP generated by a saved site template?
Ok, let get started. The first steps to execute are:
- Create a site based on the blank site definition
- Export the site
To showcase some of the new tools in the mean time I will use SharePoint Designer to create our new site:
- Connect to the portal, and select the Subsites tab
- Create a new site named wspexport based on the Blank Site template
- This brings us a blank site:
To inspect some of the export functionality we create a custom list MyList with a Title and Description field, and a document library MyDocuments. We put some entries in the custom list and add a document to the document library. I assume that everyone knowing something about SharePoint knows how to do this.
Adding a simple Dummy.txt document to the document library:
The home page after adding list view web parts for the MyDocuments and MyList:
- We go back to SharePoint Designer, set the site Title and Description of the site and save as template
- Selecting Save as template brings you to the web site where you can specify the template site settings
When save as template is done we get to the following screen:
- Following the user solution gallery will bring us to the Solution Gallery. This is a location where solutions can be uploaded and downloaded. These solutions can probably be solutions that can include code that will be run in a sandbox. More on this in an upcomming blog post.
- Right-click on the WspExportSite and select Save Target As… to save the WSP file to your location of choice.
- Note that the saved solution can be activated by selecting the arrow next to its name
This concludes the first post in this series. What do we have:
- A WSP file on disk based on Blank Site containing a list and a document library
- A solution in our solution gallery ready to be activated
Disclaimer: All information in this blog post is based on my personal interpretation of information collected at the SharePoint Conference 2009 and experiences with SharePoint 2010 Technical Preview version provided to my company in the PEP program.
Note: this post is only relevant for people running the SharePoint 2010 Technology Preview.
When I create a new site based on the Publishing Portal template you get a .../Pages/Default.aspx page with an error on it. The error seems to be generated by a ContentByQuery web part (the only web part) on the page. Add ?contents=1 to the url (…/Pages/Default.aspx?contents=1):
Check Out the page, remove the web part (Delete, not Close), and your page starts working again.
Happy Publishing!

Disclaimer: All information in this blog post is based on my personal interpretation of information collected at the SharePoint Conference 2009 and experiences with SharePoint 2010 Technical Preview version provided to my company in the PEP program.
More Posts
Next page »