提问人:Michele 提问时间:3/9/2023 最后编辑:brian d foyMichele 更新时间:3/10/2023 访问量:71
使用 -File 而不是 -Command 运行脚本时,Get-ChildItem 找不到目录
Get-ChildItem can't find dir when script run with -File instead of -Command
问:
我不得不将我的脚本更改为使用 -File 而不是 -Command,它曾经使用 GetChildItem 查找目录,但现在找不到它。该脚本是从旧的 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"`;
我已经记录并验证了变量是否包含它们在 perl 脚本中应该包含的内容。我曾经在没有传递参数的情况下使用 -Command,但现在需要将 xls 和 dir 作为参数传递,因此将其更改为 -File。
在 powershell 脚本中,我正在记录我的参数,它们看起来不错,除了 $retailer_blob_scan_dir 的尾随 / 被删除,而它曾经在 ps 脚本中定义的字符串中:
[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'
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
if('' -eq $out_pth)
{
Write-Host "out_pth was blank so setting it"
$out_pth = "\\wserverp01\support\Retailer_SN\OutputSprdsht\"
}
if('' -eq $outFilePathExcel_pl)
{
Write-Host "outFilePathExcel_pl was blank so setting it"
$outFilePathExcel_pl = "\\WserverP01\support\path_Summary\Retailer_ServicePropertiesReport.xlsx"
}
[System.String] $excel_File_from = ""
############## hours - 72 hours is ok for now so we don't cut it too close for file timestamp for blob scan..blob 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}}
由于out_pth中的文件是周日创建的,因此我对其进行了编辑,因此日期现在是资源管理器中的今天。所以是在 72 小时内。Get-ChildItem 中的错误是
Get-ChildItem:\wserverp01\support\Retailer_SN\MoveSNToSprdsht_1.0.ps1:63 生产线 | 63 |Get-ChildItem $out_pth |foreach对象 {$lastupdatetime=$_。最后写... |~~~~~~~~~~~~~~~~~~~~~~ |找不到路径“\WserverP01\support\Retailer_SN\OutputSprdsht”',因为它不存在。
它曾经使用 Get-ChildItem 找到这个目录,我唯一能想到的是尾部斜杠,或者使用 File 而不是 Command 调用,这样我就可以在运行它时使用参数。他们还更改了它,以便他们希望在周末out_pth生成文件,因此我将 Get-ChildItem 中使用的时间从 12 小时更改为 72 小时。在运行 perl 脚本期间,系统调度程序中的权限未更改。有关此问题可能是什么以及如何解决它的任何信息都会很棒。
答:
看起来我的问题是在对 MoveSNToSprdsht powershell 脚本的 perl 调用中 $retailer_blob_scan_dir 周围的双引号。当我在 powershell 脚本中打印出来时,我也看到双引号。
当我删除 perl 中的双引号时,prolbem 消失了,找到了目录:
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`;
评论