AK_*_*AK_ 2 .net powershell f# cmd f#-scripting
我喜欢F#,我想通过编写一些脚本来实现一些烦人任务的自动化.
我如何以与CMD或PowerShell相同的方式与其他程序交互,例如RoboCopy或iisreset或net?
我知道我可以自己做,System.Diagnostics.Process但很难做到正确(返回代码,标准流等).
必须有一个图书馆,任何建议?
您想使用System.Management.Automation命名空间.这是从F#运行一些cmdlet的示例.
// C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\3.0
open System.Management.Automation
open System.Management.Automation.Runspaces;
let runSpace = RunspaceFactory.CreateRunspace()
runSpace.Open()
let pipeline = runSpace.CreatePipeline()
let getProcess = new Command("Get-Process")
pipeline.Commands.Add(getProcess)
let sort = new Command("Sort-Object")
sort.Parameters.Add("Property", "VM")
pipeline.Commands.Add(sort)
// Identical to the following PowerShell command line:
// PS > Get-Process | Sort-Object -Property VM | select ProcessName
let output = pipeline.Invoke()
for psObject in output do
psObject.Properties.Item("ProcessName").Value.ToString()
|> printfn "%s"
Run Code Online (Sandbox Code Playgroud)
您还可以使用F#构建cmdlet并使用PowerShell移动数据.在Visual Studio F#团队博客上查看这篇文章.这是如何在F#中编写Cmdlet的一个很好的例子.您也可以将F#嵌入到PowerShell中,但通常最好制作Cmdlet.
Cmdlet MSDN参考
使用Windows PowerShell编写脚本
| 归档时间: |
|
| 查看次数: |
630 次 |
| 最近记录: |