如何使用PowerShell来检查服务器上用户configuration文件位置的位置?

是否有可能使用PowerShell和正则expression式find所有不在特定服务器/文件夹的用户configuration文件? 如果是这样怎么办呢?

在PowerShell中,使用Win32_UserProfile WMI对象远程查找configuration文件:

gwmi -ComputerName <computername> Win32_UserProfile 

要find不在服务器上的用户configuration文件(或者是任何一种),你可以做如下的事情:

 gwmi -ComputerName <computername> Win32_UserProfile | ? {"C:\Users\<username>" -contains $_.LocalPath} 

如果path存在,它会给出结果,如果没有,那么它不会。 你可以做比这更有趣的东西,但基本上这应该完成你所需要的,而不使用正则expression式。

对于当前用户运行:

 $env:USERPROFILE 

要获取所有环境variables的列表,请运行:

 Get-ChildItem Env: 

有趣的是,我今天不得不这样做。 试试这个脚本,用合适的值replaceservernamesharenamec:\path\to\save.csv 。 我从内存中键入这个,所以我不能保证没有错误:(

 $a = [adsisearcher]'(&(objectclass=user)(objectcategory=user)(profilepath=*))' [void]$a.propertiestoload.add('name') [void]$a.propertiestoload.add('profilepath') $a.pagesize = 1000 $a.findall() | foreach-object { if($_.properties.profilepath[0] -notmatch '^\\\\servername\\sharename\\') { $op = '' | select name,profilepath $op.name = $_.properties.name[0] $op.profilepath = $_.properties.profilepath[0] $op } } | export-csv -NoTypeInformation c:\path\to\save.csv 

如果您使LDAP查询的不匹配条件成为一部分,则效率更高
尝试:

 $a = [adsisearcher]'(&(objectclass=user)(objectcategory=user)(!profilepath=\\\\servername\sharename*))'