I've written a script so I can access my newly recorded media from a podcast player. The script runs as expected from the command line but when it runs from the task scheduler it is not getting the full path from subfolders. There are multiple errors on every foreach pass with this erroneous path. here is one such error
get-item : Cannot find path 'D:\Radio\202303100400.mp4' because it does not exist.
At C:\Users\jj9wo\mpxtorss.ps1:38 char:23
+ foreach ($folder in ((get-item $media).DirectoryName) | Select-Object ...
+ ~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (D:\Radio\202303100400.mp4:String) [Get-Item], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand
the correct path to the file is D:\Radio\kmox\202303100400.mp4
here is the full script. there is a couple thing that still need work but it is fully functioning except the issue I stated. I know my script favors one-liners and lacks adequate commenting. limiting the critique to my specified problem will be appreciated
$path= 'D:\Radio'
$site='mysite.com'
Set-Location -Path $path
if ($path -and $(Try{Test-Path $path.trim()} Catch{}) -and (((get-item $path).Attributes) -match "Directory")){
$path = Get-Item $path
} else {Write-Host "mp2rss error: Invalid" ;exit}
$media = (Get-ChildItem -path $path -File -Recurse -Filter "*.mp*") | Where-Object `
{$_.extension -in ".mp3",".mp4"} | Sort-Object -Descending -Property LastWriteTime
if (!$media.Count){Write-Host "mp2rss error: No Media Found" ;exit}
# done verifying
function when_long($param){
$param = ($param.AddMinutes(-($param.minute %5)).ToString("h:mmtt ddd, MMM d'th'") -replace
'(?<!1)1th$','1st' -replace'(?<!1)2th$','2nd' -replace '(?<!1)3th$','3rd') -replace
'(?<=:\d\d)AM','a' -replace '(?<=:\d\d)PM','p'
return $param
}
function when_short($param){
$param = ($param.ToString("ddd"),($param.AddMinutes(-($param.minute %5))).ToString("M/dd h:mmt").ToLower() -join " ")
return $param
}
if ((Get-Volume -DriveLetter $path.ToString().Substring(0,1)).FileSystemLabel){
$title_alt = (Get-Volume -DriveLetter $path.ToString().Substring(0,1)).FileSystemLabel
}else{$title_alt = "$env:COMPUTERNAME`($($path.FullName.Substring(0,1))`)"}
if ($path.FullName -eq $path.Root.FullName){
$title = $title_alt
$htm_out = "$($path)mpx.htm"
}else{
$title = $path.BaseName
$htm_out = "$path\mpx.htm"
}
$html = @("<html><head><title>$title</title></head><body>")
foreach ($folder in ((get-item $media).DirectoryName) | Select-Object -Unique){
$ext = @('mp3','mp4')
foreach ($ext in $ext){
if ($media | Where-Object {$_.DirectoryName -in $folder -and $_.Extension -match $ext}){
if ((Get-Item $folder).FullName -ne (Get-Item $folder).Root.FullName){
$title = (Get-Item $folder).BaseName
}else{$title = $title_alt}
if ($ext -match 'mp4'){$title = "$title [V]"}
$url = (([System.Uri]$folder).AbsoluteUri.TrimStart(([System.Uri]($path.ToString())).AbsoluteUri))
if ($url){$url = "$site/$url/"}else{$url = "$site/"}
$qty = (($media | Where-Object {$_.DirectoryName -in $folder -and $_.Extension -match $ext}) | Measure-Object).Count
$whn = (when_long ($media | Where-Object {$_.DirectoryName -in $folder -and $_.Extension -match $ext} | Select-Object -First 1).CreationTime)
$html += "<p><a href=`"http://$url$ext.rss`">$title</a><br>$qty files updated $whn</p>"
$rss = @("<rss><channel><title>$title</title>")
$rss += "<description>$qty files updated $whn</description>"
foreach ($file in ($media | Where-Object {$_.DirectoryName -in $folder -and $_.Extension -match $ext})){
if ($file.BaseName -match "^\d+$"){
if ($file.BaseName -match "^\d{12}$"){
$title = (when_short ([Datetime]::ParseExact($file.BaseName, 'yyyyMMddHHmm', $null)))
$dsc = (when_long ([Datetime]::ParseExact($file.BaseName, 'yyyyMMddHHmm', $null)))
}else{
$title = (when_short $file.CreationTime)
$dsc = (when_long $file.CreationTime)
}
}else{
$title = $file.BaseName
$dsc = (when_long $file.CreationTime)
}
$rss += "<item><title>$title</title>"
$rss += "<description>$dsc</description>"
$rss += "<enclosure url=`"http://$url$([uri]::EscapeDataString($file.Name))`"/></item>"
}
$rss += "</channel></rss>"
$rss_out = "$folder\$ext.rss"
if (-not (Test-Path $rss_out) -or (Compare-Object (Get-Content -Path $rss_out) $rss)){Out-File -InputObject $rss -FilePath $rss_out}
}
}
}
if (-not (Test-Path $htm_out) -or (Compare-Object (Get-Content -Path $htm_out) $html)){Out-File -InputObject $html -FilePath $htm_out}