我在让 Powershell 以我期望的方式运行时遇到问题。
我正在尝试使用 get-wmiobject win32_networkconnection
列出当前用户的映射驱动器,这样我就可以遍历这些驱动器。
当我运行 $var = get-wmiobject win32_networkconnection | select -expand localname
我得到的正是我所期望的:映射网络连接的驱动器号列表。
但是,当我运行 $var = (get-wmiobject win32_networkconnection).localname
时,我什么也没得到。它似乎没有正确选择属性。
这是有问题的,因为理想情况下,我想遍历所有驱动器,然后为每个驱动器选择各种属性。相反,似乎我将被迫拼凑一个迭代器,然后一次迭代所有变量(我认为这不是很优雅)。
我对 Powershell 不是很有经验,所以我可能遗漏了一些东西。但是,根据我的阅读,这应该有效。这是 get-wmiobject
的限制吗?
最佳答案
您尝试执行的操作仅适用于 PowerShell 3.0 和更新版本。官方文档非常含糊不清:
What's New in Windows PowerShell 3.0
Windows PowerShell Language Enhancements
Windows PowerShell 3.0 includes many features [...] The improvements include property enumeration, count and length properties on scalar objects, new redirection operators [...]
这篇博文更深入:New V3 Language Features
关于Powershell 点符号不选择数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31322548/