Trouble creating folder in SharePoint Online (PnP module) - powershell

I am trying to use PowerShell to create a new folder in a Teams document library, using the PnP Sharepoint Online module.
I can authenticate to the site in both PowerShell and in a browser. Then I run this code:
$cred = Get-Credential
$result = Add-PnPFolder -Folder "/sites/SalesDemo/Shared Documents/General/Acme Corp" -Name "testfolder" -Connection (Connect-PnPOnline -Url 'https://tenant.sharepoint.com' -Credentials $cred)
The result looks fine, showing the name, type, items/size, and last modified time, but when I view the site in SharePoint or in Teams, I do not see "testfolder". However, "testfolder" is returned when I run:
Get-PnPFolderItem -FolderSiteRelativeUrl "/sites/SalesDemo/Shared Documents/General/Acme Corp" -ItemType Folder -Connection (Connect-PnPOnline -Url 'https://tenant.sharepoint.com' -Credentials $cred)
Unfortunately, there are other folders in "Acme Corp" that are not returned.
Where is my "testfolder" being created?

Update:
As discussed, when using Connect-PnPOnline, the url parameter should be the same as the site url where the library hosted, if it's hosting in specific site collection, the url should be:
https://Tenant.sharepoint.com/sites/SalesDemo
The testfolder should be located at the relative url:
/sites/SalesDemo/Shared Documents/General/Acme Corp
"Acnm Corp" should be a subfolder within /Shared Documents/General folder, the test folder should be there:

Related

How get users permissions report to all site collections on SharePoint Online using PowerShell

I am looking for a solution on how I could generate a report with users permissions to all site collections on SharePoint Online using PowerShell.
Using the script below, I get all data about the sites. Is it possible to retrieve only the url value of each site as a response from this script?
Connect-SPOService -url "https://...-admin.sharepoint.com" -Credential (Get-credential)
Get-SPOSite -Limit ALL | Export-CSV -LiteralPath C:\Temp\SitesInventory.csv -NoTypeInformation
Below is a fragment of the second script generating the report with users permissions to the specified site collection
...
$SiteURL = "..."
$ReportFile = "C:\Temp\UsersPermissionsReport.csv"
GeneratePnPSitePermissionsReport -SiteURL $SiteURL -ReportFile $ReportFile -Recursive
Using the second script generates a report with users permissions to the specified site collection but I would need to iterate on all sites returned from the first script and generate a report with users permissions for each of them
this link is valid for a single site collection:
link
You can try to loop this in all site collections, like this below:
foreach ($SiteCollection in SPOSite) {do your stuff here}

New-WebApplication fails using network path in physicalpath parameter in powershell

We are trying to create list of applications in a particular site,
For this we have a csv which contains required information for site creation. Below is the format:
Below is the script which we are using to create application in IIS:
foreach($app in $apps){
$appname = $app.path.TrimStart("/")
New-WebApplication -Name $appname -Site $app.Site -PhysicalPath $app.PhysicalPath -ApplicationPool $app.applicationPool -Verbose
}
Problem is application whose code is stored on some network path like \\network\Webapps\appname are giving below error:
New-WebApplication : A parameter cannot be found that matches parameter name 'physicalPath'.
whereas application folder located on same server are being created without any issue,
Have also done test-path \\network\webapps\appname it results in true
What is the issue and how to rectify it?
Issue got resolved, I didn't got what was the exact reason behind that, Have updated my script, basically have assigned values to specific variable than was able to create application.
Updated Script
foreach($app in $apps){
$appname = $app.path.TrimStart("/")
$appSite = $app.Site
$appPath = $app.PhysicalPath
$appPool = $app.applicationPool
New-WebApplication -Name $appname -Site $appSite -PhysicalPath $appPath -ApplicationPool $appPool -Verbose
}
Thanks
First of all, you have to make sure that powershell has permission to access the server corresponding to the network path when creating the site.
When using powershell to create a site, if the physical path of the site is a file in the local disk, you can use New-WebApplication, if it is a UNC path, you should use New-Item.
Under normal circumstances, the UNC path will not be used as the physical path of the main site, but will be added as a virtual directory. Because the domain name of the main site points to the local machine and the UNC points to the remote IP address, the two will conflict because a server error occurred when I created the site through IIS Manager.
$siteName = 'Default Web Site'
$virtualDirectoryName = 'Test'
$physicalPath = '\\UNC-path'
## Init
$virtualDirectoryPath = "IIS:\Sites\$siteName\$virtualDirectoryName"
## Create Virtual Directory where physicalpath is an UNC-path (New-WebVirtualDirectory
wont do)
New-Item $virtualDirectoryPath -type VirtualDirectory -physicalPath $physicalPath
Any chance it's this? That error sounds like it's an issue with the cmdlet and not the data you're feeding it.
https://social.msdn.microsoft.com/Forums/en-US/3e0cb076-6ad4-4b03-bb59-c912174266b0/container-with-iis-cant-create-application-using-powershell-physicalpath-parameter-not-found?forum=windowscontainers
specifically:
You need to import the WebAdministration module. If you're in the container through docker exec simply use:
Import-Module WebAdministration

Programmatically delete the contents of SharePoint 2013 recycle bin

Scenario:
Client would like a PowerShell script wrote to delete the 2nd stage recycle bin for a SharePoint 2013 Site Collection
Client's SharePoint requires a credential authorization on load.
Issue:
I have researched, however in regards to SharePoint 2013, I have yet to find a script example that involves using my credentials. However in the SharePoint Online examples for programmatically deleting a SP recycle bin they use credential authorization.
My attempts to splice these two scripts together have been met with no success.
Question: Would somone be able to assist me by taking a look at my code and letting me know where I am going wrong?
##Variables for Processing
$SiteUrl = "https://MySite/sites/site"
Try {
#Setup the context
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$Context.Credentials = $Credentials
#Get the recycle bin
$Site = $Context.Site
$RecycleBinItems = $Site.RecycleBin
$Context.Load($Site)
$Context.Load($RecycleBinItems)
$Context.ExecuteQuery()
Write-Host "Total Number of Items found Recycle Bin:" $RecycleBinItems.Count
#sharepoint online powershell empty recycle bin
$RecycleBinItems.DeleteAll()
$Context.ExecuteQuery()
}
catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

ASP.NET 5 MVC 6: Publish-AzureWebsiteProject incorrect directory structure

When publishing a ASP.NET 5 MVC 6 application to an Azure Web App using powershell, it seems that the published folder structure is somehow incorrect.
After the publish, the wwwroot is one folder too deep:
If I change the application's virtual directory from site\wwwroot to site\wwwroot\wwwroot, then it works!
First, I build and file publish using MSBuild:
/t:Build,FileSystemPublish /p:PublishConfiguration=$(BuildConfiguration) /p:PublishOutputPathNoTrailingSlash=$(build.stagingDirectory)
Second, publish to Azure Web App using this script and passing in $(build.stagingDirectory) as the $packOutput:
param([String] [Parameter(Mandatory = $true)]$websiteName, [String] [Parameter(Mandatory = $true)]$packOutput)
$SourceFolder = "$packOutput"
[IO.DirectoryInfo] $parentDir = [System.IO.Path]::GetDirectoryName($packOutput)
$DestinationFile = "$parentDir\Publish.zip"
$Compression = "Optimal" # Optimal, Fastest, NoCompression
function Zip-Directory {
Param(
[Parameter(Mandatory=$True)][string]$DestinationFileName,
[Parameter(Mandatory=$True)][string]$SourceDirectory = "",
[Parameter(Mandatory=$False)][string]$CompressionLevel = "Optimal",
[Parameter(Mandatory=$False)][switch]$IncludeParentDir
)
Add-Type -AssemblyName System.IO.Compression.FileSystem
$CompressionLevel = [System.IO.Compression.CompressionLevel]::$CompressionLevel
[System.IO.Compression.ZipFile]::CreateFromDirectory($SourceDirectory, $DestinationFileName, $CompressionLevel, $IncludeParentDir)
}
Zip-Directory -DestinationFileName $DestinationFile `
-SourceDirectory $SourceFolder `
-CompressionLevel $Compression ` #Optional parameter
Move-Item -Path $DestinationFile -Destination $SourceFolder\Publish.zip
Write-Output "Stopping web app..."
Stop-AzureWebsite -Name $websiteName
Write-Output "Publishing web app..."
Publish-AzureWebsiteProject -Name $websiteName -Package $SourceFolder\Publish.zip
Write-Output "Starting web app..."
Start-AzureWebsite -Name $websiteName
I cannot seem to see where this is all going wrong.
Edit: Using DNX beta6
The problem is that your Publish.zip folder will contain two subfolders. That will also be the case if you use the new dnu publish command that comes with .NET 5.
approot
wwwroot
Publish-AzureWebsiteProject publishes the contents of Publish.zip to the Azure Web App's site/wwwroot. What you need is a publish method that does not publish to site/wwwroot but instead publishes to /site, so that you end with the following directory structure in your Azure Web App.
D:\
home
site
approot
wwwroot
An alternative is to push to the Azure Web App using Git or FTP instead of Publish-AzureWebsiteProject. Both give you the control you need and you use both from PowerShell. If you choose the Git route, while it will work out-of-the-box, also check out Project Kudu.
Changing the application's virtual directory from site\wwwroot to site\wwwroot\wwwroot works once. After a next deploy your app's wwwroot folder will be located at
site
wwwroot
wwwroot
wwwroot
As a consequence you have to change the application's virtual directory to site\wwwroot\wwwroot\wwwroot to get any results in your browser. This process repeats itself for every following deployment. You can prevent this by adding a extra virtual directory to the azure website.
In case of an API it would look like this:
/ site\wwwroot
/api site\wwwroot\wwwroot
Publish-AzureWebsiteProject will keep deploying to site\wwwroot and you can access your application in the browser at something.com/api

Create folder with powershell in sharepoint (webservices)

I need to create a folder in sharepoint if it does not already exist. My powershell script is not running on the sharepoint server so I think I have to use the sharepoint web services? I am currently uploading files to sharepoint with powershell using webclient as below - but I need to create the folder for the file first... if it does not already exist;
# Upload the file
$webclient = New-Object System.Net.WebClient
$webclient.Credentials = $credentials
$webclient.UploadFile($destination + "/" + $File.Name, "PUT", $File.FullName)
Is this possible to do with webclient? If not, how can this be done using the sharepoint web services?
Since you mentioned SharePoint Web Services, Lists.UpdateListItems Method could be utilized for that purpose, for example:
Function Create-Folder([string]$WebUrl,[string]$ListUrl,[string]$ListName,[string]$FolderName)
{
$url = $WebUrl + "/_vti_bin/lists.asmx?WSDL"
$listsProxy = New-WebServiceProxy -Uri $url -UseDefaultCredential
$batch = [xml]"<Batch OnError='Continue' RootFolder='$WebUrl/$ListUrl'><Method ID='1' Cmd='New'><Field Name='ID'>New</Field><Field Name='FSObjType'>1</Field><Field Name='BaseName'>$FolderName</Field></Method></Batch>"
$result = $listsProxy.UpdateListItems($ListName, $batch)
}
Usages
Create Orders folder under Documents library:
Create-Folder -WebUrl "http://contoso.intranet.com" -ListUrl "Documents" -ListName "Documents" -FolderName "Orders"
Create 2014 folder in Requests list:
Create-Folder -WebUrl "http://contoso.intranet.com" -ListUrl "Lists/Requests" -ListName "Requests" -FolderName "2014"
Update
If folder already exists then SOAP service will throw the error:
The operation failed because an unexpected error occurred. (Result
Code: 0x8107090d)
but since OnError attribute is set to Continue for Batch Element, PowerShell will continue the execution.