Showing posts with label error. Show all posts
Showing posts with label error. Show all posts
Monday, December 16, 2013
Pass arguments to powershell in batch file
Sometimes you want to run Powershell script without .ps1 files (directly from batch file).
My last post was about that but now I'll show you how to pass arguments to your scriptblock.
It is relatively easy but not easily found from internet.
Here goes:
powershell -Command "& { Write-host $args[0] }" "ARGUMENTS_HERE"
The output should be "ARGUMENTS_HERE".
Real life example:
I needed this for simple script that is fired from another batch file with arguments of files, that need to be scanned for lines. If lines are found, e-mail is sent.
powershell -Command "& { Get-Childitem -Path $args[0] -Filter $args[1] | foreach { $len = get-content $_.FullName | measure-object -char; if ($len.Characters -gt 0) { Send-MailMessage -SmtpServer 'MAILSERVER HERE' -From 'SENDER HERE' -To 'RECEIVER HERE' -Subject $( 'Errors in log: ' + $($_.Name) ) } } }" "PATH HERE" "FILTER HERE"
It has been long since I posted anything here, I should make a better use for this blog as I search for my own code snippets pretty often :)
Merry Christmas!
Tuesday, December 18, 2012
Run powershell in windows batch file
I needed to run a powershell script in batch file so it would be executed as one line of commands, without requiring to run .ps1 file directly.
The task is fairly simple, I've done this before. But this time I ran into another problem, because the need of using " signs inside " signs that were actually around -command argument.
This leaded me to a problem that I couldn't use escape chars for " because it would escape it from script altogether.
I couldn't use ' signs either, because that would turn variable names into direct strings.
Here is what I had at start (works within powershell):
$date = Get-Content '.\card_dates.txt' | Select-Object -last 1;
Get-ChildItem '.\sql' -Exclude '*.parsed' | ForEach-Object ($_) {
Get-Content $_.FullName | Foreach-Object {
$_ -replace '#PARAM_1', $date
} | Set-Content "$($_.FullName).parsed"
}
Here is the modification that gave me the error (if running from batch):
powershell -Command "& { $date = Get-Content '.\card_dates.txt' | Select-Object -last 1; Get-ChildItem '.\sql' -Exclude '*.parsed' | ForEach-Object ($_) { Get-Content $_.FullName | Foreach-Object {$_ -replace '#PARAM_1', $date} | Set-Content "$($_.FullName).parsed" } } "
This is the error:
Set-Content : Cannot bind argument to parameter 'Path' because it is null.
At line:1 char:222
+ ... | Set-Content $($_.FullName).parsed } }
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Set-Content], ParameterBinding
ValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,M
icrosoft.PowerShell.Commands.SetContentCommand
So what did I change to make it work?
The solution was actually really simple.
Instead of using:
"$($_.FullName).parsed"
I had to use:
$($($_.FullName) + '.parsed')
And here is the solution that works directly from commandline
powershell -Command "& { $date = Get-Content '.\card_dates.txt' | Select-Object -last 1; Get-ChildItem '.\sql' -Exclude '*.parsed' | ForEach-Object ($_) { Get-Content $_.FullName | Foreach-Object {$_ -replace '#PARAM_1', $date} | Set-Content $($($_.FullName) + '.parsed') } } "
What this script does?
The task is fairly simple, I've done this before. But this time I ran into another problem, because the need of using " signs inside " signs that were actually around -command argument.
This leaded me to a problem that I couldn't use escape chars for " because it would escape it from script altogether.
I couldn't use ' signs either, because that would turn variable names into direct strings.
Here is what I had at start (works within powershell):
$date = Get-Content '.\card_dates.txt' | Select-Object -last 1;
Get-ChildItem '.\sql' -Exclude '*.parsed' | ForEach-Object ($_) {
Get-Content $_.FullName | Foreach-Object {
$_ -replace '#PARAM_1', $date
} | Set-Content "$($_.FullName).parsed"
}
Here is the modification that gave me the error (if running from batch):
powershell -Command "& { $date = Get-Content '.\card_dates.txt' | Select-Object -last 1; Get-ChildItem '.\sql' -Exclude '*.parsed' | ForEach-Object ($_) { Get-Content $_.FullName | Foreach-Object {$_ -replace '#PARAM_1', $date} | Set-Content "$($_.FullName).parsed" } } "
This is the error:
Set-Content : Cannot bind argument to parameter 'Path' because it is null.
At line:1 char:222
+ ... | Set-Content $($_.FullName).parsed } }
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Set-Content], ParameterBinding
ValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,M
icrosoft.PowerShell.Commands.SetContentCommand
So what did I change to make it work?
The solution was actually really simple.
Instead of using:
"$($_.FullName).parsed"
I had to use:
$($($_.FullName) + '.parsed')
And here is the solution that works directly from commandline
powershell -Command "& { $date = Get-Content '.\card_dates.txt' | Select-Object -last 1; Get-ChildItem '.\sql' -Exclude '*.parsed' | ForEach-Object ($_) { Get-Content $_.FullName | Foreach-Object {$_ -replace '#PARAM_1', $date} | Set-Content $($($_.FullName) + '.parsed') } } "
What this script does?
- Read last line from textfile (it is populated there by other scripts)
- Read all SQL files inside SQL folder
- Withing every file, change #PARAM_1 to be the last line from textfile
- Save the file with .parsed extension
Basically we have templates for SQL sentences and historically all the jobs run as BATCH files.
I'm better with Powershell so I wanted to do it in powershell but at the same time keep the old BATCH-es.
Hopefully this information is a bit helpful for you, I spent quite some time to figure out the simple solution.
I'm better with Powershell so I wanted to do it in powershell but at the same time keep the old BATCH-es.
Hopefully this information is a bit helpful for you, I spent quite some time to figure out the simple solution.
Subscribe to:
Comments (Atom)