我尝试将文件重命名export.csv
为$name
但扩展名.csv
改为.file
。输出仍然是$name.file
。
Import-Module ActiveDirectory
$name = Read-Host "Enter Group Name"
Get-ADGroupMember -identity "$name" -Recursive |select name, SamAccountName| export-csv -path C:\export.csv -NoTypeInformation
get-childItem *.file | Rename-Item -newname {$name.name -replace '\.file', '.csv'}
所以我这样做了并且有效:
Import-Module ActiveDirectory
$name = Read-Host "Enter Group Name"
Get-ADGroupMember -identity "$name" -Recursive |select name, SamAccountName| export-csv -path h:\groups\export.csv -NoTypeInformation
rename-item "h:\groups\export.csv" $name
Dir h:\groups\ | Rename-Item -newname {$_.name + '.csv'}
但是,我如何让它在运行命令时重命名文件,而不是每次都对整个目录执行此操作? 什么一直在添加.csv
?
答案1
解决方案是将此行:export-csv -path h:\groups\export.csv -NoTypeInformation 更改为:export-csv -path "h:\groups\$Name.csv" -NoTypeInformation