Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PowerShell脚本,用于向保存在变量中的主目录列表中的文件夹名称添加日期_Powershell_Active Directory - Fatal编程技术网

PowerShell脚本,用于向保存在变量中的主目录列表中的文件夹名称添加日期

PowerShell脚本,用于向保存在变量中的主目录列表中的文件夹名称添加日期,powershell,active-directory,Powershell,Active Directory,我是PowerShell的新手。我有一个.csv用户文件,我拉了一个主目录列表,用于使用以下内容: $hdirpath = Get-Content C:\Temp\UserList.csv | ForEach {Get-ADUser $_ -properties HomeDirectory | Select HomeDirectory} $hdirpath = Get-Content C:\temp\dir.txt $fullDate = Get-Date $date = $($fullDa

我是PowerShell的新手。我有一个.csv用户文件,我拉了一个主目录列表,用于使用以下内容:

$hdirpath = Get-Content C:\Temp\UserList.csv | ForEach {Get-ADUser $_ -properties HomeDirectory | Select HomeDirectory}
$hdirpath = Get-Content C:\temp\dir.txt

$fullDate = Get-Date
$date = $($fullDate.ToShortDateString())
$day = $date -replace "/", "."

foreach ($path in $hdirpath) {

    Get-ChildItem -Path $_ -Recurse | Where-Object {$_.PSIsContainer -eq $true} |
        Rename-Item -NewName {if ($_.Name.StartsWith('target') -and $_.Name.EndsWith('folder')) {$_.Name + " " + $($day)}}
}
上面的输出示例如下所示:

主目录 \servername\Users\roc03\username
\servername\Users\nov01\username
\servername\Users\roc05\username

现在我想检查每个用户路径中是否存在一个文件夹,如果存在,我想将今天的日期添加到该文件夹名称的末尾。我知道有一个If-Exist-Then命令,我可能可以使用它,但我不确定如何将它与保存在$hdirpath中的信息一起使用


任何帮助都将不胜感激。提前谢谢。

新手本人,请原谅任何错误

从我的头脑中我想到了这一点:

foreach ($path in $hdirpath) {

$folders = Get-ChildItem $_ -Recurse | Where-Object {$_.PSIsContainer -eq $True} | Select-Object FullName

$folders | if ($_.Name -eq "Folder Name") {Rename-Item "Folder Name" + Get-Date}
}
有人可能会纠正我,但如果他们不尝试,如果你有任何问题,请告诉我

另外-为了将来参考,请确保在发布问题之前包含您尝试过的代码

编辑

因此,我现在得到以下信息:

$hdirpath = Get-Content C:\Temp\UserList.csv | ForEach {Get-ADUser $_ -properties HomeDirectory | Select HomeDirectory}
$hdirpath = Get-Content C:\temp\dir.txt

$fullDate = Get-Date
$date = $($fullDate.ToShortDateString())
$day = $date -replace "/", "."

foreach ($path in $hdirpath) {

    Get-ChildItem -Path $_ -Recurse | Where-Object {$_.PSIsContainer -eq $true} |
        Rename-Item -NewName {if ($_.Name.StartsWith('target') -and $_.Name.EndsWith('folder')) {$_.Name + " " + $($day)}}
}
这将返回多个错误,说明它
无法计算参数“NewName”,因为它的参数输入没有产生任何输出。
,但它可以工作并将当前日期附加到文件夹名称上

第3-5行获取日期并将其格式设置为“.”而不是“\”。你可以格式化它,但显然对你有用

我的测试结构是“C:\temp\roc6\username1”-有多个子文件夹和多个“usernameX”。我要重新命名的测试文件夹名为“targetfolder”。最终结果以“targetfolder 08.03.2017”结束

修订版

这将是我最后一次运行脚本-以我有限的知识,这是我能做的最好的

$ErrorActionPreference = "SilentlyContinue"
$hdirpath = Get-Content C:\temp\dir.txt

$fullDate = Get-Date
$day = $($fullDate.ToShortDateString()) -replace "/", "."

foreach ($path in $hdirpath) {
   Get-ChildItem -Path $_ -Recurse | Where-Object {$_.PSIsContainer -eq $true} |
   Rename-Item -NewName {if ($_.Name.StartsWith('target') -and $_.Name.EndsWith('folder')) {$_.Name + " " + $($day)}}
}

谢谢你的快速回复。我的代码是“Get Item-Path”\\servername\Users\roc06\username\Portability”| Rename Item-NewName“Portability$(Get Date-Format“MMddyyyy”)”,它在每次处理一个用户时起作用。当我尝试您的代码时,不幸地得到一个错误,指出术语“if”无法识别为cmdlet、函数等的名称。我尝试在“If”之前添加“foreach对象”,但出现一个错误,指出找不到接受参数“System.object[]”的位置参数。不知道下一步该怎么办。我记得我把日期整理成我想要的格式,然后把它放在脚本顶部的一个变量中。如果您有用户名列表,您可以将“\\servername\users\roc06\username\portability”位中的“username”替换为变量?然后在上面使用foreach函数?(抱歉从我的手机上发帖,或者会发示例代码)希望这么简单。不幸的是,这不起作用,因为用户名上方的文件夹(在本例中为roc06)对于每个用户并不总是相同的。这就是为什么我必须在变量$hdirpath中为每个用户使用完整路径。好的,明天我必须为您进行另一次正确的查看,并尝试在我的end@user3642200-我在答案底部添加了一个新脚本。试试看,让我知道它是否适合你。