目前我正在分析组织中成千上万个主目录的位置。 我已经运行一个脚本,吐出每个用户以及他们的主目录(共享名称)到一个csv。
Import-Module ActiveDirectory Get-ADUser -SearchBase "OU=Users,DC=org,DC=com" -Filter * -Property * | Select-Object -Property sAMAccountName,homeDirectory| Export-CSV -Path C:\homedirs.csv
无论是通过修改这个脚本还是对结果文件进行操作,我怎样才能产生相同的结果,但是每一个共享代替了它代表的实际path?
也知道这些共享位于多个服务器上。
编辑:它看起来像wmic /node:<servernames> share返回的信息wmic /node:<servernames> share可以用于检查我已经有的共享名称列表,但我怎样才能把它绑在?
我会以类似于这样的方式去完成任务:
get-wmiobject cmdlet 调用服务器,并使用上一步中提取的服务器名和共享名来获取本地path 。 这可以通过例如调用每个唯一的服务器一次并以批处理的方式在本地执行针对该服务器的所有共享查询(例如使用invoke-command和/或作为并行处理多个服务器的workflow运行)来优化。
[编辑]这是一个基于上述步骤的具体例子。
预期homedir的input格式为\\ servername \ sharename \ subdir
在处理时,我select仅对\\ servername \ sharename感兴趣
输出我select是sAMAccountName; homeDirectory; ServerName; ShareName; ShareLocalPath
自然而然,取决于你需要调整的东西。
Import-Module -Name ActiveDirectory $Cred = Get-Credential $CsvFile = 'E:\temp49\homedirs2.csv' # Read the data from AD into a hashtable. if ($HashTable) {Remove-Variable HashTable} $HashTable = Get-ADUser -Filter * -Property sAMAccountName, homeDirectory | Select-Object -Property sAMAccountName, homeDirectory # Iterate through the hashtable. $HashTable | ForEach-Object ` { # Only process if the home directory has a value. if (-not($_.homeDirectory -eq $null)) ` { # Convert the homedir and samaccountname into strings and store in variables. [string]$homeDirectory = $_.homeDirectory [string]$sAMAccountName = $_.sAMAccountName # Extract the servername and the sharename from the homedirectory path. [array]$homeDirectorySplit = $homeDirectory -Split [regex]::Escape('\') $ServerName = $homeDirectorySplit[2] $ShareName = $homeDirectorySplit[3] # ...If more levels are needed just keep going. # Call the server to get the shares local path. $ShareLocalPath = ((Get-WmiObject -ComputerName $ServerName ` -Credential $Cred Win32_Share) | Foreach-Object { $_.Name -eq $ShareName }).Path # Print to a semicolon delimited file. "$sAMAccountName;$homeDirectory;$ServerName;$ShareName;$ShareLocalPath" | Out-File $CsvFile -NoClobber -Append } }
如果这是一个严重的努力,我会添加日志logging和错误检查。 我将如上所述优化服务器调用,并在使用Test-Connection调用每个服务器之前对其进行ping。 如果服务器数量众多,我将最终考虑实现并行处理,所有这些都是以可靠性,资源节约和执行速度的名义。