使用 powershell 重命名用户目录中的文件夹时出错



我对PowerShell很陌生,但我编写了一个脚本,重命名每个用户网络主目录中的4个文件夹。 用户 SAM 帐户是从文本文件中提取的。

脚本有效,但在脚本运行后出现这样的错误。为什么我会得到这个?

">

获取项:找不到路径"C:\可移植性",因为它不存在。 行:3 字符:5 + 获取项目 - 路径"$line\可移植性" |重命名-项目 -新名称 "可移植性$(获取- ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (C:\Portability:String) [get-Item], ItemNotFoundException + FullQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Command.GetItemCommand">

这是脚本:

#Import AD PowerShell Module
Import-Module ActiveDirectory
#Get Home Path for each user
$GetHD = Get-Content C:TempUserList.txt | ForEach {Get-ADUser $_ -properties HomeDirectory | Select HomeDirectory} > C:tempUserInfo.txt
#Copy list of paths to C:tempdir.txt and safe content to variable $hdirpath
Get-Content C:tempUserInfo.txt | Select-Object -Skip 3 | Foreach {$_.TrimEnd()} | Out-File C:Tempdir.txt
$hdirpath = Get-Content C:Tempdir.txt
#Rename folder if exist adding current date at end of folder
ForEach ($line in $hdirpath) {
Get-Item -Path "$linePortability" | Rename-Item -NewName "Portability$(Get-Date -Format ""MMddyyyy"")" -WhatIf
Get-Item -Path "$lineProfile" | Rename-Item -NewName "Profile$(Get-Date -Format ""MMddyyyy"")" -WhatIf
Get-Item -Path "$lineProfile.v2" | Rename-Item -NewName "Profile.v2$(Get-Date -Format ""MMddyyyy"")" -WhatIf
Get-Item -Path "$lineTSProfile.v2" | Rename-Item -NewName "TSProfile.v2$(Get-Date -Format ""MMddyyyy"")" -WhatIf
}

您说"重命名文件夹(如果存在),但在对文件夹运行命令之前没有检查文件夹是否存在,这就是收到错误的原因:

找不到路径"C:\可移植性",因为它不存在">

您可以使用简单的if更新代码以及Test-Path来检查文件夹是否存在,并且仅在存在时才尝试重命名:

ForEach ($line in $hdirpath) {
if(Test-Path "$linePortability"){ Rename-Item -Path "$linePortability" -NewName "Portability$(Get-Date -Format ""MMddyyyy"")" -WhatIf }
if(Test-Path "$lineProfile"){ Rename-Item -Path "$lineProfile" -NewName "Profile$(Get-Date -Format ""MMddyyyy"")" -WhatIf }
if(Test-Path "$lineProfile.v2"){ Rename-Item -Path "$lineProfile.v2" -NewName "Profile.v2$(Get-Date -Format ""MMddyyyy"")" -WhatIf }
if(Test-Path "$lineTSProfile.v2"){ Rename-Item -Path "$lineTSProfile.v2" -NewName "Installs$(Get-Date -Format ""MMddyyyy"")" -WhatIf }
}

编辑:

这是一个版本,它消除了对多个 txt 文件作为临时步骤的需要,并添加了重命名功能以避免重复代码行。

我已经删除了两个位,因为我不确定为什么您要跳过列表中的前三个用户(Select-Object -Skip 3),或者为什么需要删除任何尾随空格(Foreach {$_.TrimEnd()}),因为这意味着当您尝试重命名时,HomeDirectory 路径将不匹配。

Function Rename-Folder
{
Param (
[Parameter(Mandatory=$true)]
[string]$Path,
[Parameter(Mandatory=$true)]
[string]$NewName
)
if(Test-Path $Path){ Rename-Item -Path $Path -NewName $NewName}
}
Import-Module ActiveDirectory
#Get Home Path for each user
$HomeDirectories = Get-Content "C:TempUserList.txt" | ForEach {Get-ADUser $_ -properties HomeDirectory | Select HomeDirectory}
#Rename folder if exist adding current date at end of folder
ForEach ($line in $HomeDirectories) {
Rename-Folder -Path "$linePortability" -NewName "Portability$(Get-Date -Format ""MMddyyyy"")"
Rename-Folder -Path "$lineProfile" -NewName "Profile$(Get-Date -Format ""MMddyyyy"")"
Rename-Folder -Path "$lineProfile.v2" -NewName "Profile.v2$(Get-Date -Format ""MMddyyyy"")"
Rename-Folder -Path "$lineTSProfile.v2" -NewName "Installs$(Get-Date -Format ""MMddyyyy"")"
}

注意:我在 AD 设置中不使用主目录,因此无法完全测试,但在单独测试时效果很好。

最新更新