我已经把一个脚本放在一起,用新文件更新 Azure 文件共享。它使用本地文件夹来传输文件。当我从Powershell ISE中运行该代码时,该代码完美地运行,但我无法通过调用此文件从常规Powershell控制台中运行它 ./TestScript.ps1
. 任何反馈或建议都会有很大帮助。
以下是从Powershell控制台运行时收到的错误。
Set-AzStorageFileContent : Failed to open file C:\Test\Folder\File1: Illegal characters in path..
At C:\Test\TestScript.ps1:100 char:5
+ Set-AzStorageFileContent -Share $fileShare -Source $file -Path $d ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [Set-AzStorageFileContent], TransferException
+ FullyQualifiedErrorId : TransferException,Microsoft.WindowsAzure.Commands.Storage.File.Cmdlet.SetAzureStorageFileContent
Set-AzStorageFileContent : Failed to open file C:\Test\Folder\File2: Illegal characters in path..
At C:\Test\TestScript.ps1:100 char:5
+ Set-AzStorageFileContent -Share $fileShare -Source $file -Path $d ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [Set-AzStorageFileContent], TransferException
+ FullyQualifiedErrorId : TransferException,Microsoft.WindowsAzure.Commands.Storage.File.Cmdlet.SetAzureStorageFileContent
Set-AzStorageFileContent : Failed to open file C:\Test\Folder\File3: Illegal characters in path..
At C:\Test\TestScript.ps1:100 char:5
+ Set-AzStorageFileContent -Share $fileShare -Source $file -Path $d ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [Set-AzStorageFileContent], TransferException
+ FullyQualifiedErrorId : TransferException,Microsoft.WindowsAzure.Commands.Storage.File.Cmdlet.SetAzureStorageFileContent
以下是我的脚本中的功能。
Function UploadintfFiles ($subscriptionName, $resourceGroupName, $storageAccountName, $directoryName) {
$fileShareName = "FileShare"
Set-Location $Source
Select-AzSubscription -SubscriptionName $subscriptionName
# Get the storage account context
$ctx = (Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccountName).Context
# Get the file share
$fileShare = Get-AZStorageShare -Context $ctx -Name $fileShareName
#Delete old files from the folder
Write-Host -ForegroundColor Red "Deleting old $fileShareName files from file share..."
Get-AzStorageFile -ShareName $fileShareName -Path $directoryName -Context $ctx | Get-AzStorageFile | where { ($_.Name -like "$filePattern1") } | Remove-AzStorageFile
Get-AzStorageFile -ShareName $fileShareName -Path $directoryName -Context $ctx | Get-AzStorageFile | where { ($_.Name -like "$filePattern2") } | Remove-AzStorageFile
Get-AzStorageFile -ShareName $fileShareName -Path $directoryName -Context $ctx | Get-AzStorageFile | where { ($_.Name -like "$filePattern3") } | Remove-AzStorageFile
Write-Host -ForegroundColor Red "File Deletion Complete.`n"
# Upload new files to file share
Write-Host -ForegroundColor Green "Uploading new $fileShareName files to file share..."
$files = Get-ChildItem -Path $Source -Recurse
foreach ($file in $files) {
## Upload the file
Set-AzStorageFileContent -Share $fileShare -Source $file -Path $directoryName -Force
}
Write-Host -ForegroundColor Green "File Upload Complete.`n"
}
不知道为什么,但我改变了一点与你的代码,它可以工作在PS控制台和ISE。
.ps1文件中的代码。
param(
$resourceGroupName,
$storageAccountName,
$directoryName
)
Function UploadintfFiles ($resourceGroupName, $storageAccountName, $directoryName) {
$fileShareName = "aaa"
$Source="C:\Test\Folder"
Set-Location $Source
# Get the storage account context
$ctx = (Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccountName).Context
# Get the file share
$fileShare = Get-AZStorageShare -Context $ctx -Name $fileShareName
#I didn't use the delete code here, so comment them out
#Delete old files from the folder
#Write-Host -ForegroundColor Red "Deleting old $fileShareName files from file share..."
#Get-AzStorageFile -ShareName $fileShareName -Path $directoryName -Context $ctx | Get-AzStorageFile | where { ($_.Name -like "$filePattern1") } | Remove-AzStorageFile
#Get-AzStorageFile -ShareName $fileShareName -Path $directoryName -Context $ctx | Get-AzStorageFile | where { ($_.Name -like "$filePattern2") } | Remove-AzStorageFile
#Get-AzStorageFile -ShareName $fileShareName -Path $directoryName -Context $ctx | Get-AzStorageFile | where { ($_.Name -like "$filePattern3") } | Remove-AzStorageFile
#Write-Host -ForegroundColor Red "File Deletion Complete.`n"
# Upload new files to file share
Write-Host -ForegroundColor Green "Uploading new $fileShareName files to file share..."
#here, I just list all the files.
$files = Get-ChildItem -Path $Source -Recurse -file
foreach ($file in $files) {
## Upload the file
Set-AzStorageFileContent -ShareName $fileShareName -Source $file.fullname -Path $directoryName -Force -Context $ctx
}
Write-Host -ForegroundColor Green "File Upload Complete.`n"
}
UploadintfFiles -resourceGroupName $resourceGroupName -storageAccountName $storageAccountName -directoryName $directoryName
请告诉我上面的代码是否能解决你的问题。
我们也遇到过同样的问题。无论我们对代码做了什么修改,都是一直碰到这个问题。
我们在这里找到的解决方案-------------------------------------------------------------。Set-AzStorageBlobContent 抛出异常。路径中出现非法字符就是在powershell目录下引入Powershell.exe.config文件,内容如下。
<configuration>
<runtime>
<AppContextSwitchOverrides value="Switch.System.IO.UseLegacyPathHandling=false" />
</runtime>
</configuration>