0
votes

I have a Windows 2016 Server with Powershell 5.0 installed on it. I'm trying to create a folder on a network file share (which is also on the same server, but I access the file share with \server\backup instead of D:\backup) with Powershell command below.

However the code below occasionally throw errors.

System.ArgumentException: Invalid Path: '\\server\backup\PSBackup\newFolder'.
   at Microsoft.SqlServer.Management.PowerShell.SqlPath..ctor(String path)
   at Microsoft.SqlServer.Management.PowerShell.SqlServerProvider.NewItem(String path, String type, Object newItem)
   at System.Management.Automation.SessionStateInternal.NewItemPrivate(CmdletProvider providerInstance, String path, String type, Object content, CmdletProviderContext context)

I have a powershell script which creates folders in an order, the same code creates the first folder, but throws error for the second folder creation.

Did you ever face such a problem when creating folder on a network share? By the way the same powershell script works without any problem on my dev machine (Windows 2012 R2) and Windows 2008 R2 servers.

$rootFolder = "\\server\backup\PSBackup"
$folderName = "newFolder"

$createdDirectory = New-Item -Path $rootFolder -ItemType "directory" -Name $folderName
1
What happens when you do like $createdDirectory = New-Item -LiteralPath (Join-Path -Path $rootFolder -ChildPath $folderName) -ItemType Directory ? - Theo
Does the path have any square brackets? [ ] - js2010
@js2010 no path doesn't have brackets. - serkanz
@Theo no LiteralPath also didn't work. Actually my code above works most of the time, but throws errors for another run. - serkanz

1 Answers

0
votes

When Path parameter is changed "Microsoft.PowerShell.Core\FileSystem::$($rootFolder)" instead of $rootFolder, code worked as expected.

There should be a problem with passing remote folder as parameter.

$createdDirectory = New-Item -Path "Microsoft.PowerShell.Core\FileSystem::$($rootFolder)" -ItemType "directory" -Name $folderName