| Richard's profileRichard Siddaway's BlogPhotosBlogLists | Help |
|
June 30 out-gridviewThe version of PowerShell that ships with Windows 7 RC has fixed out-gridview so that we can display more than 10 columns. try get-process | select * | Out-GridView to test it Technorati Tags: PowerShell,version 2 June 28 AD PowerShell on Windows 2008/2003 - supposedlyAs James pointed out recently the AD Web Service from Windows 2008 R2 has been released for Windows 2008 and Windows 2003 - see http://blogs.msdn.com/adpowershell/archive/2009/06/23/use-active-directory-powershell-to-manage-windows-2003-2008-dcs.aspx for details. Having installed it there doesn’t appear to be any sign of the AD cmdlets or Provider. On this basis it is useless. If anyone knows different please let me know because I’ve looked in all the logical (and illogical) and I can’t find those cmdlets. The Windows 2008 R2 RSAT won’t install so that source doesn’t work. June 25 Microsoft Learning ManagerI went on to the Microsoft site looking for books on SQL Server 2008. What you get is an incomprehensible jumble of stuff. Don’t tell me about everything there is – let me get to the information I want. I used to really like the Microsoft site as there was masses of information that usually logically arranged. Now we get some many layers between us and the information that I’m likely to expire of old age (OK I know its not that long to go) before finding it. Another example – I went looking for the information on the SQL Server exams – the logical route through the site leaves you with the impression there isn’t an upgrade from SQL 2005 MCITP to the 2008 version. Wrong! But oh how you have to search. I’m coming to the conclusion that the only way to access the Microsoft site is to search from outside and hope you can get to what you need. A triumph of style over substance at the moment. Technorati Tags: SQL Server exams SQL Server autocloseBuck Woody blogged about SQL Server autoclose and how it should be set to being off - http://blogs.msdn.com/buckwoody/archive/2009/06/24/sql-server-best-practices-autoclose-should-be-off.aspx I’m doing a lot with SQL Server and PowerShell at the moment and it started me thinking that I could easily set this property in PowerShell.
Create a server object and display the databases, selecting name and the autoclose property. Its boolean so we get true or false back. We can iterate through the database collection. Test if the database is a system database and set autoclose to false (we don’t want autoclose to happen). A call to Alter() saves the change. Finally we can recreate the server object and redisplay the autoclose property. Technorati Tags: PowerShell,SQL Server PowerShell surveyThe PowerShell team want to know how you find PowerShell related material – scripts, training material etc etc http://blogs.msdn.com/powershell/archive/2009/06/24/new-powershell-survey-on-connect.aspx Technorati Tags: PowerShell June 24 foreach usageI’ve noticed the way foreach is being used and it falls into two groups. I am using foreach-object to keep the discussion straight – like everyone else I use the foreach alias – yes I know I rant about aliases but the aliases for the *-object cmdlets make sense.
The first one reads the file and then iterates through the objects while second one reads the file and puts the objects on the pipeline. So whats the difference and is it a big deal? Instinctively I try to use the second but is that best? using foreach we have to read the file and hold all the objects in memory while the foreach-object works with them one at a time as they come down the pipeline – for a big file that makes a lot of difference in resources. However, if we need to access a file multiple times using foreach may be the better option as we only have to read the file once. foreach can be faster which is a trade off we may need to think about inside the loops we can use $thing or $_ to access the object respectively. One point to think about is that if you do further work inside the loop that generates a different $_ eg a switch then reading the code may get confusing especially if you come back to it after a while So is there a best option – probably not as an all time categorical “this is the best so always use it” I tend to prefer the foreach-object as I can get on with processing objects straightaway though we do have to be careful with subsequent order of events sometimes. The other advantage is that foreach-object gives me a –begin, –process and –end script block which makes things even more flexible So no hard and fast answer but as a final thought the foreach-object usage looks more “powershelly” as someone once put it Technorati Tags: PowerShell,foreach AD RecoveryI have always been impressed by by Quest’s AD Recovery Manager and now its even better – yep it has PowerShell cmdlets see http://wiki.powergui.org/index.php/Quest_Recovery_Manager_for_Active_Directory_cmdlets_reference June 23 Scripting Games – Event 6 and 7Event 6 Beginners – find the errors in the script Advanced – read a text file of a network trace and find the delay can’t be bothered. Beginners was done last year plus I really hate scripts that have $objxxx - so VBScript Advanced is yet another variation on reading a file. Event 7 Advanced – write a script to play 21 or blackjack or pontoon or whatever you want to call it. Did that last year see http://richardsiddaway.spaces.live.com/blog/cns!43CFA46A74CF3E96!1161.entry Might have a look at beginners 7 Technorati Tags: PowerShell June 22 PowerShell in Practice Chapter 13Chapter 13 – IIS – is now available on MEAP www.manning.com/siddaway Technorati Tags: PowerShell June 21 Games: Advanced 4Read a file and create a graph of the results. The quickest way to graph the data is to take the height and display a number of “*” depending on the value.
-split is a PowerShell 2 operator - use $_.Split(“,”) in v1 BTW – there are 43 jumps per person not the 30 as stated in the event information. This answers the letter of the event but probably not the spirit. PowerGadgets would be an interesting option for this. PowerGadgets installs straight into Windows 7. I had tested it with PowerShell v2 in the past but nice that it installs on Windows 7. If we want to create individual graphs using PowerGadgets
All we do is pipe numeric data (notice the double split – the first gives the names plus the numbers as one string. We can then split the numbers separately as required) into out-chart using the first two elements from the first split (jumpers name) as the title. Finally lets look at charting in Excel – we have to use the COM object to work with Excel (wouldn’t be nice if we got proper PowerShell support in next version of Office – please - )
We create a spreadsheet and add a work book . Have to use this long winded method as I am using a non-US culture on my machine (Excel bug). We can add the work sheet, add the data and finally add the chart. Chart looks ugly but it is a chart. Finally save the spreadsheet using the same horrible construction. didn’t like this event - tedious Games: Advanced 5In this event we have to pull some information out of the properties of some photos. This information isn’t in the normal properties. We need to load the drawing classes to access the data.
Use –f to create a formatted string as a header. Loop through the photos and create a bitmap object for the photo. The properties are held in a number of formats – they are explained in James’ blog that is commented in the beginning of the script. Luckily the ones we want are strings – but are held as byte arrays. The camera maker and model are simply passed into the function that we use convert to a string. The date we convert and then re-arrange from yyyy-mm-dd to dd/mm/yyyy format. We can then create a formatted script to display the data. Technorati Tags: PowerShell,Scripting Games Games: Beginner 5This one involves setting some registry settings to increase the number of concurrent downloads. I’m running Windows 7 and IE 8 which have already increased the limits from 2 to 6. Tobias has done a really good job on his expert commentary - http://blogs.technet.com/heyscriptingguy/archive/2009/06/18/hey-scripting-guy-event-5-solutions-from-expert-commentators-beginner-and-advanced-the-400-meter-race.aspx - so rather than repeat that I’m going to use this as an example of using transactions in PowerShell v2.
Use Requires to restrict to PowerShell v2 use the registry provider to navigate to the location of the settings and use get-itemproperty to display the values We can then start our transaction. Make the changes and display the properties again. Notice that we have to use the -UseTransaction parameter to involve these cmdlets in the transaction. A Read-Host is used to pause the script. Check the values and we can seen that the properties have been added. Continue the script by pressing any key (if you can find it) and then the transaction will undo. Display the properties again to show that the change has been undone. Transactions are only available on the Registry provider at the moment. Hopefully, they will be extended onto the other providers in the next version of PowerShell. June 20 The incredible shrinking URLSome urls on the web become a real pain to type because of their length. Tinyurl, and other similar services, can be used to shrink the url to a much more manageable length. Joe Pruitt has a PowerShell script to perform the shrinking – find it here http://devcentral.f5.com/weblogs/Joe/archive/2009/06/19/shrink-url-ndash-use-powershell-to-shrink-your-urls.aspx June 18 Games: Beginner 4I did the expert commentary for this one – it can be read here http://blogs.technet.com/heyscriptingguy/archive/2009/06/17/hey-scripting-guy-event-4-solutions-from-expert-commentators-beginner-and-advanced-the-high-jump.aspx Technorati Tags: PowerShell,Scripting Games Games: Advanced 3This one involves reading a text file, finding all the words that use a single vowel (may have multiple instances of that vowel e.g. look) and write them out to a text file.
Delete the answer file if it exists. Read the file & loop through the contents. Quick & dirty brute force approach for this one. Convert the work to a character array and use contains to check for vowel membership – only 5 vowels so 5 if statements. Tried a switch but it counts each each instance so wrecks the result ie didn’t accept “look”. For each hit increment the counter ($y). At the end if $y = 1 it means we have only one vowel in the word so write it to the text file. Not a task I need to do very often but it works. Technorati Tags: PowerShell,Scripting Games Kill the MessengerI’ve mentioned before that I find the way that the latest incarnation of Messenger on Windows 7 keeps starting whenever I access any other Live product. Finally got annoyed enough to make a kill messenger icon. Create a shortcut on your desktop for the PowerShell exe and then change the target string (on Shortcut tab) to "%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe " -NoProfile -Nologo -Command &"{Get-Process msnmsgr | Stop-Process}" This starts up PowerShell without the profile and logo and immediately runs the command Get-Process msnmsgr | Stop-Process which terminates with extreme prejudice any instances of messenger. On the same tab I also set the Run drop down to Minimized so I don’t get the full flash of PowerShell starting and shutting. Messenger: you can start but you can’t run Technorati Tags: PowerShell,Process Virtual ConferenceDon’t forget the TechNet virtual conference tomorrow (19th June). There have been a lot of registrations for this – but we can squeeze a few more in at the back.
June 17 Games: Beginner 3We start with a file and have to put the first paragraph into one file and the second into another file.
Create a flag (set true). Test for the existence of the output files and delete if present Read the file and loop through. We can identify the paragraph break as there is an empty line – when we get to that we change the value of the flag. If the flag is true add the line to the first file otherwise add to the second file.
Technorati Tags: PowerShell,Scripting Games Games: Advanced 2This is playing in a spreadsheet. Need to do some calculations and sorting
Some constants are defined at the beginning and then we open the spreadsheet. Add a header to an extra column we will be using (11) . We can then loop through each row finding the maximum of the scores and putting it into the results column (17) In the same loop we can determine if they are exceeding their season best (21) and calculate a ratio (25) A sort on the results column means we can pick off the winner (31) and then another sorts leaves the spreadsheet ordered by the comparison to the season best. It can be saved like that if required. Interesting mix of PowerShell and Excel but the challenges are in the Excel side and how to get that working in PowerShell rather than PowerShell itself. One possibility would be to open the spreadsheet and create a csv file which would make some the work easier but its probably not worth the effort. Technorati Tags: PowerShell,Scripting Games Games: Beginner 2In this one we need to use WMI to get some information about the Processor in our machine. Main difficulty is finding which class. In PowerShell v2 I can get away with this Get-WmiObject -List "*processor*" whereas in v1 I need to use Get-WmiObject -List | where{$_.Name -like "*processor*"} In either case the Win32_Processor class looks to be the best choice. This leads to
Get the WMI information once and then select the bits we want to meet the criteria Get-WmiObject -Class Win32_Processor | Get-Member will give a full list of the properties. If you want to colour the output use write-host and pick off the properties individually Technorati Tags: PowerShell,Scripting Games |
|
|