Home > front end >  Why does my Powershell script to convert .vss to .vssx result in errors on execution?
Why does my Powershell script to convert .vss to .vssx result in errors on execution?

Time:01-18

Does this seem to be correct? I am getting errors on trying to convert .vss to .vssx.

     >> $directoryToUpdate='C:\office\Stencils\*'
     >> $visio= New-Object -comVisio.Application
     >> foreach($vssFile in (get-childitem "$directoryToUpdate\*.vss" -Recurse")){
     >>    write-host "Working on $vssfile"
     >>    $doc=$visio.Documents.Open($vssFile.FullName)
     >>    $vssxFileName=[io.path]::ChangeExtension($vssFile,'.vssx')
     >>    $doc.SaveAs($VSSXFileName)
     >>    $doc.close();
     >> }

The output error messages are:

At line:1 char:21
  >> foreach($vssFile in (get-childitem "$directoryToUpdate\*.vss" -Rec ...
                      ~~
Unexpected token 'in' in expression or statement.
At line:1 char:20
  >> foreach($vssFile in (get-childitem "$directoryToUpdate\*.vss" -Rec ...
                     ~
Missing closing ')' in expression.
At line:19 char:73
  ... ch($vssFile in (get-childitem "$directoryToUpdate\*.vss" -Recurse")){
                                                                         ~
Unexpected token ')' in expression or statement.
      CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
      FullyQualifiedErrorId : UnexpectedToken

CodePudding user response:

I would say you are overcomplicating this. Here is a slight refactor of your code.

$visio = New-Object -com Visio.Application

 Get-ChildItem -Path 'C:\office\Stencils\*.vss' -Recurse | 
 ForEach-Object {
    "Working on $($PSItem.Name)"

    $doc          = $visio.Documents.Open($PSItem.FullName)
    $vssxFileName = [io.path]::ChangeExtension($PSItem,'.vssx')

    $doc.SaveAs($VSSXFileName)
    $doc.close()
 }
  • Related