设为首页 收藏本站
查看: 939|回复: 0

GS1: Three Ways to Execute a PowerShell Command

[复制链接]

尚未签到

发表于 2018-9-3 09:29:34 | 显示全部楼层 |阅读模式
  Here are basic instructions to help you execute a PowerShell command. You have a choice of three strategies:

  • firstly, the time-honoured method of copying other people's examples, and then pasting their code into your PowerShell session.
  • Secondly, creating cmdlets (my favorite).
  • And thirdly, simply typing the instructions at the PowerShell command line.
  Topics - How to Execute a PowerShell Command DSC0000.jpg
  1. Method 1 Copying and Pasting (Easiest)
  This is how to copy and paste PowerShell instructions at the command line.

  • Launch Windows PowerShell
  • Copy all the lines of code into memory
  • Right-click on the PowerShell symbol DSC0001.jpg
  • Edit –> Paste
  • Check the menus on my screenshot to the right
  • Press 'Enter' to execute your pasted code
  'Vehicle' for Executing a PowerShell Command
  Since the aim is to learning a technique, the practice code does not matter. Most people use 'Hello World' as their test 'vehicle'; however, I prefer to choose a real example. Here is an example cmdlet which gets the operating system processes, and then groups them by company name. Incidentally, the code is a work-in-progress where I am trying to output the data to a file called ProcessCompany.txt.
  # PowerShell cmdlet to group Processes by company
  $Path = "C:\DemoPS\ProcessCompany.txt"
  $ProSvc = get-Process |sort company |ft -groupby company
  $ProSvc
  $ProSvc | out-file $Path
  Result: You should see a list of processes grouped by Company name.
  2. Method 2 Cmdlet (Best)

  Because it saves your instructions permanently into a text file, this cmdlet method is an improvement over copy and paste. Creating cmdlets is my favorite technique because it is>

  • Copy the code in the 'Vehicle' above into a text file
  • Save the file with a .ps1 extension, for example: addcontenta.ps1
  • In Powershell, navigate to the folder where you saved addcontenta.ps1
  • Issue this command: .\addcontenta  (dot backslash filename)
  Tip: For each of my PowerShell projects, I launch Windows Explorer and then create a subfolder. Once I have a cmdlet that works, I store it in that subfolder. Thereafter my technique is to call for: File (menu), SaveAs, create a new file. Then I work with the new file and try to improve on that original version. At various points I call for SaveAs again, thus creating a family of cmdlets, for example: addcontenta, addcontentz, addcontenty etc.
  My reason for employing this cmdlet technique is to twofold, to cater for that moment when my code gets into a tangle, and I think: 'If only I could go back in time to when my script WAS working'. Well, thanks to saving lots of intermediary examples, I can revert to a working backup. Secondly, producing cmdlets means that I can return to my research months later and pick up from where I left off, instead of starting the project from scratch.
  You may like to combine methods 1 and 2 by copying other people's code then pasting, not to the command line, but into a cmdlet text file.
  3. Method 3 Type at the command line (Simplest method)
  Because PowerShell commands are so efficient, and thus short, I have no qualms about recommending that you simply type them at the command line. To test method 3, I have different examples or 'Vehicles'. You could start by typing this at the command line: get-childitem c:\windows.
  Indeed, if you are new to PowerShell there is nothing like typing to give you a 'feel' for the syntax. As you type simple commands, so you get into the rhythm of the verb-noun pair. Another bonus of typing is that you understand when to use a dash (-) and when to precede a dash with a space. Here are three examples of what I mean:
  get-eventlog -list (Correct: Space before -list)
  get-help eventlog (Correct: No space before eventlog)
  get-eventlog system | get-member (Correct: No space between each verb-noun)
  get -eventlog -list (Wrong: Space between get and -)
  get-help -eventlog (Wrong: 'Overthink' eventlog is not a parameter or a switch, it is a positional
  argument and thus does not need a dash.
  There are occasions when even experienced PowerShell scriptwriters resort to typing commands. As for me, I love creating cmdlets, but I do type commands especially when I want a list of an object's properties, for example, abc-xyz |gm. (gm is an alias for get-member).
  Summary of How to Execute a PowerShell Command
  Typical Microsoft, there are always at least two ways of executing PowerShell code. By all means start with the time-honoured method of copying and pasting, but for the long term, my advice is take the time to master the cmdlet method.


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-561884-1-1.html 上篇帖子: PowerShell 2010: Basic SharePoint Cmdlets 下篇帖子: GS2: Three Key PowerShell Commands
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表