I'm using arguments for a PowerShell script, but I found that if the second one isn't set, it's getting garbage, although it's possible I'm doing something wrong, since I'm having trouble finding examples with two passed in arguments. How can I verify that both are set?
This script is called from a very old perl script as shown:
#`"C:\\Program Files\\PowerShell\\7\\pwsh.exe" -NoProfile -noninteractive -ExecutionPolicy bypass -Command "\\\\wserverp01\\kiosksupport\\Retailer_DeviceSN\\MoveSNToSprdsht_1.0.ps1 $service_report_xls"`;
In my script, MoveSNToSprdsht_1.0.ps1:
$out_pth = ""
$outFilePathExcel_pl = ""
$outFilePathExcel_pl = $Args[0]
$out_pth = $Args[1] #this is blob scan output dir that we look for correct date file in
Write-Host "new tab will go to:$($outFilePathExcel_pl)"
Write-Host "Will get files from:$($out_pth)" #this had garbage in it, like terminal output from something I ran yesterday that's unrelated
I was thinking maybe I'm not supposed to use $Args1, and found this example, but it doesn't say what to do in the script with -arg1 and -arg2. I'm using VisualStudioCode to run the PowerShell to test it for now so it's possible I'm executing it from the VSC command line incorrectly as well. When I used the pasted perl line from the old perl script, it worked ok with the first parameter but I've added the second parameter now.
I haven't found in a search how to verify the script arguments are actually set. I tried checking against empty string and that didn't work. Something had filled Arg1 with the garbage so it failed the check for empty string.
Update:
So if I used cmdletbinding, would this be good program structure, and would the program be run the normal way?
[cmdletbinding()]
Param ( [Parameter(Mandatory)][string]$outFilePathExcel_pl, [Parameter(Mandatory)][string]$out_pth="\\wserverp01\support\Retailer_SN\OutputSprdsht\")
#and the rest of the program...
Get-ChildItem $out_pth | Foreach-Object {$lastupdatetime=$_.LastWriteTime;$nowtime = get-date; if (($nowtime - $lastupdatetime).totalhours -le 72) {write-host "here2";$excel_File_from = $_.Name;write-host $_.name}}
..
Update2: Hopefully I'm calling the powershell script correctly, below, per comments.
my $resultPS = `"C:\\Program Files\\PowerShell\\7\\pwsh.exe" -NoProfile -noninteractive -ExecutionPolicy bypass -File "\\\\wserverp01\\support\\Retailer_SN\\MoveSNToSprdsht_1.0.ps1" "-outFilePathExcel_pl $service_report_xls" "-out_pth $cvs_blob_scan_dir"`;
Calling updated powershell script:
[cmdletbinding()]
param( [Parameter(Mandatory)][string]$outFilePathExcel_pl, [Parameter()][string]$out_pth="\\wserverp01\support\Retailer_SN\OutputSprdsht\")
Start-Transcript -path '\\wserverp01\support\Retailer_SN\Logs\MoveSNToSprdsht.log'
#get file location from parameter so put sn tab there
Write-Host "new tab will go to:$($outFilePathExcel_pl)"
Write-Host "Will get scan files from:$($out_pth)"
#make sure params are valid paths; non-terminating error if invalid
if(Test-Path $outFilePathExcel_pl)
{
Write-Host "path for $outFilePathExcel_pl looks good"
}
else
{
Write-Error "path for $outFilePathExcel_pl invalid"
}
if(Test-Path $out_pth)
{
Write-Host "path for $out_pth looks good"
}
else
{
Write-Error "path for $outFilePathExcel_pl invalid"
}
Get-ChildItem $out_pth | Foreach-Object {$lastupdatetime=$_.LastWriteTime;$nowtime = get-date; if (($nowtime - $lastupdatetime).totalhours -le 72) {write-host "here2";$excel_File_from = $_.Name;write-host $_.name}}
Update3:
For some reason when I use the named parameters from perl and use them from the powershell, I'm getting
Cannot bind argument to parameter 'Path' because it is an empty string.
when I try to use the variable. Is there a problem with my syntax?
from perl:
my $resultPS = `"C:\\Program Files\\PowerShell\\7\\pwsh.exe" -NoProfile -noninteractive -ExecutionPolicy bypass -File "\\\\wserverp01\\support\\Retailer_SN\\MoveSNToSprdsht_1.0.ps1" -outFilePathExcel_pl "$service_report_xls" -out_pth "$retailer_blob_scan_dir"`;
(where the variables are set from an ini file and passed to the script as the parameters, printout before the powershell call look good in perl)
Then in MoveSNToSprdsht_1.0.ps1:
[cmdletbinding()]
param( [Parameter(Mandatory)][string]$outFilePathExcel_pl, [Parameter()][string]$out_pth="\\wserverp01\support\Retailer_SN\OutputSprdsht\")
Start-Transcript -path '\\wserverp01\support\Retailer_SN\Logs\MoveSNToSprdsht.log'
#get file location from parameter so put sn tab there
Write-Host "new tab will go to:$($outFilePathExcel_pl)" #need to use below instead of $outFilePathExcel############################
Write-Host "Will get blob scan files from:$($out_pth)"
#make sure params are valid paths; non-terminating error if invalid
if(Test-Path $outFilePathExcel_pl)
{
Write-Host "path for $outFilePathExcel_pl looks good"
}
else
{
Write-Error "path for $outFilePathExcel_pl invalid"
}
if(Test-Path $out_pth)
{
Write-Host "path for $out_pth looks good"
}
else
{
Write-Error "path for $out_pth invalid"
}
[System.String] $excel_File_from = ""
############## hours - 72 hours is ok for now so we don't cut it too close for file timestamp for scan..scan run on weekend, use Monday:
Get-ChildItem $out_pth | Foreach-Object {$lastupdatetime=$_.LastWriteTime;$nowtime = get-date; if (($nowtime - $lastupdatetime).totalhours -le 72) {write-host "here2";$excel_File_from = $_.Name;write-host $_.name}}
Maybe I need curly brackets below the cmdlet binding? It the error is complaining about using the empty parameter, not wrong formatting though.