0

I have this issue deploy Sharepoint using Powershell:

I have Windows Service C# with credentials for user UserInstallerMoss. Windows Service execute an EXE Console Aplicaction C# with credentials UserInstallerMoss. EXE Console Aplicaction executes powershell.exe with credentials UserInstallerMoss.

UserinstallerMOss is Farm Administrator, local administrator, WSS_ADMIN_WPG group...

Powershell functions returns $true value:

$ok = IsCurrentUserAdmin
$ok = IsCurrentUserAdmin2

Fails in this line:

        $fileCollection.Delete($file.Name)

Call to the function Add-ItemsToLib

$siteAssets = Get-SPSite -identity $urlAssetsSite
$webAssets = $siteAssets.OpenWeb()

Write-Host "Uploading images to images library... Debug: imageslistName: $imageslistName - imagesDirectory: $imagesDirectory "
Add-ItemsToLib $webAssets $imageslistName $imagesDirectory

UPDATE
Note: Finally, I discover that script ps1 works if I use without Test-Connection cmdlet.

In the script ps1, there was Test-Connection cmdlet for testing:

$Servidor = $env:ComputerName
if (Test-Connection -ComputerName $Servidor -Count 1 -Quiet)
{
     WriteTraceForTrans ("Test connection to server successful. user: {0}, Server: {1}, Hostname: {2}`r`n" -f $env:USERNAME, $Servidor, (hostname))
}

Full error:

Exception calling "Delete" with "1" argument(s): "0x8 0070005OWSSVR.DLL: (unresolved symbol, module offset=000 000000014A330) at 0x000007FB679BA330 Microsoft.SharePoint.Library.ni.dll: (unresolved symbol, module offset=000000000 00BE9B6) at 0x000007FB6AF0E9B6 Microsoft.SharePoint.ni.dll: (unresolved symbol, module offset=0000000002B0111A) at 0x000007FB7216111A Microsoft.SharePoint.ni.dll: (unresolved symbol, module offset=0000000002BC3FF0) at 0x000007FB72223FF0 Microsoft.SharePoint.ni.dll: (unresolved symbol, module offset=000000000310E5DF) at 0x000007FB7276E5DF Access denied

Powershell functions:

function Add-ItemsToLib($web, [string] $listName, [string] $Path)
{
    $list = $web.Lists[$listName]
    $fileCollection = $list.RootFolder.Files

    $files = get-childItem $Path
    foreach ($file in $files)
    {
        $fn = $file.Name
        $stream = $file.OpenRead()

        $f = $fileCollection | Where { $_.Name -eq $file.Name }
        if ($f -ne $null)
        {
            Write-Host -f Red "UserName: $env:username" `r`n
            $ok = IsCurrentUserAdmin
            Write-Host -f Red "IsCurrentUserAdmin: $ok"  `r`n
            $ok = IsCurrentUserAdmin2
            Write-Host -f Red "IsCurrentUserAdmin2: $ok"  `r`n

            $fileCollection.Delete($file.Name)
        }

        $uploaded = $fileCollection.Add($file.Name, $stream, $TRUE)
        $uploaded.CheckIn("")
        $uploaded.Publish("")
        $uploaded.Approve("")
        if ($stream) {$stream.Dispose()}
    }
}


Function IsCurrentUserAdmin
{
    $ident = [Security.Principal.WindowsIdentity]::GetCurrent() 

    foreach ( $groupIdent in $ident.Groups ) 
    { 
        if ( $groupIdent.IsValidTargetType([Security.Principal.SecurityIdentifier]) ) 
        { 
            $groupSid = $groupIdent.Translate([Security.Principal.SecurityIdentifier]) 
            if ( $groupSid.IsWellKnown("AccountAdministratorSid") -or $groupSid.IsWellKnown("BuiltinAdministratorsSid"))
            { 
                return $TRUE 
            } 
        } 
    } 
    return $FALSE 
} 

Function IsCurrentUserAdmin2
{
    $user = [Security.Principal.WindowsIdentity]::GetCurrent();
    (New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)  
}
1

the files your accessing are on a local or shared drive eg C:/filepath that your looping through each item?

from what i gather its giving you access denied to the file path location your trying to delete from!

goto the folder location (my example its called filepath, so i goto C:/ and right click filepath) right click and select properties... security -> edit -> check to make sure the group account is added there (WSS_ADMIN_WPG) if not add it in with full control. try again and it should work

as Bunzab noted your already looping through the collection and can directly delete the item using $file.delete() rather than $fileCollection.Delete($file.Name) but you would still hit the same permission issue that iv addressed above.

EDIT

Sorry i assumed that you were already running the powershell console as admin! but i presume you want to run it through code? That function is required when your logged in account to windows doesnt have the correct elevated privlages to set dlls on the server that it needs to execute hence why it works when you elevate to admin level, this applies irregardless if the account you used to login is at admin level or not. You can add this code to your script block at the start:

If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{   
$arguments = "& '" + $myinvocation.mycommand.definition + "'"
Start-Process powershell -Verb runAs -ArgumentList $arguments
Break
}

This results in the current script being passed to a new powershell process in Administrator mode (if current User has access to Administrator mode and the script is not launched as Administrator).

https://stackoverflow.com/questions/7690994/powershell-running-a-command-as-administrator

i think if you dont want to add the code above as the account UserinstallerMOss could run many ps scripts than you need to set the account as domain admin to have the correct privlages to execute as admin on powershell! are you sure your account UserinstallerMOss is part of the local admin user group? the group (Administrator), when you run powershell console as admin, its using the local admin group.... if that works than it indicates to me the account your using is not part of that group! are you logged on as UserinstallerMOss on the server and then running the code.

  • Files are in a list in Sharepoint ($list = $web.Lists[$listName], $fileCollection = $list.RootFolder.Files) – Kiquenet Sep 3 '14 at 8:48
  • lol sorry totally missing the line $list.RootFolder.Files will update my answer! – Ali Jafer Sep 3 '14 at 8:50
  • Note: Works if I execute manually using Powershell as Run as Administrator – Kiquenet Sep 3 '14 at 8:53
  • In that case you need to give us a better idea of how you are calling the Powershell script. I guess another question would be why are you doing this in PowerShell and not in your initial app? – Bunzab Sep 3 '14 at 9:08
  • just ammended my answer – Ali Jafer Sep 3 '14 at 9:11
0

Instead of:

$fileCollection.Delete($file.Name)

Try:

$file.delete()
  • Files are in a list in Sharepoint ($list = $web.Lists[$listName], $fileCollection = $list.RootFolder.Files) – Kiquenet Sep 3 '14 at 8:49
  • Finally, I get more information: Works if I use it without Test-Connection cmdlet. I don't know why. In my script, I have Test-Connection cmdlet for any testing. – Kiquenet Sep 8 '14 at 6:19

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.