Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test writing to a file share path using credential?

I have an array of Credential objects and I would like to test that these credentials have permissions to write a file to a file share.

I was going to do something like

$myPath = "\\path\to\my\share\test.txt"
foreach ($cred in $credentialList)
{
    "Testing" | Out-File -FilePath $myPath -Credential $cred
}

but then I discovered that Out-File doesn't take Credential as a parameter. What's the best way to solve this?

like image 762
Mark Allison Avatar asked Jan 24 '14 14:01

Mark Allison


2 Answers

You can use New-PSDrive:

$myPath = "\\path\to\my\share"

foreach ($cred in $credentialList)
{
  New-PSDrive Test -PSProvider FileSystem -Root $myPath -Credential $Cred
  "Testing" | Out-File -FilePath Test:\test.txt 
  Remove-PSDrive Test
}
like image 120
mjolinor Avatar answered Nov 10 '22 05:11

mjolinor


Here is asituation where an old exe (net.exe) seems to do better than powershell... I guess you could try to map a network drive with the credential provided then test to write a file to that drive :

$cred=get-credential 
$pass=$cred.GetNetworkCredential().Password 
net use q: \\servername\share $pass /user:$cred.username  
like image 5
Loïc MICHEL Avatar answered Nov 10 '22 04:11

Loïc MICHEL