Jump to content

Trouble making a powershell script


morejava
 Share

Recommended Posts

Hey all!  I'm trying to figure out how to create a powershell script that would run the following.  Any help would be super appreciated!

 

Set-Location $env:USERPROFILE\Desktop\VideoPull\
$videoDir = Get-ChildItem | where {$_.Attributes -eq 'Directory'} | Select-Object FullName
Write-Output "Switching to Event Folder:" + $videoDir.FullName
Set-Location $videoDir.FullName
$a = [regex]"f-(\d{1,4}).ts"
$segmentFolders = Get-ChildItem | where {$_.Attributes -eq 'Directory'}
$segmentFolders | ForEach-Object {
    Write-Output "Switching to Segment Folder: " + $_.FullName
     Set-Location $_.FullName
     $segmentName = $_.Name
     $text = ''
     $text | Set-Content 'mylist.txt'
     $fragments = Get-ChildItem -Include @("*.ts") -Path $_.FullName -Recurse
     $fragments | ForEach-Object {
     $b = $a.Match($_.FullName)
     $order = [int]$b.Groups[1].Value
     Write-Output "Fragment # is: $order"
     $_ | Add-Member -MemberType NoteProperty Order $order
     }
     $fragments | Sort-Object Order | ForEach-Object{
         $fragText = 'file ''' + $_.BaseName + '.ts'''
         Write-Output $fragText
         $fragText | Add-Content 'mylist.txt'
     }
     Write-Output "Segment name is $segmentName"
     ..\..\ffmpeg.exe -f concat -i mylist.txt -c copy ..\$segmentName.ts
}

 

Link to comment
Share on other sites

I tried taking a crack at it with this....

#RequireAdmin

Local $sCommand = 'PowerShell.exe '
;~  Reinstall and Re-Register All Windows Apps for All Accounts
;~  $sCommand &= 'Get-AppXPackage -AllUsers | Foreach {Add-AppxPackage -DisableDevelopmentMode -Register "$($_.InstallLocation)\AppXManifest.xml"}'
;~  Using the following for testing purposes only
    $sCommand &= 'Set-Location $env:USERPROFILE\Desktop\VideoPull\'
    $sCommand &= '$videoDir = Get-ChildItem | where {$_.Attributes -eq 'Directory'} | Select-Object FullName'
    $sCommand &= 'Write-Output "Switching to Event Folder:" + $videoDir.FullName'
    $sCommand &= 'Set-Location $videoDir.FullName'
    $sCommand &= '$a = [regex]"f-(\d{1,4}).ts"'
    $sCommand &= '$segmentFolders = Get-ChildItem | where {$_.Attributes -eq 'Directory'}'
    $sCommand &= '$segmentFolders | ForEach-Object {'
    $sCommand &= 'Write-Output "Switching to Segment Folder: " + $_.FullName'
    $sCommand &= 'Set-Location $_.FullName'
    $sCommand &= '$segmentName = $_.Name'
    $sCommand &= '$text = '''
    $sCommand &= '$text | Set-Content 'mylist.txt''
    $sCommand &= '$fragments = Get-ChildItem -Include @("*.ts") -Path $_.FullName -Recurse'
    $sCommand &= '$fragments | ForEach-Object {'
    $sCommand &= '$b = $a.Match($_.FullName)'
    $sCommand &= '$order = [int]$b.Groups[1].Value'
    $sCommand &= 'Write-Output "Fragment # is: $order"'
    $sCommand &= '$_ | Add-Member -MemberType NoteProperty Order $order'
    $sCommand &= '}'
    $sCommand &= ' $fragments | Sort-Object Order | ForEach-Object{'
    $sCommand &= '$fragText = 'file ''' + $_.BaseName + '.ts''''
    $sCommand &= 'Write-Output $fragText'
    $sCommand &= '$fragText | Add-Content 'mylist.txt''
    $sCommand &= '}'
    $sCommand &= 'Write-Output "Segment name is $segmentName"'
    $sCommand &= '..\..\ffmpeg.exe -f concat -i mylist.txt -c copy ..\$segmentName.ts'
    $sCommand &= '}'
    $sCommand &= ''
    $sCommand &= ''
    $sCommand &= 'Stop-Service tiledatamodelsvc'
;~  Last two lines are just so you can see the output
    $sCommand &= ' ; '
    $sCommand &= 'Read-Host -Prompt "Press Enter to exit"'
    Run($sCommand)

 

Link to comment
Share on other sites

  • Moderators

Look at FileInstall in the help file, it will allow you to include the .ps1 file in the compiled executable.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

  • 1 month later...

So I'm back to this again.  Long break from this. 

Got the install ps1 file done but the first line of the ps1 file is currently 

USERPROFILE\Desktop\VideoPull\

I need that VideoPull folder to be something that the user chooses in the Autoit GUI written like this

;SELECT THE FOLDER
FUNC _select_folder()
    $sDir = FileSelectFolder("Choose the folder", "", 0, @ScriptDir)
    If StringRight($sDir, 1) <> "\" Then $sDir &= "\"

    If $sDir = "\" Then
        ToolTip("user abort",0,0)
        Sleep(500)
        ToolTip("",0,0)

    ;RUNNING THE SCRIPT
    ;$CMD=('cd '& $sDir &' && (FOR /R %G in ("*.ts") DO @echo file 'G') > mylist.txt && (FOR /R %G in ("*.ts") DO @echo file ''%G'') > mylist.txt && %userprofile%\desktop\ffmpeg.exe -f concat -i mylist.txt -c copy '& $sDir &'UPLOAD-THIS-FILE.ts && exit{ENTER}')

$folder = @DesktopDir&"\ffmpeg.exe" ;Set folder

    Elseif $sDir <> "\" Then
                ShellExecute('PowerShell.exe', '-executionpolicy Bypass -File "C:\Users\Server\Desktop\WindowsPull.ps1"')

So the Powershell file is on the desktop.  How can I allow the user to choose the folder that the Powershell is going to execute the ps1 (above) on?

Link to comment
Share on other sites

Thanks for the reply Subz!  Not really...So the windowspull.ps1 will always be on the desktop.  The user needs to be able to select the folder that the Powershellwill execute on.  So the folder that the user selects will change the first line of the Powershell... If I could It would be something like this as the first line of the Powershell

Set-Location $env:%USERPROFILE%\' & $sDir & '

But I can't put autoit scripting on a Powershell right???

Link to comment
Share on other sites

Or maybe something like:

_Select_Folder()
Func _Select_Folder()
    Local $sDir = FileSelectFolder("Choose the folder", "", 0, @ScriptDir)
    If @error Then
        ToolTip("user abort",0,0)
        Sleep(500)
        ToolTip("",0,0)
        Exit
    EndIf
    Local $sFFMPeg = @DesktopDir&"\ffmpeg.exe"
    Local $sWindowsPull = ""
        $sWindowsPull &= 'Set-Location "' & $sDir & '"' & @CRLF
        $sWindowsPull &= "$videoDir = Get-ChildItem | where {$_.Attributes -eq '" & "Directory" & "'} | Select-Object FullName" & @CRLF
        $sWindowsPull &= 'Write-Output "Switching to Event Folder:" + $videoDir.FullName' & @CRLF
        $sWindowsPull &= 'Set-Location $videoDir.FullName' & @CRLF
        $sWindowsPull &= '$a = [regex]"f-(\d{1,4}).ts"' & @CRLF
        $sWindowsPull &= "$segmentFolders = Get-ChildItem | where {$_.Attributes -eq 'Directory'}" & @CRLF
        $sWindowsPull &= '$segmentFolders | ForEach-Object {' & @CRLF
        $sWindowsPull &= 'Write-Output "Switching to Segment Folder: " + $_.FullName' & @CRLF
        $sWindowsPull &= 'Set-Location $_.FullName' & @CRLF
        $sWindowsPull &= '$segmentName = $_.Name' & @CRLF
        $sWindowsPull &= "$text = ''" & @CRLF
        $sWindowsPull &= "$text | Set-Content 'mylist.txt'" & @CRLF
        $sWindowsPull &= '$fragments = Get-ChildItem -Include @("*.ts") -Path $_.FullName -Recurse' & @CRLF
        $sWindowsPull &= '$fragments | ForEach-Object {' & @CRLF
        $sWindowsPull &= '$b = $a.Match($_.FullName)' & @CRLF
        $sWindowsPull &= '$order = [int]$b.Groups[1].Value' & @CRLF
        $sWindowsPull &= 'Write-Output "Fragment # is: $order"' & @CRLF
        $sWindowsPull &= '$_ | Add-Member -MemberType NoteProperty Order $order' & @CRLF
        $sWindowsPull &= '}' & @CRLF
        $sWindowsPull &= ' $fragments | Sort-Object Order | ForEach-Object{' & @CRLF
        $sWindowsPull &= "$fragText = 'file ' + $_.BaseName + '.ts'" & @CRLF
        $sWindowsPull &= 'Write-Output $fragText' & @CRLF
        $sWindowsPull &= "$fragText | Add-Content 'mylist.txt'" & @CRLF
        $sWindowsPull &= '}' & @CRLF
        $sWindowsPull &= 'Write-Output "Segment name is $segmentName"' & @CRLF
        $sWindowsPull &= '..\..\ffmpeg.exe -f concat -i mylist.txt -c copy ..\$segmentName.ts' & @CRLF
        $sWindowsPull &= '}' & @CRLF
        $sWindowsPull &= '' & @CRLF
        $sWindowsPull &= '' & @CRLF
        $sWindowsPull &= 'Stop-Service tiledatamodelsvc' & @CRLF
        $sWindowsPull &= ' ; ' & @CRLF
        $sWindowsPull &= 'Read-Host -Prompt "Press Enter to exit"'
        Local $hWindowsPull = FileOpen(@DesktopDir & "\WindowsPull.ps1", 2)
        FileWrite($hWindowsPull, $sWindowsPull & @CRLF)
        FileClose($hWindowsPull)
    If FileExists(@DesktopDir & "\WindowsPull.ps1") Then ShellExecute('PowerShell.exe', '-executionpolicy Bypass -File "' & @DesktopDir & '\WindowsPull.ps1"')
EndFunc

 

Link to comment
Share on other sites

Something strange is happening with this script.  Everything works as intended when the desktop is the true desktop (C:\Users\firstname.lastname\desktop) but when the desktop is mapped to OneDrive (C:\Users\firstname.lastname\OneDrive\desktop) It's like it can't find the ps1 file or something (see error at the end of the post).  For example:  

True desktop

Set-Location "C:\Users\C:\Users\firstname.lastname\Desktop\12-19-2018_13-09-41.135"
$videoDir = Get?-ChildItem | where {$_.Attributes -eq 'Directory'} | Select-Object FullName
Write-Output "Switching to Event Folder:" + $videoDir.FullName
Set-Location $videoDir.FullName
$a = [regex]"f-(\d{1,4}).ts"
$segmentFolders = Get-ChildItem | where {$_.Attributes -eq 'Directory'}
$segmentFolders | ForEach-Object {
Write-Output "Switching to Segment Folder: " + $_.FullName
Set-Location $_.FullName
$segmentName = $_.Name
$text = ''
$text | Set-Content 'mylist.txt'
$fragments = Get-ChildItem -Include @("*.ts") -Path $_.FullName -Recurse
$fragments | ForEach-Object {
$b = $a.Match($_.FullName)
$order = [int]$b.Groups[1].Value
Write-Output "Fragment # is: $order"
$_ | Add-Member -MemberType NoteProperty Order $order
}
 $fragments | Sort-Object Order | ForEach-Object{
$fragText = 'file ' + $_.BaseName + '.ts'
Write-Output $fragText
$fragText | Add-Content 'mylist.txt'
}
Write-Output "Segment name is $segmentName"
C:\UsersC:\Users\firstname.lastname\desktop\ffmpeg.exe -f concat -i mylist.txt -c copy ..\$segmentName.ts
}


Stop-Service tiledatamodelsvc
 ; 
Read-Host -Prompt "Press Enter to exit"

For some reason, a OneDrive mapped Desktop doesn't work:

Set-Location "C:\Users\firstname.lastname\OneDrive\desktop\12-19-2018_13-09-41.135"
$videoDir = Get?-ChildItem | where {$_.Attributes -eq 'Directory'} | Select-Object FullName
Write-Output "Switching to Event Folder:" + $videoDir.FullName
Set-Location $videoDir.FullName
$a = [regex]"f-(\d{1,4}).ts"
$segmentFolders = Get-ChildItem | where {$_.Attributes -eq 'Directory'}
$segmentFolders | ForEach-Object {
Write-Output "Switching to Segment Folder: " + $_.FullName
Set-Location $_.FullName
$segmentName = $_.Name
$text = ''
$text | Set-Content 'mylist.txt'
$fragments = Get-ChildItem -Include @("*.ts") -Path $_.FullName -Recurse
$fragments | ForEach-Object {
$b = $a.Match($_.FullName)
$order = [int]$b.Groups[1].Value
Write-Output "Fragment # is: $order"
$_ | Add-Member -MemberType NoteProperty Order $order
}
 $fragments | Sort-Object Order | ForEach-Object{
$fragText = 'file ' + $_.BaseName + '.ts'
Write-Output $fragText
$fragText | Add-Content 'mylist.txt'
}
Write-Output "Segment name is $segmentName"
C:\Users\firstname.lastname\OneDrive\desktop\ffmpeg.exe -f concat -i mylist.txt -c copy ..\$segmentName.ts
}


Stop-Service tiledatamodelsvc
 ; 
Read-Host -Prompt "Press Enter to exit"

I get this Powershell error:

Get?-ChildItem : The term 'Get?-ChildItem' is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
again.
At C:\Users\firstname.lastname\OneDrive\desktop\WindowsPull.ps1:2 char:13
+ $videoDir = Get?-ChildItem | where {$_.Attributes -eq 'Directory'} |  ...
+             ~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Get?-ChildItem:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Switching to Event Folder:
+
Set-Location : Cannot process argument because the value of argument "path" is null. Change the value of argument
"path" to a non-null value.
At C:\Users\firstname.lastname\OneDrive\desktop\WindowsPull.ps1:4 char:1
+ Set-Location $videoDir.FullName
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Set-Location], PSArgumentNullException
    + FullyQualifiedErrorId : ArgumentNull,Microsoft.PowerShell.Commands.SetLocationCommand

Stop-Service : Cannot find any service with service name 'tiledatamodelsvc'.
At C:\Users\firstname.lastname\OneDrive\desktop\WindowsPull.ps1:30 char:1
+ Stop-Service tiledatamodelsvc
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (tiledatamodelsvc:String) [Stop-Service], ServiceCommandException
    + FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.StopServiceCommand

Press Enter to exit:

Any idea where I've gone wrong or what the error means?  It's just strange that the Powershell script seems to be looking in the right spots but it still doesn't work for OneDrive.  Thank you for your time and help!

Edited by morejava
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...