【发布时间】:2019-06-03 13:11:21
【问题描述】:
我对 Powershell 还很陌生。我试图通过引用作为备份一部分创建的每个 GPO 备份的“gpresult.xml”文件中的名称来重写 GPO 备份文件夹的名称,以使用它们的友好名称而不是它们的 GUID。但是,我不明白如何引用正在读入 ForEach-Object 循环的特定对象(在本例中为文件夹名称),以便读入该文件夹下的文件。
function Backup_GPO {
$stamp = Get-Date -UFormat "%m%d"
New-Item -ItemType "directory" -Name $stamp -Path \\dc-16\share\GPO_Backups -Force | out-null # create new folder to specify day backup is made
Backup-GPO -All -Path ("\\dc-16\share\GPO_Backups\" + "$stamp\" )
Get-ChildItem -Path \\dc-16\share\GPO_Backups\$stamp | ForEach-Object {
# I want to reference the current folder here
[xml]$file = Get-Content -Path (folder that is being referenced in for loop)\gpresult.xml
$name = $file.GPO.Name
}
我来自 Python,如果我想引用我当前正在迭代的对象,我可以非常简单地这样做 -
for object in list:
print(object)
如何在 Powershell 的 ForEach-Object 命令中引用当前正在使用的对象?
【问题讨论】:
-
大多数人使用
$_,但$psitem可以作为替代使用。检查 PowerShell 帮助总是值得的,因为它非常全面地介绍了语言的基础知识:ForEach-Object
标签: powershell