【问题标题】:Script only runs in PS ISE but errors out in PS console脚本仅在 PS ISE 中运行,但在 PS 控制台中出现错误
【发布时间】: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


【解决方案1】:

不知道为什么,但我对你的代码做了一些改动,它可以在 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

如果上面的代码可以解决您的问题,请告诉我。

【讨论】:

  • 感谢 Ivan Yang 的意见。我已经对我的脚本进行了建议的更改,但它仍然无法在 ps 控制台上运行。它继续在 ISE 中正常工作。我想我会继续从 ISE 运行这个脚本。我会让我的几个朋友从他们的机器上运行它,看看他们是否得到不同的结果。
  • @milan21984,能附上ps控制台的截图吗?什么是路径源文件夹?它是在 windows 还是 linux 上?
  • 脚本正在 Windows 机器上执行。我已将控制台屏幕截图添加到原始帖子中。
  • @milan21984,这真的很奇怪。您可以尝试在另一台机器上运行它。
【解决方案2】:

我们也遇到过同样的问题。无论我们对代码做了什么更改,我们一直在解决这个问题。

我们在这里找到的解决方案 - Set-AzStorageBlobContent throws exception: Illegal characters in path 就是在powershell目录下引入了Powershell.exe.config文件,内容如下:

<configuration>
  <runtime>
    <AppContextSwitchOverrides value="Switch.System.IO.UseLegacyPathHandling=false" />
  </runtime>
</configuration>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-22
    • 2014-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-29
    • 1970-01-01
    相关资源
    最近更新 更多