【发布时间】:2020-05-12 12:47:01
【问题描述】:
我已经编写了一个脚本,它使用新文件更新 Azure 文件共享。它使用本地文件夹来传输文件。当我从 Powershell ISE 运行此代码时,它运行完美,但我无法通过调用此文件 ./TestScript.ps1 从常规 Powershell 控制台运行它。任何反馈或建议都会有很大帮助。
以下是从 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"
}
【问题讨论】:
-
使用
-Source $file.FullName而不是-Source $file。 $file 是 FileInfo 对象,而不是字符串。还要检查$directoryName持有什么。也许 cmdlet 也在抱怨这一点。还向我们展示您如何调用UploadintfFiles以及如何将参数发送到脚本。 -
Set-Location $Source将不起作用,因为此时$Source未定义。 -
感谢@Theo @Thomas 根据您的建议,我已更新代码以使用
-Source $file.FullName这样做,我可以消除Source $file.FullName以下是函数的调用方式:@ 987654337@ 这些变量值来自 CSV 文件,我已验证它们是否正确传递。令人惊讶的是,该脚本仍然可以在 ISE 上运行。 -
我设法从 PS 控制台捕获了运行错误,该错误现在附加到原始帖子中。
标签: powershell azure-powershell powershell-ise