2

I was hoping I could get some guidance.

I have a csv file with two columns "Path" and "Owner"

I've created the belkow script and it can read from trhe CSV file, but when I attempt to assign the variables through from the csv it fails.

I can't see where I'm going wrong. Any help would be appreciated.

import-csv c:\test\output.csv | foreach-object {
  $Path = $_.path
  $owner =$_.owner
  "The username is $Path and the owner is $owner"
}
ForEach-Object {
         $Path = $_.path
         $owner =$_.owner
         $Account = New-Object -TypeName System.Security.Principal.NTAccount -ArgumentList '$owner';
         $Acl = Get-Acl -Path $path ; 
    $Acl.SetOwner($Account);
    Set-Acl -Path $owner -AclObject $Acl; 
    }

The output is correct from the first segment and shows the path and the owner, but the second part doesn't set the owner according to the path.

Thanks

  • 1
    The second foreach has no input object to iterate over. – LotPings Mar 19 '17 at 15:18
2

The second foreach has no input object to iterate over. So either

  • import to a variable and pipe it twice to the foreach loops
  • import twice

$csv = import-csv c:\test\output.csv 
$csv | foreach-object {
  $Path = $_.path
  $owner =$_.owner
  "The username is $Path and the owner is $owner"
}
$csv | ForEach-Object {
    $Path = $_.path
    $owner =$_.owner
    $Account = New-Object -TypeName System.Security.Principal.NTAccount -ArgumentList "$owner"
    $Acl = Get-Acl -Path $path
    $Acl.SetOwner($Account);
    Set-Acl -Path $Path -AclObject $Acl
}
  • The issue now is it's not accepting the variables in $Acl.SetOwner($Account) Exception calling "SetOwner" with "1" argument(s): "Some or all identity references could not be translated." – Norrin Rad Mar 19 '17 at 15:24
  • Variables inside single quotes are not expanded, use double quotes instead (sorry didn't test this for the obvious previous mistake). – LotPings Mar 19 '17 at 15:27
  • Hi Thanks for your help with this.. I can't believe I made such schoolboy errors :) – Norrin Rad Mar 19 '17 at 15:31
  • the errors I am now getting is Set-Acl -Path $owner -AclObject $Acl; Set-Acl : Cannot find path and the way I have the csv set up is Path Owner C:\Users\Gin\Desktop\test\New folder Host\Nath C:\Users\Gin\Desktop\test\New folder - Copy Host\Raj – Norrin Rad Mar 19 '17 at 15:32
  • $owner is not a path, change to $path. Maybe you should use Test-Path prior trying to change the ownership. I'd gracefully accept a vote up and/or a checked answer. – LotPings Mar 19 '17 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.