Scriptblocking A Perfectly good Powershell Script within xp_CMDShell

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 "&{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


from Recent Questions - Stack Overflow https://ift.tt/37TpkSJ
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Spring Elasticsearch Operations

Network Error and Timeout on Authorize.net JS

Object oriented programming concepts (OOPs)