## Declaire Variables
## File Path Variables
$savePath = $env:TEMP
$sourceFileAccellerator = "C:\ALL_SALES_WITH_uuid_07-09-2014.txt" ##Path to Source File goes here
$sourceFileLineBreaks = $($savePath + "NifaLeadsListCategoriesWLineBreaks.txt")
$categoryOutput = $($savePath + "CategoryParsedforPowerShell.txt")
$categoryOutputXtraSlash = $($savePath + "CategoriesXtraSlash.txt")
$categoryOutputNoNull = $($savePath + "CategoriesNoNULL.txt")
$searchedFile = $($savePath + $exactSearchTerm + "Temp.txt")
$uuidOutput = $($savePath + $exactSearchTerm + "uuid.SQL")
$uuidColumnSQL = $($savePath + $exactSearchTerm + "Table.sql")
## .sql file creation strings
$sqlUpdate = $("UPDATE ADC.dbo.Contacts SET [" + $exactSearchTerm + "] = 1 WHERE NifaID = '")
$columnAddLine1 = "ALTER TABLE ADC.dbo.Contacts"
$columnAdd = $("ADD [" + $exactSearchTerm + "] nvarchar(10)")
## Regex Variables
$uuidRegEx = '[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}'
$categoryRegEX = '(?<=\\)[^\\\r\n]*\\[^\\\r\n]*$'
## Get the original text file and add line breaks to the categories and output to a new file.
Get-Content $sourceFileAccellerator |% {$_-replace "`t","`n"} > $sourceFileLineBreaks
## Look for Categories and pars the last 2 categories
select-string -Path $sourceFileLineBreaks -Pattern $categoryRegEX -AllMatches | % { $_.Matches } | % { $_.Value } > $categoryOutput
## Cleanup the Categories for input into the ForEach loop.
$job1 = Start-Job { Get-Content $categoryOutput |% {$_-replace "\\\\", "\\\\\\\\"} > $categoryOutputXtraSlash }
Wait-Job $job1
Receive-Job $job1
$job2 = Start-Job { Get-Content $categoryOutputXtraSlash |% {$_-replace '/(?:(?:\r\n|\r|\n)\s*){2}/s', "\n\n"} > $categoryOutputNoNull }
Wait-Job $job2
Receive-Job $job2
## Import the SQL Cmdlets
Import-Module SQLPS
## Load the searchFile and run the script on each column
$SearchFile = New-Object System.IO.StreamReader -Arg $categoryOutputNoNull
while ($line = $file.ReadLine()) {
Select-String -Path $sourceFile -Pattern $line > $searchedFile
Select-String -Path $searchedFile -Pattern $uuidRegEx -AllMatches | % { $_.Matches } | % { $sqlUpdate + $_.Value + "';"} > $uuidOutput
## Create the sql script to add a column
Add-Content -Path $uuidColumnSQL -Value $($columnAddLine1 + " " + $columnAdd)
## Run the SQL scripts
Invoke-Sqlcmd -InputFile $uuidColumnSQL -OutputSqlErrors $true -ServerInstance DUMPSTER\DB_DEFAULT -QueryTimeout 0
Invoke-Sqlcmd -InputFile $uuidOutput -OutputSqlErrors $true -ServerInstance DUMPSTER\DB_DEFAULT -QueryTimeout 0
## Remove Temporary files
Remove-Item $searchedFile
Remove-Item $uuidOutput
Remove-Item $uuidColumnSQL
}
$file.close()
Remove-Item $categoryOutput
Remove-Item $sourceFileLineBreaks
Remove-Item $categoryOutputXtraSlash
我正在研究上面的脚本,该脚本从制表符分隔的.txt文件中解析数据,并测试了脚本的每个组件并取得了成功,但是当它们组合成while循环时,Powershell正在抛出以下错误:
输出:
Id Name PSJobTypeName State HasMoreData Location Command
-- ---- ------------- ----- ----------- -------- -------
6 Job6 BackgroundJob Completed True localhost Get-Content $category...
Cannot bind argument to parameter 'Path' because it is null.
+ CategoryInfo : InvalidData: (:) [Get-Content], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetContentCommand
+ PSComputerName : localhost
8 Job8 BackgroundJob Completed True localhost Get-Content $category...
Cannot bind argument to parameter 'Path' because it is null.
+ CategoryInfo : InvalidData: (:) [Get-Content], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetContentCommand
+ PSComputerName : localhost
You cannot call a method on a null-valued expression.
At C:\CategoryParseAndSQLUpdate.ps1:53 char:8
+ while ($line = $file.ReadLine()) {
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At C:\CategoryParseAndSQLUpdate.ps1:66 char:1
+ $file.close()
+ ~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
答案 0 :(得分:1)
当您使用作业时(例如使用Start-Job
cmdlet),您必须仔细查看变量的范围。根据版本:您可以:
$using:VariableName
(v.3 +)ArgumentList
中的Start-Job
参数和脚本块内的param()
块修改强>
刚刚注意到:我看不到你在哪里定义$file
- 看起来好像缺少了?