【发布时间】:2020-11-09 18:47:45
【问题描述】:
我正在使用 5.1 版的 Windows Powershell ISE 从目录中读取文件,然后希望将这些文件名添加到 Enum 以供以后使用。我找到了一种方法,通过 HereString 传入 C# 代码。
#where the files would be read
Param([Parameter(Mandatory=$True, Position=0)][ValidateNotNullOrEmpty()][String]$path)
#search for files in the path directory
$files = Get-ChildItem $path -Name
$HereString = @"
public enum Files
{
$(
foreach($file in $files){$file}
{
"$file,"
}
)
}
"@
Add-Type -TypeDefinition $HereString
如果我不尝试传入文件名(通过声明一个简单的 Enum),则代码可以正常工作。但是这段代码会出现以下错误:
Add-Type : c:\Users\HPC\AppData\Local\Temp\1n43jzgm.0.cs(3) : } expected
c:\Users\HPC\AppData\Local\Temp\1n43jzgm.0.cs(2) : {
c:\Users\HPC\AppData\Local\Temp\1n43jzgm.0.cs(3) : >>> delete_maybe.txt, hello_world.txt, test.txt ,
c:\Users\HPC\AppData\Local\Temp\1n43jzgm.0.cs(4) : }
At c:\Users\HPC\Documents\Task Scripts\grab_sql_scripts.ps1:35 char:1
+ Add-Type -TypeDefinition $HereString
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (Microsoft.Power...peCompilerError:AddTypeCompilerError) [Add-Type], Exception
+ FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand
Add-Type : c:\Users\HPC\AppData\Local\Temp\1n43jzgm.0.cs(4) : Type or namespace definition, or end-of-file expected
c:\Users\HPC\AppData\Local\Temp\1n43jzgm.0.cs(3) : delete_maybe.txt, hello_world.txt, test.txt ,
c:\Users\HPC\AppData\Local\Temp\1n43jzgm.0.cs(4) : >>> }
At c:\Users\HPC\Documents\Task Scripts\grab_sql_scripts.ps1:35 char:1
+ Add-Type -TypeDefinition $HereString
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (Microsoft.Power...peCompilerError:AddTypeCompilerError) [Add-Type], Exception
+ FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand
Add-Type : Cannot add type. Compilation errors occurred.
At c:\Users\HPC\Documents\Task Scripts\grab_sql_scripts.ps1:35 char:1
+ Add-Type -TypeDefinition $HereString
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Add-Type], InvalidOperationException
+ FullyQualifiedErrorId : COMPILER_ERRORS,Microsoft.PowerShell.Commands.AddTypeCommand
我在网上找不到很多关于这个特定问题的文档。有谁知道这里发生了什么?如果没有,有没有人知道另一种方法来尝试我想要实现的目标?
谢谢!
【问题讨论】:
-
将文件名更改为驼峰式大小写导致同样的错误。
-
不是大小写,而是
.'s -
什么“点”!?我在上面的示例 sn-p 中没有看到任何点!
标签: c# windows powershell