PowerShell脚本失败

问题描述:

我想用PSv5下面的PowerShell脚本来自动化一些手动过程,但它保持失败,我不明白为什么。当我尝试调试它在ISE,我得到的是生成以下错误,并没有输出文件:PowerShell脚本失败

Get-Content : Cannot bind argument to parameter 'Path' because it is null. 
At line:15 char:53 
+ $HeaderLine, ${$DestinationFile} = Get-Content -Path <<<< $SourceFile.FullName 
+ CategoryInfo   : InvalidData: (:) [Get-Content], ParameterBindingValidationException 
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetContentCommand 
# BEGIN SCRIPT 
# 
# Collect all the files in the directory 
$SoureFiles = Get-ChildItem -Path 'C:\raw' -Filter '*.txt' 

# Create the destination file 
$DestinationFile = (New-Item -Path 'c:\done' ` 
-Name ('CombinedSources-' + (Get-Date -Format 'ddMMMyyyy') + '.txt') ` 
-ItemType File -Force).FullName 
$DestinationFile 

# Loop source, files, remove header line, append to the destination file 
ForEach ($SourceFile in $SoureFiles) 
{ 
$HeaderLine, ${$DestinationFile} = Get-Content -Path $SourceFile.FullName 
${$DestinationFile}` 
| Out-File -FilePath $DestinationFile -Append 
} 

# Sort the Destination file content and replace with sorted unique lines 
(Get-Content -Path $DestinationFile ` 
| Sort-Object { [string]$_.split(',')[1,2] }) ` 
| Get-Unique ` 
| Out-File -FilePath $DestinationFile -Force 
# 
#END SCRIPT 
+0

它在哪里失败?你有什么尝试? – Nick

+0

它是如何失败?你可以发布输出吗? – PrestonM

+0

请[编辑你的问题](https://*.com/posts/44395657/edit)包括你得到的实际错误信息。 – sodawillow

这为我工作

# BEGIN SCRIPT 
# 
# Collect all the files in the directory 
$SoureFiles = Get-ChildItem -Path 'C:\raw' -Filter '*.txt' 

# Create the destination file 
$DestinationFile = (New-Item -Path 'c:\done' -Name ('CombinedSources-' + (Get-Date -Format 'ddMMMyyyy') + '.txt') -ItemType File -Force).FullName 


# Loop source, files, remove header line, append to the destination file 
ForEach ($SourceFile in $SoureFiles) 
{ 
    Get-Content -Path $SourceFile.FullName | Out-File -FilePath $DestinationFile -Append 
} 

(Get-Content -Path $DestinationFile | Sort-Object { [string]$_.split(',')[1,2] }) | Get-Unique | Out-File -FilePath $DestinationFile -Force 

解决

的问题我的PowerShell版本。该脚本是在运行PSv5.1的Windows 10系统上编写的。我从来没有想过在我的Windows 7工作系统(PSv2)上检查PS的版本。一旦我升级我的工作系统上的PS版本,脚本工作完美...