Powershell 学习: 第六章:管道 动手实验答案

1. 在控制台运行: Get-Service | Export-Csv SHAWSERVICES.CSV | Out-File 会发生什么情况?

根据错误信息我们可以看出 out-file 命令需要一个 path参数,但是很显然这个参数是空的。 通过help Out-File 命令可以看到这个cmdlet 命令的语法中,-filepath 是一个必选参数。

然而在我们运行的命令中,get-Service 将得到的结果传输给了 Export-Csv SHAWSERVICES.CSV ,作为export-csv 命令中 -inputObject 参数的参数值,进程信息保存在了SHAWSERVICES.CSV 文件中。

然而这个命令运行以后,已经没有结果可以再传输给 out-file 这个命令了,所以报错。

2. 除了通过get-service 命令获得服务以管道方式将服务传输到 stop-service 之外,stop-service 还可以通过什么方式来停止一个服务?

通过帮助命令我们可以看到有多个提供要停止的进程的参数 :

    a. -Exclude 停止除提供的服务意外的服务

    b. -Include 停止服务名字中包含所提参数的服务

    c. -InputObject 提供可以代表某个服务的 ServiceController, 来停止该服务

     d. -Name 直接提供该服务的名字,来停止该服务

3. 应该使用哪个参数来创建一个数显分隔符文件替代一个逗号分隔符(csv)文件?

通过帮助系统可以知道,可以通过 -Delimiter 参数来指定其他的分隔符创建csv 文件

如: Get-Process | Export-Csv -Path "processes.csv" -Delimiter "|"

4. 可以在已导出的CSV 文件头部忽略 # 命令行吗? 该怎么做?

PS C:\WINDOWS\system32> Start-Job -ScriptBlock { Get-Process } | Export-Csv jobs.csv
PS C:\WINDOWS\system32> $Header = "MoreData", "StatusMessage", "Location", "Command", "State", "Finished", "InstanceId","SessionId", "
Name","ChildJobs", "Output", "Error", "Progress", "Verbose", "Debug", "Warning", "StateChanged"
PS C:\WINDOWS\system32> $A = (Get-Content .\jobs.csv)
PS C:\WINDOWS\system32> $A = $A[0], $A[2.. ($A.Count - 1)]
PS C:\WINDOWS\system32> $A > jobs.csv
PS C:\WINDOWS\system32> $J = Import-Csv jobs.csv -Header $Header
PS C:\WINDOWS\system32> $J


MoreData      : Running
StatusMessage : True
Location      :
Command       : localhost
State         :  Get-Process
Finished      : Running
InstanceId    : System.Threading.ManualResetEvent
SessionId     : fa0c857b-77cf-4edd-8eeb-6d421a6e6c4b
Name          : 3
ChildJobs     : Job3
Output        : System.Collections.Generic.List`1[System.Management.Automation.Job]
Error         : 8/1/2018 3:48:24 PM
Progress      :
Verbose       : BackgroundJob
Debug         : System.Management.Automation.PSDataCollection`1[System.Management.Automation.PSObject]
Warning       : System.Management.Automation.PSDataCollection`1[System.Management.Automation.ErrorRecord]
StateChanged  : System.Management.Automation.PSDataCollection`1[System.Management.Automation.ProgressRecord]

5. Export-csv 和 export-clixml 都可以通过创建并覆盖文件来修改系统,你可以用什么参数来阻止他们覆盖文件? 还有什么参数可以在你输出文件前提醒并请求确认?

 Get-Service | Export-Csv SHAWSERVICES.CSV -Noclobber 可以阻止该命令覆盖已经存在的文件

-confirm 参数可以在命令运行前请求确认是否确定运行该命令: Get-Service | Export-Csv SHAWSERVICES.CSV -Confirm

6. Windows 维护少数局部配置,包括一个默认分隔符列表。在美国系统中,分隔符是逗号,你如何让 export-csv 使用当前系统默认的分隔符而不是逗号?

通过帮助文档我们可以看有一个名为 -UseCulture 的参数, 该参数的作就是让该cmdlet 使用系统当前语言的分隔符。

Indicates that this cmdlet uses the list separator for the current culture as the item delimiter. The default is a comma (,).

        This parameter is very useful in scripts that are being distributed to users worldwide. To find the list separator for a
        culture, use the following command: `(Get-Culture).TextInfo.ListSeparator`.

猜你喜欢

转载自blog.csdn.net/weixin_42545594/article/details/81326388
今日推荐