Microsoft project open files using powershell

1k views Asked by At

I'm having problem opening project file from Project Online (server). I want to save all online files locally. If I open project first through Desktop client and try to open that file using function FileOpenEx it works (maybe because it opens draft?) Also I've tried full URL

 $Project.FileOpenEx(".../pwa/_api/projectdata/Projects/$name",$true)

But it started import wizard This is the code:

$csv = Import-Csv '...\ProjectNames.csv'
$Project = New-Object -ComObject msproject.application 
$csv | ForEach-Object {

$name=$_.ProjectName
Write-Output $name

    $Project.FileOpenEx("<>\$name",$true)

    $Save=$Project.FileSaveAs("...\Desktop\ProjectData\$name.mpp")

    Get-Process | where{$_.ProcessName -like "*winproj*"} | Stop-Process
}

I get this error when I try to open Project Online projects.

ForEach-Object : Exception calling "FileOpenEx" with "2" argument(s): "The remote procedure call failed. (Exception from HRESULT: 0x800706BE)"
    At ...\MigrateProjects.ps1:26 char:8
    + $csv | ForEach-Object {
    +        ~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [ForEach-Object], MethodInvocationException
        + FullyQualifiedErrorId : COMException,Microsoft.PowerShell.Commands.ForEachObjectCommand

EDIT: I've changed code to use shell like pointed in comments and it opens now. But when I try to save file sometimes it doesn't return boolean value and just stops. It happens on different projects, but it did save all my listed projects once. But when I want to repeat saving process it just exits at FileSaveAs. And I can't figure out why.

$csv = Import-Csv '...\ProjectData\ProjectNames.csv'

$ProjServer=".../sites/pwa/"

$ProjShell = new-object -comobject wscript.shell 
$ProjShell.Run("winproj /s $ProjServer", 1,$false)

Start-Sleep -s 15
$Project =[System.Runtime.InteropServices.Marshal]::GetActiveObject("msproject.application")

Try{
$csv | ForEach-Object {
    
    $name=$_.ProjectName
    Write-Output $name
    $NameWithoutSpaces= $name -replace '\s','_'

    $Project.FileOpenEx("<>\$name",$true)
    
   $Save= $Project.FileSaveAs("...\ProjectData\$NameWithoutSpaces.mpp")
   Write-Output $Save
}
}
catch{
    Get-Process | where{$_.ProcessName -like "*winproj*"} | Stop-Process
    $error[0].Exception.GetBaseException().LoaderExceptions
    }


Get-Process | where{$_.ProcessName -like "*winproj*"} | Stop-Process
0

There are 0 answers