开发者

Creating file in a user context in powershell

开发者 https://www.devze.com 2023-01-16 10:12 出处:网络
I am trying to create a file using pow开发者_开发技巧ershell 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 trying to create a file using pow开发者_开发技巧ershell 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 ?


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.


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.


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")


Or just make a call to SUBINACL.EXE? No need for password then.

0

精彩评论

暂无评论...
验证码 换一张
取 消