在 powershell 中的用户上下文中创建文件
我正在尝试在特定用户上下文中使用 powershell 创建文件。例如,我的本地计算机上有一个用户 user01,我想在其上下文中创建一个文件。
我正在做类似
New-Item c:\file.txt -Credential User01 的
事情,它可以工作,但会提示我输入我不希望的密码。有什么方法可以在不提示输入密码的情况下完成此操作吗?
I am trying to create a file using powershell in a specific user context. E.g I have a user user01 on my local machine and I want to create a file in its context.
I am doing something like
New-Item c:\file.txt -Credential User01
It works but prompts me for password which I dont want it to. Is there any way I can accomplish this without having it prompt for password ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
文件系统实际上不支持 new-item 上的凭据参数,因此我不确定您所说的“它有效”是什么意思。它不会以传递的用户身份创建文件。事实上,文件系统提供程序会说:
“提供程序不支持使用凭据。请在不指定凭据的情况下再次执行该操作。”
根据有根据的猜测,我想说您正在尝试使用不同的所有者创建一个文件。 PowerShell 无法自行执行此操作,因此您需要以下重要脚本:
http://cosmoskey.blogspot.com/2010/07/setting-owner-on-acl-in-powershell.html
它的工作原理是为您启用 SeBackup 权限安全令牌(但您必须已经是管理员。)这允许您设置文件的任意所有者。通常您只能将所有者更改为管理员或您自己的帐户。
哦,这个脚本仅适用于 powershell 2.0。
The credential parameter on new-item is not actually supported for filesystems, so I'm not sure what you mean by "it works." It does NOT create the file as the passed user. In fact, the filesystem provider will say:
"The provider does not support the use of credentials. Perform the operation again without specifying credentials."
Taking an educated guess, I'd say you're trying to create a file with a different owner. PowerShell cannot do this on its own, so you'll need the following non-trivial script:
http://cosmoskey.blogspot.com/2010/07/setting-owner-on-acl-in-powershell.html
It works by enabling the SeBackup privilege for your security token (but you must already be an administrator.) This allows you to set any arbitrary owner on a file. Normally you can only change owner to administrators or your own account.
Oh, and this script is for powershell 2.0 only.
您可以查看 Windows 实用程序
takeown.exe
,而不是在此使用 PowerShell cmdlet 或 .NET 脚本。但是,即使它也要求您提供要为其分配所有权的用户的密码。Rather than use a PowerShell cmdlet or .NET scripting on this one, you might take a look at the Windows utility
takeown.exe
. However, even it requires you supply the user's password that you're assigning ownership to.好的,我确实在用户上下文中启动进程,然后创建一个文件。就像魅力一样。
密码、文件路径和用户名作为参数从命令行传入。
$pw = 转换为安全字符串“$Password” -asplaintext –force
$credential = new-object -typename system.management.automation.pscredential -argumentlist "-default-",$pw
$localArgs = "/c echo>$FilePath"
[System.Diagnostics.Process]::Start("cmd", $localArgs, "$UserName", $credential.Password, "$Computer")
Ok, I do start process in the user context and then create a file. Works like a charm.
Password, FilePath and UserName are passed in as arguments from command line.
$pw = convertto-securestring "$Password" -asplaintext –force
$credential = new-object -typename system.management.automation.pscredential -argumentlist "-default-",$pw
$localArgs = "/c echo>$FilePath"
[System.Diagnostics.Process]::Start("cmd", $localArgs, "$UserName", $credential.Password, "$Computer")
或者只是调用 SUBINACL.EXE?那就不需要密码了。
Or just make a call to SUBINACL.EXE? No need for password then.