0

I have a script that works perfectly in powershell. You give it a video file and it tells you all the extended attributes.

I'm trying to execute this from SQL's XPCmdShell by surrounding it with a script block like "&{my code goes here}" but that is not working. It's telling me I'm missing curly braces but i have them wrapped well since i jut added a single pair for the script blocking.

Can someone with experienced eyeballs tell me what I'm missing?

declare @cmd varchar(8000)
set @cmd = 'powershell -Command "&{
    $InputObject = ''C:\Downloads\PowershellScripts\Tests\testvideo.mp4'';
    $shell = New-Object -ComObject Shell.Application;
        foreach($object in $InputObject)
        {
            if($object -is [string])
            {
                try
                {
                    $object = Get-Item $object -ErrorAction Stop
                }
                catch
                {
                    Write-Warning "Error while processing $object : $($_.exception.message)"
                    break
                }
            }
            try
            {
                Test-Path $object -ErrorAction Stop
            }
            catch
            {
                Write-Warning "Error while processing $($object.fullname) : $($_.exception.message)"
                break
            }
            switch ($object)
            {
                {$_ -is [System.IO.DirectoryInfo]}{
                    write-host Processing folder $object.FullName -ForegroundColor Cyan
                    $currentfolder = $shell.namespace($object.FullName)
                    $items = $currentfolder.items()
                }
                {$_ -is [System.IO.FileInfo]}{
                    write-host Processing file $object.FullName -ForegroundColor Cyan
                    $parent = Split-Path $object
                    $currentfolder = $shell.namespace($parent)
                    $items = $currentfolder.ParseName((Split-Path $object -Leaf))
                }
            }
            try
            {
                foreach($item in $items)
                {
                    0..512 | ForEach-Object -Begin {$ht = [ordered]@{}}{
                        if($value = $currentfolder.GetDetailsOf($item,$_))
                        {
                            if($propname = $currentfolder.GetDetailsOf($null,$_))
                            {
                                $ht.Add($propname,$value)
                            }
                        }    
                    } -End {[PSCustomObject]$ht}
                }
            }
            catch
            {
                Write-Warning "Error while processing $($item.fullname) : $($_.exception.message)"
            }
        }
        }"'

print @cmd

exec xp_cmdshell @cmd
jimerb
  • 67
  • 9
  • Does a multi-line command like that usually work? Bring it down to something simpler to see if that's the issue (do a working one-liner, then split it to more than one line without changing the actual code). I suspect you can't do it this way. If you can, you have no need for wrapping it in another scriptblock anyway. Also take a look at the `-EncodedCommand` parameter (it's last in the list when you do `powershell /?`) – briantist Dec 26 '20 at 00:33
  • "It's telling me I'm missing curly braces" please show us the full extent of the error message you receive :) – Mathias R. Jessen Dec 26 '20 at 15:08
  • Maybe change `}}{` to `};`, or `} -Process {`? – leeharvey1 Dec 26 '20 at 21:47
  • create a script on network path and run – Esperento57 Dec 28 '20 at 03:49

0 Answers0