使用带有凭据的删除项目
我正在尝试使用Remove-Item cmdlet 作为系统自动化的一部分。这些文件存储在需要提升权限才能执行文件删除的服务器上。我有权访问用于此类自动化脚本的域管理员帐户。
下面的代码将构建 PSCredential 对象:
$password = New-Object System.Security.SecureString
"passwordhere".ToCharArray() | ForEach-Object { $password.AppendChar($_) }
$cred = New-Object System.Management.Automation.PSCredential("domain\username",$password)
$cred
我将此对象传递给以下操作:
Remove-Item -LiteralPath $path -Force -Credential $cred
有什么想法吗?
I am attempting to use the Remove-Item cmdlet as part of an automation for a system. The files are stored on a server that requires elevated rights to perform the file deletion. I have access to a domain admin account that I use for such automation scripts.
The code below will build the PSCredential object:
$password = New-Object System.Security.SecureString
"passwordhere".ToCharArray() | ForEach-Object { $password.AppendChar($_) }
$cred = New-Object System.Management.Automation.PSCredential("domain\username",$password)
$cred
I am passing this object to the following action:
Remove-Item -LiteralPath $path -Force -Credential $cred
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不清楚这些文件是本地的(您在服务器上运行脚本)还是远程的(在另一台机器上)。如果是本地尝试使用后台作业运行命令并将凭据传递给 Start-Job:
如果是远程,请尝试使用远程处理:
注意:这要求您在远程计算机上执行 Enable-PSRemoting。
一般来说,将原始密码放入脚本中并不是一个好主意。您可以使用 DPAPI 及更高版本以加密方式存储密码,只有该用户帐户才能解密密码,例如:
It's not clear to me if the files are local (you're running the script on the server) or remote (on another machine). If local try running the command using a background job and pass in the credentials to Start-Job:
If they're remote, try using remoting:
Note: This requires that you execute Enable-PSRemoting on the remote machine.
In general, putting raw passwords in your script isn't a great idea. You can store the password in an encrypted manner using DPAPI and later, only that user account can decrypt the password e.g.:
由于授权,删除项目可能会失败。或者,找到每个文件的引用并使用 .Delete() 删除它,或者将所有文件移至回收站。
Remove-Item can fail due to authorisation. Alternatively, either find the reference for each file and hit it with a .Delete() or move all of the files to the recycle bin.