【问题标题】:Where in the world is complete documentation for PowerShell scripting?世界上哪里有完整的 PowerShell 脚本文档?
【发布时间】:2013-06-14 02:26:06
【问题描述】:

我今天开始为我的工作编写一些 powershell 脚本,我可以找到这个页面:http://technet.microsoft.com/en-us/library/hh849827.aspx

这显示了我在脚本中使用的所有 Cmdlet,但我找不到有关如何使用这些 Cmdlet 返回的对象的文档。例如,我正在使用 Get-ChildItem cmd 以递归方式获取目录中的所有文件。然后我使用这样的 ForEach 循环:

 $dest = "C:\Users\a-mahint\Documents\Testing\Dest"
                    $destlist = Get-ChildItem $dest -Recurse

                    foreach ($file in $destlist){
                    write-host "File: $file"
                    write-host $file
                    $result = test-path -path "C:\Users\a-mahint\Documents\Testing\Src\*" -include $file.Name
                        if (-not $result){
                          Copy-Item $file -Destination "$backup"
                        }

                    }
                    write-host "Done copying deleted files"

除了我不知道 $file 是什么类型的对象...在上面的文档中,它只是说它输出一个 System.Object,但这根本没有帮助。我想知道这个对象的所有属性,以便我可以使用它们进行调试。

【问题讨论】:

  • 根据您上面的代码,$dest 位于 c:\ 下,这是一个使用文件系统提供程序的 PSDrive。当您将 Get-ChildItem 与该类型的提供程序一起使用时,您会得到一系列 System.IO.FileSystemInfo 对象,DirectoryInfoFileInfo,具体取决于每个项目是目录还是文件。
  • $file.GetType() 将显示它实际上是一个 FileInfo 或 DirectoryInfo,基于 System.IO.FileSystemInfo。
  • @EBGreen 将其发布为您的解决方案。这就是我用的
  • 我会,只是它没有回答您实际提出的问题。它回答了适用于您尝试解决的特定问题的特定子集。但一般来说,Get-Help、Get-Command 和 Get-Member 应该是您学习 Powershell 时最常用的三个 cmdlet。

标签: powershell


【解决方案1】:

来自question,有一次我问Andy Arismendi 提供了一些链接供我阅读。

您可以下载上述规范:2.03.0

$file = Get-Item C:\foo.txt

记住有一个$file | Get-Member 命令可以用来查看对象的方法和属性。此外,由于 PowerShell 中的所有内容都是对象,因此您始终可以执行 $file.GetType()Bing that type

【讨论】:

    【解决方案2】:

    Get-ChildItem "C:\Windows\System32\WindowsPowerShell\v1.0\en-US" -Filter *.txt

    【讨论】:

      【解决方案3】:

      这是 get-childitem 的不错参考。

      http://technet.microsoft.com/en-us/library/ee176841.aspx

      您究竟需要什么样的文件详细信息?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-02-22
        • 1970-01-01
        • 2022-07-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多