PowerShell脚本失败

时间:2017-06-06 16:50:45

标签: powershell powershell-v5.0

我正在尝试使用下面的PowerShell脚本与PSv5自动化一些手动过程,但它一直失败,我无法弄清楚原因。当我尝试在ISE中调试它时,我收到以下错误,并且没有生成输出文件:

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

2 个答案:

答案 0 :(得分:0)

这对我有用

# 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

答案 1 :(得分:-1)

已解决

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