写点什么

【21-8】PowerShell 输入输出

用户头像
耳东
关注
发布于: 2021 年 06 月 15 日
【21-8】PowerShell 输入输出

输入

关于 PowerShell 的输入有很多个指令,我们来看一些常用的。

Read-Host

Read-Host 是一个交互式的输入指令,主要用于获取交互页面的数据,多用于必须用户干预的脚本。


示例脚本如下


#!/usr/bin/env pwsh# Author:Erdong# PowerShell v5.1Read-Host "请输入你的名字"
复制代码


执行这个脚本,接入如下:


[root@erdong ~]# ./import1.ps1请输入你的名字: ErdongErdong
复制代码


另外可以发现,在输入的文字的最后并没有写 : ,但是 PowerShell 帮我们输出了一个冒号。

Get-Content

如果涉及到大量数据的输入,则需要用到 Get-Content


我们准备一个测试的文件 test-file.txt,内容如下:


aaabbbcccddd
复制代码


测试脚本如下:


#!/usr/bin/env pwsh# Author:Erdong# PowerShell v5.1Get-Content ./test-file.txt
复制代码


执行这个脚本我们可以发现:


[root@erdong ~]# ./import2.ps1aaabbbcccddd
复制代码

Import-Csv

Import-Csv 的命令和 Get-Content 的作用是类似的,区别在于 Import-Csv 是通过 Csv 文件进行导入,并且导入的每一行都是一个对象.我们准备一个测试文件 test-file.csv


TestNameaaabbbcccddd
复制代码


测试脚本如下:


#!/usr/bin/env pwsh# Author:Erdong# PowerShell v5.1Import-Csv ./test-file.csv
复制代码


执行这个脚本我们可以发现:


[root@localhost ~]# ./import3.ps1
TestName---aaabbbcccddd
复制代码


这里大家要注意第一行是对象的属性名,在这里我命名成了TestName,你也可以去定义成其他的名称


常用的输入方式就是这些,当然输入数据的方式还有很多很多,这里不再一一列举,大家可以通过 Get-Command 来去查找更多的导入方式

输出

输出也有很多方式,我们来看几种。

Write-Host

Write-Host 是最常用的输出语句,可以把结果输出到 PowerShell 界面


测试脚本如下:


#!/usr/bin/env pwsh# Author:Erdong# PowerShell v5.1Write-Host "HelloWorld,PowerShell"
复制代码


执行这个脚本可以得到如下内容:


[root@erdong ~]# ./output1.ps1HelloWorld,PowerShell
复制代码

Write-Output

PowerShell 中有 Write-Host 和 Write-Outup 这两个命令,但是很多人都不是很清楚两者的区别。表明上看这两者都可以输出字符到 Console 上,但是他们到底有什么不同呢?


测试脚本如下:


#!/usr/bin/env pwsh# Author:Erdong# PowerShell v5.1Write-Output "HelloWorld,PowerShell"
复制代码


执行这个脚本可以得到如下内容:


[root@erdong ~]# ./output2.ps1HelloWorld,PowerShell
复制代码


我们接下来进一步进行测试,测试脚本如下:


#!/usr/bin/env pwsh# Author:Erdong# PowerShell v5.1Function WriteTest($amount, $rate=0.8){    $amount * $rate}Function WriteOutputTest($amount, $rate=0.8){    Write-Output $amount * $rate}
WriteTest 100
WriteOutputTest 100
复制代码


执行结果如下:


[root@localhost ~]# ./output3.ps180100*0.8[root@localhost ~]#
复制代码


通过测试以后发现,当直接运行方法的时候,会得到所有的两个输出,但是 write-host 是直接输出到控制台的,所以这个值是无法通过变量获得的。当然两者还有个字不同的参数支持,比如 Write-Host 支持 NewLine 等参数,实际上两者的区别是 Write-Host 只是仅仅将字符串输出到屏幕,而 Write-Output 则是可以把对象传递输出,

Export-Csv

这是一个到处成 csv 的命名,可以将内容输出到 csv 文件。


测试脚本如下:


#!/usr/bin/env pwsh# Author:Erdong# PowerShell v5.1Get-Process | Export-Csv -Path tst.csv
复制代码


执行这个脚本可以得到如下内容:


[root@erdong ~]# ./output4.ps1[root@erdong ~]# cat tst.csv"Name","SI","Handles","VM","WS","PM","NPM","Path","CommandLine","Parent","Company","CPU","FileVersion","ProductVersion","Description","Product","__NounName","SafeHandle","Handle","BasePriority","ExitCode","HasExited","StartTime","ExitTime","Id","MachineName","MaxWorkingSet","MinWorkingSet","Modules","NonpagedSystemMemorySize64","NonpagedSystemMemorySize","PagedMemorySize64","PagedMemorySize","PagedSystemMemorySize64","PagedSystemMemorySize","PeakPagedMemorySize64","PeakPagedMemorySize","PeakWorkingSet64","PeakWorkingSet","PeakVirtualMemorySize64","PeakVirtualMemorySize","PriorityBoostEnabled","PriorityClass","PrivateMemorySize64","PrivateMemorySize","ProcessName","ProcessorAffinity","SessionId","StartInfo","Threads","HandleCount","VirtualMemorySize64","VirtualMemorySize","EnableRaisingEvents","StandardInput","StandardOutput","StandardError","WorkingSet64","WorkingSet","SynchronizingObject","MainModule","MainWindowHandle","MainWindowTitle","Responding","PrivilegedProcessorTime","TotalProcessorTime","UserProcessorTime","Site","Container""ata_sff","0","0","0","0","0","0",,"","System.Diagnostics.Process (kthreadd)",,"0",,,,,"Process","Microsoft.Win32.SafeHandles.SafeProcessHandle","996","-20",,"False","2021-6-15 下午8:55:30",,"1749",".","9223372036854771712","0","System.Diagnostics.ProcessModuleCollection","0","0","0","0","0","0","0","0","0","0","0","0","False","RealTime","0","0","ata_sff","1","0",,"System.Diagnostics.ProcessThreadCollection","0","0","0","False",,,,"0","0",,,"0","","True","00:00:00","00:00:00","00:00:00",,"auditd","5187","10","63537152","1114112","0","0","/usr/sbin/auditd","/sbin/auditd","System.Diagnostics.Process (systemd)",,"0.01",,,,,"Process","Microsoft.Win32.SafeHandles.SafeProcessHandle","1032","-4",,"False","2021-6-15 下午8:55:38",,"5187",".","9223372036854771712","0","System.Diagnostics.ProcessModuleCollection","0","0","0","0","0","0","0","0","1114112","1114112","63537152","63537152","False","BelowNormal","8892416","8892416","auditd","1","5187",,"System.Diagnostics.ProcessThreadCollection","10","63537152","63537152","False",,,,"1114112","1114112",,"System.Diagnostics.ProcessModule (auditd)","0","","True","00:00:00.0100000","00:00:00.0100000","00:00:00",,
复制代码


从结果我们可以看到获取的进程信息已经以 csv 的格式存到了指定文件。

Out-File

Out-File可以实现输出文件的重定向。这样可以在很多时候方便地通过文件记录命令执行的输出文本提示符号。


测试脚本如下:


#!/usr/bin/env pwsh# Author:Erdong# PowerShell v5.1"This is PowerShell !" | Out-File test.txt"This is PowerShell !xxxx" | Out-File -Append test.txt
复制代码


执行这个脚本可以得到如下内容:


[root@erdong ~]# ./output5.ps1[root@erdong ~]#  cat test.txtThis is PowerShell !This is PowerShell !xxxx
复制代码


执行成功的时候,在当前目录中生成了一个名为test.txt的文件。如果在文件上加上路径信息,写的是文件的完整路径信息,那么在指定的位置会生成相应的输出文件。


使用Out-File这个 cmdlet,把 This is PowerShell !当作一个管道参数输入,输出到test.txt中。这里使用一个-Append参数,这个参数的含义是,将输入的内容附加到原文的末尾,而不覆盖原文的内容。如果我们不使用-Append参数,第三个命令的显示结果将只有一句 This is PowerShell !


另外也可使用 > 来实现同样的效果。

发布于: 2021 年 06 月 15 日阅读数: 10
用户头像

耳东

关注

还未添加个人签名 2020.05.24 加入

主要研究分享运维技术,专注于监控、CICD、操作系统、云原生领域,公众号【耳东学堂】,知识星球同名,坚持原创,希望能和大家在运维路上结伴而行 邮箱:erdong@mail.erdong.site

评论

发布
暂无评论
【21-8】PowerShell 输入输出