views:

60

answers:

4

Hi, 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 ?

+1  A: 

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.

x0n
A: 

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.

Keith Hill
A: 

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

Frank Q.
A: 

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

Simon Catlin