2016-12-16 160 views
1

我已经构建了一个脚本,用于从AD,Exchange,Lync等用户中删除该脚本。在脚本结尾删除用户配置文件文件夹和主目录。问题是,只有被删除的用户拥有完全控制配置文件文件夹,所以我需要用他/她的凭证删除文件夹。无法使用PowerShell删除网络驱动器上的目录

首先,我想这:

Remove-Item -Path \\networkdrive\share$\profilefolder -Recurse -Force -Credential $UsersCredentials 

,但我得到了以下错误:

Remove-Item : Cannot retrieve the dynamic parameters for the cmdlet. The FileSystem provider supports credentials only on the New-PSDrive cmdlet. Perform the operation again without specifying credentials.

然后我试图与New-PSDrive cmdlet来做到这一点:

New-PSDrive -Name F -PSProvider FileSystem -Root \\networkdrive\share$\profilefolder -Credential $UsersCredentials 

我得到出现以下错误:

New-PSDrive : Multiple connections to a server or shared resource by the same user, using more than one user name, are not allowed. Disconnect all previous connections to the server or shared resource and try again

我读的地方,连接到IP地址的网络驱动器可能会解决上述错误,但是当我尝试,我得到了一个新的错误:

New-PSDrive : The specified network password is not correct

你有什么意见或建议,以我能做什么?我的目标是使用被删除者的凭据来删除文件夹。我的下一步是下载NetApp PowerShell cmdlet并查看是否可以使用这些目录删除目录。

编辑:我决定不尝试删除其他凭据的文件夹,而是采取所有权,然后删除文件夹。我想:

takeown /F \\server\share\folder /A /R /D y 

但我得到了以下错误:

SUCCESS: The file (or folder): "\\server\share\folder" now owned by the administrators group. 

takeown: 在行:1字符:1 + takeown/F \服务器\共享\文件夹/ A/R/D y + ~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~ + CategoryInfo:NotSpecified:(:字符串)[],的RemoteException + FullyQualifiedErrorId:NativeCommandError

错误:传递给系统调用的数据区域太小。

因此,我似乎无法取得所有权,出于某种原因。任何建议如何从这里出发?

+0

被删除的人是否已将驱动器映射到该位置? –

+0

如果您拥有该用户的凭据,为什么不直接运行脚本呢?我希望系统管理员或某人有权访问以便能够删除这些类型的文件夹 – Matt

+2

如果配置文件文件夹和主目录存储在ADUser对象中,请将其从对象中保存,然后在需要删除时,只需'Remove-Item -Path $ profilefolder'和'Remove-Item -Path $ homefolder'。在删除文件夹之前,您也可以考虑将这些文件夹的所有权作为管理员。 您描述的业务案例几乎与https://learn-powershell.net/2014/06/24/changing-ownership-of-file-or-folder-using-powershell/ –

回答

0
Remove-Item -Path \networkdrive\share`$\profilefolder -Recurse -Force-Credential $UsersCredentials 

用倒退(`)将路径中的$退出。

+1

供参考:如果有以下字母数字,您只需要跳过$。斜线在案件中停止。尽管没有危害逃脱。 – Matt

+0

'无法检索cmdlet的动态参数。“我认为这恰好表明了这种情况? – 4c74356b41

+0

这似乎没有帮助。我犯了同样的错误。 – TimoY