Learn about Windows PowerShell
Summary: Learn how to use Windows PowerShell to download files from the Internet and other cool tasks in today’s Quick-Hits Friday.
In this Post:
Hey, Scripting Guy! The GroupPolicy module seems to lack complete documentation. Even on MSDN, the following command seems to be the best example:
new-gpo -name TestGPO | new-gplink -target "ou=marketing,dc=contoso,dc=com" | set-gppermissions -permissionlevel gpoedit -targetname "Marketing Admins" -targettype group
This is all fine but how do I set actual GPO settings now? The only description I found is related to Registry Preference settings, but I want to set Computer|Administrative Templates etc. etc. settings.
How do I do this?
Hello HM, Microsoft Scripting Guy Ed Wilson here. The thing about the Windows PowerShell Group Policy cmdlets that are included with Windows Server 2008 R2 is that they are not designed to accomplish what you are seeking to do. They will accomplish the following tasks:
This is why I wrote a series of Hey, Scripting Guy! Blog posts about using Group Policy and Windows PowerShell. I did a week’s worth of articles specifically about using the Group Policy module. The other articles in the tag cloud are about other aspects of Group Policy.
The short answer is that you can create a new GPO, and you can backup GPOs. You can create Registry Preference settings, but to work with the templates, you will need to use a third-party utility. When I am working with Group Policy, I love to have the Group Policy Settings Reference for Windows and Windows Server Microsoft Excel spreadsheet open because it gives me a great idea of what the settings are and where they are located.
Hey, Scripting Guy! Is it possible to get Windows PowerShell to log in to an https site and download/upload files?
Yes it is. The System.Net.WebClient .NET Framework class can be used to log in to a website using SSL and to download or upload files. One example of such a script is in the Script Center Script Repository. The System.Net.WebClient .NET Framework class has been highlighted in several Hey, Scripting Guy! posts, including some that talk about working with invalid certificates associated with a particular website. I would recommend you peruse those articles for additional information. By the way, James Brundage wrote the script that I pointed you to, and he discusses the technique that he uses in a series of articles on splatting (a very important concept).
Hey, Scripting Guy! I just wanted to drop you a line to say thank you for your presentation of Windows PowerShell best practices yesterday to the UK PowerShell Users Group. I listened with interest and found it very useful. I also went straight off and downloaded a copy of PrimalScript 2009 Community Edition which has helped to solve a conundrum for me in a script that I am creating.
Afterward, I was thinking about what was said and I thought that a posting talking about best practices would be very good, starting at the basics of when to and when to not use aliases and how to lay out code properly. You touched on comments, but I was thinking tabulation, brackets, and braces, too. I personally am a stickler for tabulated code, but never know what to do with the braces. I have limited C++ background.
Hello MB, I have written quite a few articles about Windows PowerShell best practices. Many of them have been excerpted from my book on Windows PowerShell 2.0 Best Practices.
As for tabulation, my recommendation is that if you want to tab and align your code that is great – it does look very nice when it is laid out that way. In fact, it makes the code easier to read. My number one best practice is that Windows PowerShell code should be easy to read. I would suggest, however, that instead of using five spaces for a tab, you shorten it to something like three. In addition, make sure that your editor uses spaces for a tab, and not the actual tab (`t) character itself (ASCII 9). The reason for this is that some publishing mechanisms do not interpret the ASCII tab properly (at least that was my experience with one book publisher; I had to go back and reformat all my scripts, which was a major pain for a 400-page book). I know that some editors have an option called “use spaces for tabs” and they will allow you to specify how many spaces to use when you press Tab.
As for braces (curly brackets), I have standardized on leaving the opening brace at the end of the first line of code, and leaving the closing brace at the end of the last line of code. It seems to be more compact, and appears to be pretty much the community standard. On a For statement, I will open and close the braces on their own lines. In addition, I will add a comment to the final brace that indicates it is the closing one. This is shown in the following image.
This makes finding missing braces much easier. However, I almost never have missing braces because of the way I write code. I open a brace, and then immediately press Enter twice, and I close the brace. I then press the up arrow twice, and begin to type my code.
Hope this helps, and I am glad you enjoyed my presentation. Look forward to speaking to the UK PowerShell Users Group again in the future.
Hey, Scripting Guy! How do I write a VBScript script that sets SACLs and DACLs on registry keys, such as HKLM\SYSTEM?
Hello MH, The easiest way to do this is to write a VBScript script that calls icacls. I have written several Hey, Scripting Guy! Blog posts that talk about using icacls with Windows PowerShell. To use icacls with VBScript, you will need to write a VBScript script that runs an executable. This Hey, Scripting Guy! post might get you started with calling external executables from VBScript.
Hey, Scripting Guy! One of the most important scripts that I have written fetches the *** ADS (in PDF format), downloads them to a network drive, and then finally emails a list of people of the new ADS. This was one of my first Windows PowerShell scripts and can be found on the Script Repository. As the PDFs are downloaded, it can take some time. What I would like to do is determine how large the file to be downloaded is and then display a percentage as the data is downloaded. Is there a way that I can do this? Would I need to spin off another thread in order to accomplish this?
A person after my own heart! I love computer hardware stores. It is indeed possible to do what you want to do. Unfortunately, it will require a bit of a rewrite to your script. You will need to use System.Net.HttpWebRequest instead of System.Net.WebClient. The GetResponse method returns an instance of the HttpWebResponse class that contains a contentLength property that will do what you need.
Well, this concludes another edition of Quick-Hits Friday. Join me tomorrow for the Weekend Scripter as I delve into the mysteries of creating a function to automate exporting history.
I would love you to follow me on Twitter and Facebook. If you have any questions, send email to me at firstname.lastname@example.org, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy
utterly lacking in content. How DO you use weblient to download files? Nothing on this page gives a cliue.