Mastering Powershell 学习笔记-2 续

命令的参数

开关参数

当它存在它就代表某个值为true,不存在则为false

比如 -recurse

位置参数

一些参数有固定的位置,另一些参数则有名字。可以使用help来看这些参数的Position属性(不过我用help没看到这些)

-path <string[]>
Specifies a path to one or more locations. Wildcards are
permitted. The default location is the current directory (.).
Required? false
Position? 1
Standard value used <NOTE: if not specified uses
the Current location>
Accept pipeline input? true (ByValue, ByPropertyName)
Accept wildcard characters? true

当一个命令的参数被解析的时候,它会先解析带名字的参数,然后把剩下的参数按照位置赋值。

例如

Get-ChildItem c:\windows *.exe -recurse -name
Get-ChildItem -recurse -name c:\windows *.exe
Get-ChildItem -name c:\windows *.exe -recurse
首先那些带名字的参数会先被解析:-recurse -name, 

然后因为-path的位置是1,而且它还没有被赋值,所以c:\windows会被赋给-path,同理 *.exe会被赋给 -filter

但在实践中,为了可读性,不建议采用参数名缩写,方法别名或以上位置参数。


公共参数

你可以通过get-help get-childitem -detailed 看到公共参数的详细信息

This cmdlet supports the common parameters: -Verbose,
-Debug, -ErrorAction, -ErrorVariable, and -OutVariable.
For more information, type "get-help about_commonparameters".

方法别名

在ps中,很多方法都有别名,有时候是因为历史原因,有时候是为了简化写法,比如Get-ChildItem就有2个别名 Dir 和 ls

$alias:Dir
Get-ChildItem
$alias:ls
Get-ChildItem
你可以使用 Dir alias: 查看所有别名

如果你只想看某一个命令的别名可以使用 

PS C:\Users\LIHUANG\Desktop\common> Get-Alias -Name dir

CommandType     Name                                               ModuleName
-----------     ----                                               ----------
Alias           dir -> Get-ChildItem
如果想知道get-childitem总共有多少个别名,稍微有一点复杂,需要用到管道命令,在第五章我们会讲到。

PS C:\Users\LIHUANG\Desktop\common> Get-Alias | Where-Object {$_.Definition -eq "Get-ChildItem"}

CommandType     Name                                               ModuleName
-----------     ----                                               ----------
Alias           dir -> Get-ChildItem
Alias           gci -> Get-ChildItem
Alias           ls -> Get-ChildItem

或者

PS C:\Users\LIHUANG\Desktop\common> Dir alias: | Out-String -Stream | Select-String "Get-ChildItem"

Alias           dir -> Get-ChildItem
Alias           gci -> Get-ChildItem
Alias           ls -> Get-ChildItem
这条命令是在转换字符,out-strig将dir alias:转化成为字符串(本来是object),参数-stream保证每一个object转化完之后就直接传入后面的命令。

select-string 筛选出包含’get-childitem‘的字符串。


还有一个小例子可以演示ps的强大

PS C:\Users\LIHUANG\Desktop\common> Dir alias: | Group-Object definition

Count Name                      Group
----- ----                      -----
    2 ForEach-Object            {%, foreach}
    2 Where-Object              {?, where}
    1 Add-Content               {ac}
    1 Add-PSSnapIn              {asnp}
    3 Get-Content               {cat, gc, type}
    3 Set-Location              {cd, chdir, sl}
    1 Clear-Content             {clc}
    2 Clear-Host                {clear, cls}
    1 Clear-History             {clhy}
    1 Clear-Item                {cli}
    1 Clear-ItemProperty        {clp}
    1 Clear-Variable            {clv}
    1 Connect-PSSession         {cnsn}
    2 Compare-Object            {compare, diff}
    3 Copy-Item                 {copy, cp, cpi}
    1 Copy-ItemProperty         {cpp}
    1 Convert-Path              {cvpa}
....


自己制作别名

set-alias edit notepad.exe

在使用set-alias之前,edit会让ps进入一个编辑器,你可以直接关闭ps退出这个编辑器,也可以使用Alt+F 然后 X 退出。

在使用set-alias之后,edit会启动记事本。


别名只在ps运行时有效,当ps退出别名就会失效。

如果你想一直使用别名,那么你可以每次都手动添加别名,或者把别名设置放在start profile里,(start profile会在第十章讲到),或者使用import/export方法。

Export-Alias 使用它的时候,会提示你需要设置一个名字。在这里我们设置为alias1

 Import-Alias alias1. 这样就可以将之前使用到的alias导入了。

也许你会遇到错误

Import-Alias alias1
Import-Alias : Alias not allowed because an alias with the
name "ac" already exists.
At line:1 char:13
+ Import-Alias <<<< alias1

这是因为alias1里保存了所有的别名,包括系统中默认存在的。所以你可以使用

Import-Alias alias1 -Force 这样如果有别名已经存在就强制覆盖它。


删除别名

Del alias:edit    

Del命令不仅可以删除别名,也可以删除文件和文件夹。

Tips:你可以使用以下代码找到所有空别名:

Get-Alias | ForEach-Object {if (!(Get-Command $_.Definition -ea SilentlyContinue)) {$_}}


方法:’扩展的’别名

有一个大家都很熟悉的方法ping,假设你需要经常使用下面的代码

Ping -n 1 -w 100 10.10.10.10

你想给它设置别名,因为参数每次都是一样的。但别名无法起作用。此时就可以定义一个新的方法。

>function quickping { ping -n 1 -w 100 $args }
>quickping 10.10.10.10

Pinging 10.10.10.10 with 32 bytes of data:
Reply from 10.10.10.10: bytes=32 time<1ms TTL=128
Ping statistics for 10.10.10.10:
Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum = 0ms, Average = 0ms

>Set-Alias qp quickping
>qp 10.10.10.10

Pinging 10.10.10.10 with 32 bytes of data:
Reply from 10.10.10.10: bytes=32 time<1ms TTL=128

$args 表示你可以添加任意多的参数(>= 0)

方法的存活时间也跟别名一样,随着ps的关闭而失效。


调用方法和脚本

调用方法和脚本和调用文件的时候一样,你需要指定一个路径,相对路径或者绝对路径。

在之前的cmd中,如果在当前路劲下有一个名为ping.bat的文件,当你要执行它的时候,你可以直接输入

ping

但在ps中,你必须得指定路径,所以是  

.\ping

这样做的好处是,它可以避免一些陷阱。假设有人写了一个病毒程序,名字叫做ping.bat,当你每次想运行ping 10.10.10.10的时候,该程序就会运行,而在ps中,它只会执行真正ping。

调用vb脚本或者ps自己的脚本的时候,需要带后缀名.vbs/.ps1.

如果在执行脚本的时候遇到错误,有可能是因为policy不允许,

你可以采用get-ExecutionPolicy 看看你当前的设置是什么。

一般来说设置成 RemoteSigned 是比较合适的,意思是只限制从远程获得的脚本。

关于ExecutionPolicy 会在第十章讲到


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Use Wireshark 2 to overcome real-world network problems Key Features Delve into the core functionalities of the latest version of Wireshark Master network security skills with Wireshark 2 Efficiently find the root cause of network-related issues Book Description Wireshark, a combination of a Linux distro (Kali) and an open source security framework (Metasploit), is a popular and powerful tool. Wireshark is mainly used to analyze the bits and bytes that flow through a network. It efficiently deals with the second to the seventh layer of network protocols, and the analysis made is presented in a form that can be easily read by people. Mastering Wireshark 2 helps you gain expertise in securing your network. We start with installing and setting up Wireshark2.0, and then explore its interface in order to understand all of its functionalities. As you progress through the chapters, you will discover different ways to create, use, capture, and display filters. By halfway through the book, you will have mastered Wireshark features, analyzed different layers of the network protocol, and searched for anomalies. You’ll learn about plugins and APIs in depth. Finally, the book focuses on pocket analysis for security tasks, command-line utilities, and tools that manage trace files. By the end of the book, you'll have learned how to use Wireshark for network security analysis and configured it for troubleshooting purposes. What you will learn Understand what network and protocol analysis is and how it can help you Use Wireshark to capture packets in your network Filter captured traffic to only show what you need Explore useful statistic displays to make it easier to diagnose issues Customize Wireshark to your own specifications Analyze common network and network application protocols Who this book is for If you are a security professional or a network enthusiast and are interested in understanding the internal working of networks, and if you have some prior knowledge of using Wireshark, then this book is for you. Table of Contents Installing Wireshark 2 Capturing Traffic Filtering Traffic Customizing Wireshark Statistics Introductory Analysis Network Protocol Analysis Application Protocol Analysis I Application Protocol Analysis II Command-Line Tools A Troubleshooting Scenario
Wireshark is a popular and powerful tool used to analyze the amount of bits and bytes that are flowing through a network. Wireshark deals with the second to seventh layer of network protocols, and the analysis made is presented in a human readable form. Mastering Wireshark will help you raise your knowledge to an expert level. At the start of the book, you will be taught how to install Wireshark, and will be introduced to its interface so you understand all its functionalities. Moving forward, you will discover different ways to create and use capture and display filters. Halfway through the book, you'll be mastering the features of Wireshark, analyzing different layers of the network protocol, looking for any anomalies. As you reach to the end of the book, you will be taught how to use Wireshark for network security analysis and configure it for troubleshooting purposes. What you will learn Install Wireshark and understand its GUI and all the functionalities of it Create and use different filters Analyze different layers of network protocols and know the amount of packets that flow through the network Decrypt encrypted wireless traffic Use Wireshark as a diagnostic tool and also for network security analysis to keep track of malware Troubleshoot all the network anomalies with help of Wireshark Resolve latencies and bottleneck issues in the network About the Author Charit Mishra, works as a consultant and pentester at Protiviti, one of the top global consulting firms. He enjoys his job, which involves helping clients identify security vulnerabilities, more than anything. With real hands-on experience in security, he has obtained leading industry certifications such as OSCP, CEH, CompTIA Security+, and CCNA R&S. He also holds a master's degree in computer science. He has delivered professional talks at various institutions and private organizations on information security and penetration testing. You can reach him at LinkedIn at https://ae.linkedin.com/in/charitmishra, and on Twitter at @charit0819. Table of Contents Chapter 1. Welcome to the World of Packet Analysis with Wireshark Chapter 2. Filtering Our Way in Wireshark Chapter 3. Mastering the Advanced Features of Wireshark Chapter 4. Inspecting Application Layer Protocols Chapter 5. Analyzing Transport Layer Protocols Chapter 6. Analyzing Traffic in Thin Air Chapter 7. Network Security Analysis Chapter 8. Troubleshooting Chapter 9. Introduction to Wireshark v2

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值