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

35 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. 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
  4. คาสิโนออนไลน์ 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
  5. 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
  6. 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


  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. คาสิโน 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



  15. คาสิโนออนไลน์ 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

  16. คาสิโน 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
  17. 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
  18. 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
  19. บาคาร่า 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
  20. Thanks for posting such amazing post. Well explained. Keep sharing
    Windows PowerShell Admin Training in Delhi

    ReplyDelete
  21. I just like the helpful information you provide in your articles. I will bookmark your blog and take a look at once more here regularly.
    I am somewhat certain I’ll be informed plenty of new stuff right here! Good luck for the following! japanese classes

    ReplyDelete
  22. Hey.. I checked your blog its really useful.. Provides lot of information.. Exploring2gether is a food, travel, health, and fitness, beauty and tips content platform. From delicious food to travel inspiration to living a healthy life, Exploring2gether has got you covered with all these things! Do check my blogs also https://exploring2gether.com/

    ReplyDelete
  23. hey...It is highly comprehensive and elaborated. Thanks for sharing!

    Localebazar- Your single guide for exploring delicious foods, travel diaries and fitness stories.

    Visit us for more- localebazar.com

    ReplyDelete
  24. Very well explained and easy to understand. Thanks for sharing !!!!
    https://exploring2gether.com/shopping-places-in-delhi-that-are-worth-checking-out/

    ReplyDelete
  25. the team via the website 1 person per sbobet มือถือ 1 account with the bet money in the trial account but the

    ReplyDelete

  26. This are new articles style for you. เว็บแทงบอลขั้นต่ำ10 You can find some new idea on this. เว็บแทงบอลขั้นต่ำ10 It might help you to write or think some new idea.
    เว็บแทงบอลขั้นต่ำ10 Thanks for sharing such a wonderful post.
    เว็บแทงบอลขั้นต่ำ10 I am very glad for reading my articles.

    ReplyDelete
  27. This Is Really Useful And Nice Information. เว็บแทงบอลขั้นต่ำ10
    This are such great articles. เว็บแทงบอลขั้นต่ำ10 This articles can help you to make some new ideas.
    เว็บแทงบอลขั้นต่ำ10 I appreciate for reading my blogs.

    ReplyDelete
  28. Good post, and thanks for sharing such an adorable post. I would love to see more articles / blogs. I am also writing about online GST course you can read this article.

    ReplyDelete
  29. Good post, and thanks for sharing such an adorable post. I would love to see more articles / blogs. I am also writing about Personality Development Courses For Kids

    ReplyDelete

  30. The 24kbet app provides users with an exciting and convenient way to enjoy a wide range of gaming experiences right at their fingertips

    ReplyDelete