Sunday, May 3, 2009

How to run windows powershell scripts

Running Powershell Scripts on local machine is so easy. You just need to change ExecutionPolicy in the Powershell. Powershell Execution Policies are used to prevent unauthorized scripts from running. They provide security for the scripting environment.

In Unix-like machines, we need to set the executable bit for shell scripts to run them (chmod +x <scriptname>)

Windows Powershell Execution Policies


Restricted
- Default execution policy.
- Permits individual commands, but scripts cannot
run.

AllSigned
- Scripts can run.
- Requires a digital signature from a trusted
publisher on all scripts and configuration files,
including scripts that you write on the local
computer.
- Prompts you before running scripts from trusted
publishers.
- Risks running signed, but malicious, scripts.

RemoteSigned
- Scripts can run.
- Requires a digital signature from a trusted
publisher on scripts and configuration files that
are downloaded from the Internet (including
e-mail and instant messaging programs).
- Does not require digital signatures on scripts run
from the local computer.
- Does not prompt you before running scripts from
trusted publishers.
- Risks running signed, but malicious, scripts.

Unrestricted
- Unsigned scripts can run.
- Scripts and configuration files that are downloaded
from the Internet (including Microsoft Outlook,
Outlook Express and Windows Messenger) run after
warning you that the file originated on the Internet.
- Risks running malicious scripts.

For us to learn, we will use the ExecutionPolicy "RemoteSigned" to run Powershell scripts without digital signatures on the local machine.

I have written a script to display first 5 running services. Let's see how to run that script (List_5_Running_Services.ps1)


PS C:\Users\Jagadish\Documents\Scripts> Get-ExecutionPolicy
Restricted

PS C:\Users\Jagadish\Documents\Scripts> Get-Content List_5_Running_Services.ps1
Get-Service | Where-Object { $_.Status -eq "Running" } | Select-Object -First 5

PS C:\Users\Jagadish\Documents\Scripts> .\List_5_Running_Services.ps1
File C:\Users\Jagadish\Documents\Scripts\List_5_Running_Services.ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see "get-help about_signing" for more details.
At line:1 char:14
+ .\Testing.ps1 <<<<
+ CategoryInfo : NotSpecified: (:) [], PSSecurityException
+ FullyQualifiedErrorId : RuntimeException

PS C:\Users\Jagadish\Documents\Scripts>

As you can see in the above code, the current ExecutionPolicy is "Restricted". "Restricted" ExecutionPolicy is the most secure policy, and is the default. It permits individual commands, but does not permit scripts to run.

So to run our scripts without digital signatures, we need to change it to "RemoteSigned" Policy.


PS C:\Users\Jagadish\Documents\Scripts> Get-ExecutionPolicy
Restricted
PS C:\Users\Jagadish\Documents\Scripts> Set-ExecutionPolicy RemoteSigned
Set-ExecutionPolicy : Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' is denied.
At line:1 char:20
+ Set-ExecutionPolicy <<<< RemoteSigned
+ CategoryInfo : NotSpecified: (:) [Set-ExecutionPolicy], UnauthorizedAccessException
+ FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand

PS C:\Users\Jagadish\Documents\Scripts>

If you get the above error while changing ExecutionPolicy to RemoteSigned, then probably you are running the Windows Powershell without Administrator Privileges. You can easily start Windows Powershell with Administrator Privilege just by right-clicking on the Powershell icon and selecting "Run as Administrator" as shown in below picture

Users running Windows XP or Windows Server 2003, may not get the above error as most users login with administrative privileges. In Windows Vista/7, this changed with the advent of User Account Control. Unless explictly requested, processes started by an Administrator account will run as a standard user and not Administrator.



After starting Windows Powershell with Administrator Privileges, lets try changing the ExecutionPolicy and running the script


PS C:\Users\Jagadish\Documents\Scripts> Get-ExecutionPolicy
Restricted
PS C:\Users\Jagadish\Documents\Scripts> Set-ExecutionPolicy RemoteSigned
PS C:\Users\Jagadish\Documents\Scripts> Get-ExecutionPolicy
RemoteSigned

PS C:\Users\Jagadish\Documents\Scripts> .\List_5_Running_Services.ps1

Status Name DisplayName
------ ---- -----------
Running AntiVirSchedule... Avira AntiVir Scheduler
Running AntiVirService Avira AntiVir Guard
Running Appinfo Application Information
Running AudioEndpointBu... Windows Audio Endpoint Builder
Running Audiosrv Windows Audio

PS C:\Users\Jagadish\Documents\Scripts>


More details can be found in http://www.microsoft.com/technet/scriptcenter/topics/winpsh/manual/run.mspx

Let me know if you have any questions or comments please

30 comments:

  1. It sounds to be quite effective scripts to prevent unauthorized scripts from running. It looks like digital signature plays major role for preventing it from any kind of unsecure or you can say unauthorized scripts.

    ReplyDelete
  2. It solved my problem! Thank you very much!

    ReplyDelete
  3. Thank you for your post. This is excellent information.
    It is amazing and wonderful to visit your site
    goldenslot
    gclub casino

    ReplyDelete
  4. Latest Online Betting Games
    รูบี้888 Is a g club website Cambodia's leading casino. Another fun way to play and enjoy online gaming experience, new online gambling offers gamers from all over the world to play online gambling games on the web. Online casinos with no caveats and restrictions, since it is now playing online gambling games, is one of the new systems that make online gambling games fun and interesting. And the service system that is fashionable makes all the online gambling games on the gclub website. The casino is even more interesting. And nowadays, the technological advances make online gambling games all the more accessible and easy to play, the fun and the challenge that the players can access and choose to play according to their satisfaction. This player can join the online gambling experience with hundreds of online gambling games on the site.บาคาร่าออนไลน์

    ReplyDelete

  5. คาสิโน A new gambling game site that is fun for everyone who joins the game on the new channel. Gambling turns into a simple play. The most popular activity that makes people look at modern gambling is the soothing comfort of so-called monotonous play. Look for opportunities to play fun games. Online gambling The game is easy to play, such as Baccarat, Roulette, Slots, Hi Lo Ruby, etc. The online casino site has more fun games to play. Popular Games And for over 10 years, good activities have been developed for all of you to participate in modern games. Popular activities that meet online gambling. Changing the story is easy to play. Gambling itself is fun. Casino site You do not incur loss. Have fun and enjoy the free bonuses. Do not hesitate to join in the fun of playing the best match. Make fun of a simple play style. Generate additional income in new ways through the website.Gclub

    ReplyDelete
  6. Royal1688 casino Online
    จีคลับThe new trend of gamblers is very agreeable and well-informed. In the internet gambling industry It is a front-line site in Thailand that has international standards and hello. Because of our web site Royal1688 casino, our computer network is guaranteed to be open to the use of the online games of freedom under the provisions of Cambodia and live broadcast by the signal. Satellite from gambling casinos in Poipet Cambodia It makes the gambler to gamble gambling games in the field. And that's more than that. You will also be able to play live through the site with another person who is a real person. Play live, ever called you to invite your friends to compete in the game quite entertaining. Our website also has a welcome bonus for new players who apply for team membership with us when you open our shop. Minimum of 500 baht or more. Get instant 25% 25% up to 25% Do not wait to come to us. We are ready to serve 24 hours a day at the website. G club

    ReplyDelete

  7. Royal1688 Online Gambling Betting Sites Make fun of online gambling with us and enjoy the pleasure of playing online casino games. Online Gambling Games Online Gambling is an online gambling game that allows players to choose their favorite game. Make happy every time you join online bets via the mobile screen. The easier and more convenient way. It's not like you have to travel far. To join the big casino, but nowadays, online gambling. Advances in online media increasingly. Modern technology Fast service around It is a great opportunity to enjoy the privilege of playing online casino games. According to the needs of online gamblers, we have played really well. To play casino games. Join a game of over 30 million gambling games at home. Earn extra income for real fun always. We are delighted to offer every opportunity for popular gambling today through simple play channels. Make happy every day through the web.Goldclub Slot

    ReplyDelete
  8. คาสิโนออนไลน์ Online Gambling Games Site Create the enjoyment of online gambling that is played with us forever, create a happy and open service to play casino games online often. Online Gambling Games Online Gambling Games is an online gambling game where you can choose to play according to your aptitude. Make every effort to participate in online betting on the mobile phone screen. The easier and more convenient way. It's not like you have to travel far. To join the big casino, but nowadays, online gambling. Advances in online media increasingly. Advanced technology Fast service around Allegedly a great opportunity to join the fun and enjoy the privilege of playing online casino games. According to the needs of online gamblers, we have played really well. To play casino games. Join the gamble game over 30 million baht to play at home. Earn extra money for real enjoyment. We are delighted to offer every chance to win the most popular gambling today through simple play-picking. Comfortable every day through the web. Ruby888

    ReplyDelete
  9. IBCbet Online Betting Sites Free life is easy to play. No more time and trouble traveling. Travel costs us so much money. And also have to spend days. Nowadays, there are new online betting developments. By online betting Online bets are available. Everyone is more interested in betting and betting. Get ready to get into the betting experience and find the best bet on boundless betting. The life of independent bets, the joys that advance the modern society. And get special discount on playing games that enjoy online gaming moods that make good things for ourselves. With great promotions Special offer on various bets. Online Gambling Games, Advanced Social Entertainment The answer to the problem of luck. Interested in touching the fun of the industry. A better way to live Games that create happiness every day. Every aspect of the new play. Have fun with us at the web. Sbobet

    ReplyDelete
  10. Free Online Betting Sites
    รอยัล1688Our online casino site. Is a site that provides online gambling services. The most comprehensive and most popular today. This will make the player, whether a gambler or a gambler who is fun to play wherever you are, wherever you are, you can access unlimited online casinos. In addition, our online casino site also has various forms of play. Developed from a real casino abroad. By simulating it on our online casino site developed for you, you do not have to spend time traveling far. And games with a variety of games. Variety of choices Everytime, anywhere You can choose to play as you like. By the way, you can set a custom play all. Which you decide on in each game itself. To win each game and take home money back home. And if you plan to play games well. Not only will you enjoy the leisure time. You will also receive a large reward. And more special If your play has a problem, we have a 24 hour support staff that you do not need to worry about. And our staff will help you comfortably. You can enjoy our online casino and relax with the style of online gambling. Every time you click on us at the web. viva3388

    ReplyDelete
  11. Royal1688 Online Gambling Gambling Sites The more luck you play, the easier it is to play. By choosing to play in online format. The open activity for everyone to join the game easily. Ready to enjoy all ages. Open service is friendly. With a new enjoyment. freedom life The gambling game is ready to serve as intimate. There are new games that everyone can choose to play at home. Only subscribers can join online gambling with us. Worth the enjoyment Go to activities that go far. Open a good service to get rich first. The game meets the way forward happiness. People have a lot of deliberation. Different gambling techniques. Along with the great gambling techniques to gamble frequently. The way to leading gambling. An alternative to playing free games. Betting on new fun channels to choose from. We have more than 30 million baht worth of prizes for those who are interested in our games. In the pursuit of pleasure, the web is really open.Goldclub Slot

    ReplyDelete


  12. IBCbet Online Betting Sites Welcome to every day fun. Modern game resources Gambling became a popular game continues. Get ready for all the moments of entertainment that lead to a bright future. Web Casino is a fun game that brings complete fun to the path of gambling, ready to answer every moment of fun that anyone. I choose to play all the time. Playing gambling games At all times, enjoy the modern fun. Gambling that delivers fun throughout the day. Make you enjoy the new social opportunity of full game play. Start a fun and endless game online. Easy gambling Top Gambling Games Choose the most fun, exciting moment. In addition We also have a great offer. Called a game that is easy to understand. Just by playing on your mobile device, tap the laptop, you can access the online gambling immediately, we care about who you want to choose a simple online today. Sbobet

    ReplyDelete
  13. The original bets are universal.
    Ruby888It is a site that is open to the problem of playing the game through the computer network. The instant excellent service. You will find a variety of risky games. And the games are broadcast live. Directly to your residence every day, such as casino games that are popular in modern times, slots, roulette, poker, slots, slots, online. And many more that you will play baccarat online. And other games can be delivered directly to the residence. Or wherever you go, you can play games anywhere. With a desirable design for everyone. Make it easy to use and let you enjoy every minute. Get easy money, minutes per minute. Ensure a steady, steady eye to fascinate us with a distinctly. Join us for fun with our online risk website today. Get paid immediately. Dividend to the new team 30% of the minimum registration open from 500 baht up to 15000 baht. This excellent choice is not a regular, hurry to register with us at. Website IBCbet

    ReplyDelete
  14. Online Casino Games Make you rich
    รอยัล1688One of the most popular online casino games of the industry. Nowadays, casinos are one of the popular and well known. Today, many gamblers love to play fast, so playing fast online casino games is another good way to play online gambling games in each. This is an interesting and important form today. Online casino games in each format can also be played both domestically and internationally, with the Internet as a medium for connecting the enjoyment. The style, excitement and challenge make online gambling a very popular in Asia. Any favorite passion for gambling and betting able to attend a joy to bet online. It is not difficult because of the gamble, online gambling is based on the Internet and mobile 1. Only Just you have these two things. Bets can be played on any online casino games that players like the webviva3388

    ReplyDelete
  15. Royal1688 Online Gambling Games Site Life is easy to play. Open to all modern activities. Play at home. The fun of creating new experiences for all players who are interested in fun. We offer a wide variety of free bets to enjoy. Play free online games. Have a modern mind The game gives you all the modern life. The perfect happiness. Opportunities for modern entertainment. Games that make social choices anytime, anywhere. The game is open to all people to participate in online gambling. Fun to share It is a great opportunity to win a lot of money. Find the future of social advancement in Thailand. Gambling is the current era of gambling. To play the game with us. We give you a chance to get to know each other better. Make money every day through the web.Goldclub Slot

    ReplyDelete
  16. Guarantee quality service.
    คาสิโนออนไลน์ It is a website that is open to the service of casino gambling through the Internet. There are also slots games. Live risk The famous and famous in the service. It is also the template of the live gambling group in Asia. Our Royal1688 website offers fair sponsorship from the First Cagayan Leisure and Resort Corporation. Everyone can join in the fun of all the games, live betting, live roulette, blackjack, blackjack, slots, internet games. And more at royal1688 gambling network, internet casino games, online casinos. With the online baccarat. To be able to use the baccarat formula to play the game has been compiled by seasoned gamblers on our site. It makes it possible to beat all the casino games easily. Many online casino promotions. With the new 250% promotion, you can open up to our online betting site at a minimum of 400, up to 8000 baht. Do not wait, this great way is not often to apply. Join us on the site. Gclub Slot

    ReplyDelete
  17. I want to be rich is not difficult. Just play online gambling.
    Ruby888The site is open for online casino games for a long time. One option that a player can play online gambling more easily. With its innovative service system, online gambling games are becoming more and more popular, and not just because of the gambling websites. This casino. The game is also open to live betting. The system that makes playing online games in each of these forms is interesting by playing online casino games of the player that is considered to be very popular, just a gambler. To play online casino games only, players will know that the fun of playing baccarat online. Or how to play slot games online on the web page gambling. Online casino is very pleased. If the player wants to join the joy. The player can participate in the game with our online gambling whenever the player wants. Because our casino gambling club is open 24 hours a day, so the fun of playing casino games online is a never ending fun. Since whenever a gambler is thinking about gambling online, do not forget to come join us because of the online gambling, but the pleasure is waiting for you on the webIBCbet

    ReplyDelete
  18. IBCbet New Age Gambling Website Turn to the online entertainment where everyone is interested in creating fun, open the channels of entertainment that welcome everyone to the path of social fun. An alternative to the exhilarating view of gambling, such as online gambling, the ultimate gaming experience, and the launch of a unique financial channel. Gambling Online There are more than 200 gambling games to win a jackpot of up to 30 million. Sink easy to get promotions, discounts, privileges. A lot of fun to open. Get started on the chance to create a chance for everyone to play. The game is fast approaching. Do not waste time traveling any further, opening a gambling perspective into a bright future. Betting Odds Play the game in the way you want to enjoy the new game to create a happy new gambling. Choose to play in the modern era. Make the most of it. Do you want to play games in the modern era with pleasure on the web.Sbobet

    ReplyDelete
  19. Gambling online via Royal1688
    Holiday Palace It is a website to create thrills and excitement with the game of risk. In the present day, there are websites that provide services in the field of games, risk, network, emerging Internet network to serve the gamblers computer network is a lot. And our betting site, our computer network, is a leading provider of gambling games that have enjoyed widespread fervor for any popular online gambler. Betting on the most popular players such as slot games, baccarat, online gambling, including the website. Online Gambling These gamblers also provide all types of gambling services. Whether it's football, every game, the most popular matches. The most popular sport betting game is playing online. The game is a gambler to make money for many players. Our online gambling website is also considered to be a provider of online gambling games that have gained a lot of trust from online gamers. Because of the game bets here. When a player wins a bet Get paid instantly and get the full amount. There are also promotions for gamblers to enjoy each month as well. And all this you can find at the website. Gclub

    ReplyDelete
  20. To get rich is not difficult just to play online gambling games.
    SbobetIs a web g club The world's leading online casino and casino gambling website. This online casino is another turning point that makes gambling games more popular because of this. Technology has made the game of online gambling in each type of prosperity as well. By playing games online casino games in modern times. Can be played in both mobile and computer. The more online betting games, the more flexibility. Playing online casino games is becoming more popular. The fact that you are playing online gambling games is very simple. Just a bettor. Apply to the casino site. Our only club. The player can play all online gambling games on the casino site. Our club. Gamblers can find online casino games and services. At any time, the player needs the webคาสิโน

    ReplyDelete
  21. คาสิโน Website of modern gambling games. New Alternative to Alternative Gambling Online Casino Games Play a fun game in the game with the opportunity to make a good bet. Start with the chance to meet the perfect life. The online casino is popular online. Developed and forwarded games for better communication. Join the fun and secure around the world is a game that complements the opportunity for many people to bet on the fun of online gambling. Challenge Activities Start from what should be, no matter what kind of gambling game you are. The game is a good choice. The game has a positive feedback to itself. Have fun every day. Interested in the fun of playing the best match. The leading way to open gambling. Win the extra income for all of you as well. Interested in playing today's games. The opportunity for all of you to play a simple online casino games through real money.Gclub

    ReplyDelete



  22. คาสิโนออนไลน์ Online Gambling Games The game is popular in long-standing bets. Most people are interested in online betting because it is a simple game that can be played at home. Or anywhere. You want to bet on your free time. Nowadays, online casino websites have developed better online game systems. There are also many promotions and discounts. Our games cater to the needs of gamblers who participate in gambling. The game is a joy of all ages to earn extra income very well. Life is full of financial opportunities. With the chance to win each other. Get Started To Bet Online Ready to develop an alternative play option. Make the most of your experience. Open up every aspect of gambling in the modern era. The way to gambling is fun. Betting opportunities Win many prizes Welcome everyone with a willingness. We give you the best of everything. In full format Social Gambling Have fun at the web. Ruby888

    ReplyDelete
  23. Entertainment with online betting games.
    จีคลับThe online gambling game is where the player is genuinely enjoying the gambling online with full of joy. Many new online gambling sites, which will make users gambling. The game of online gambling is easy. Ways of Making Money Online There are hundreds of free online gambling games. In today's online gambling games like the gamblers around the world. This is due to the ability to fully enjoy the service and can earn money from the game. The atmosphere of online casino games is more realistic by live casino games online. With sharp images, play without interruption. Gambling, good luck, good luck to the players. Not only that, they can find tips and entertainment with other users. Holiday free online games to meet the needs of users. If you are interested or are looking for a way to make extra money to the players. Online casino games are a way to choose which can meet all your needs. บาคาร่า

    ReplyDelete
  24. We need to play rich online games.
    รอยัล1688The web is open at Poipet. Casinos casinos that risk can play all forms of luck. The Internet only to play. Every time an internet player bets, the player can play all online gambling on the site of this online casino. The online casino site. It's a fun and fun new opportunity for people who are passionate about gambling and gambling. Because of this, online gambling games in each form are constantly being developed. It makes playing online casino games at this time is well-known and popular in it. Whether it is a player. Thais or foreigners are passionate about playing online gambling games as well, because online gambling in each form is both joyful and fluent in playing and important to gambling. Online gambling is also a new safe choice, a player can play all kinds of online casino games through the Internet. Brannon's not a player where he can play and bet online. When thinking about online gambling, remember to think of web services viva3388

    ReplyDelete

  25. คาสิโน Top Game Popular fun bets to choose from. It's fun to have fun at home. Do not waste your time online. Exciting game With the atmosphere of realistic bets. Get a moment of excitement. Make sure you have a good time playing the casino game. For online gambling It is a gambling game that continues to meet the desire. Join us to create value together. Enjoyment Good to play together at home comfortably. Create excitement Every time, through the way of fun. Online gambling games that allow everyone to gamble on mobile. Join fun with excitement. Every time Bet Make money in the modern era. Welcome everyone with a willingness. Enjoy the new gambling. We welcome every aspect of betting. Make money in the modern era. Welcome to the fun, simple and fast money through the web.Gclub

    ReplyDelete
  26. Royal1688 Gambling is a popular game of fun to play with. The entertainment is for everyone to play at home. Do not waste your time online. Exciting game With the atmosphere of realistic bets. Enjoy the moment of mind-blowing entertainment. It's a great opportunity to find the path of joy that challenges every time you play casino. For online gambling It is a gambling game that fulfills the desire to continue. Join us to create value together. Fun model Great to play together at home comfortably. Create excitement Every time a fun walk worthwhile. Online gambling games that allow you to gamble on mobile, join fun with excitement. Every time Bet Make money in the modern era. Welcome everyone with a willingness. Enjoy the new gambling. We welcome every aspect of gambling. Make Money in Modern Era Welcome to simple fun, fast asset web. Goldclub Slot

    ReplyDelete
  27. The site is more entertaining.
    Goldclub Slot Our online casino site. The web is standard in all aspects of the service. The staff is available. Including 24-hour advice, the gambler does not have to be embarrassed to encounter problems at any time. We will always be happy to share with you. Or will be the standard of comprehensive entertainment. Because our website imitates online casinos like real casinos in foreign countries. When you come in. Must be exciting and enjoy the game. There are many. And unlimited time to play. In addition, access is easy. No need to download the program. Just come to our online casino website. The player will enjoy simultaneously without restrictions. It also ensures that. Our online casino is not dangerous. With the recognition of the player, whether it is a professional or professional gambler. Every day, more and more people come to the site. If you are looking for a way Make our website a part of your relaxation. And make money at the same time, you just join the site. คาสิโนออนไลน์

    ReplyDelete
  28. บาคาร่า A fun and entertaining website that will give players the option to play at their own pace. Do not waste time. To bet well There are gamblers to choose from. Enjoy the fun. Online games that everyone choose. Make a good profit every area. Make more money. Gambling can be good at any time.

    In addition, it is interesting to mushrooms. Mushrooms are good in nature. It is widely cooked. A variety of properties, such as vitamin C, and then prevent gum disease. Scurvy Volvatoxin helps prevent the growth of the virus that causes influenza. Helps reduce blood pressure. Nourish Helps digestion, maintenance blood, heal liver, heat in the bruise.

    It is interesting and there are gambling games to play professionally. Can play more. Betting is easy, fun and gives you a great return on gambling that will give you comfort. Realistic gambling always good. Do you want to gamble amateur all the fun. There are gambling games that everyone likes to play all the time. G club

    ReplyDelete