0

나는 작고 단순한 과제를 수행하면서 이것을 수행하면서 powershell을 배우고있다. 나는 누군가 내가 올바른 방향으로 나를 가리 키길 바랄 것이므로 내가 잘못 가고있는 것을 정말로 알 수 없다. 나는 "All.sln"이라는 솔루션에 여러 프로젝트를 추가하려고합니다. 내 생각은 확실히 여기에 잘못된 일을하고 있다는 것입니다하지만 내가 잘못 뭐하는 거지 나에게 명확하지 않다, 그래서 다른 오류의 모든 종류가 계속PowerShell을 사용하여 솔루션에 여러 프로젝트를 추가하려면 어떻게합니까? (내가 뭔가 근본적으로 잘못된 것을해야만한다.)

[string]$slnName = "All" 
[string]$baseDir = "SOMEDIR_OF_PROJECTS" 
[string]$slnPath = (Join-Path $baseDir 'All.sln') 

$dteObj = New-Object -ComObject "VisualStudio.DTE.10.0" 
$solution = $dteObj.Solution 
$solution.Create($baseDir, $slnName) 
Write-Output "Solution Path: " $slnPath  

(ls $baseDir -Recurse *.vbproj) | % { 

    Write-Output $_.FullName 
    $solution = $dteObj.Solution 
    $solution.AddFromFile($_.FullName, $false) 
    $solution.SaveAs($slnPath) 
} 

Write-Output ($slnPath) 
$dteObj.Solution.SaveAs($slnPath) 
$dteObj.Quit() 

: 나는 다음과 같은 코드가 있습니다.

가끔 (보통 내가 추가 할 첫 번째 프로젝트 파일에)이 오류를 얻을 :

WARNING: System.Runtime.InteropServices.COMException (0x80010001): Call was rejected by callee. (Exception from HRESULT: 0x80 
010001 (RPC_E_CALL_REJECTED)) 
    at System.Runtime.InteropServices.ComTypes.ITypeInfo.GetTypeAttr(IntPtr& ppTypeAttr) 
    at System.Management.Automation.ComTypeInfo.GetTypeAttr(ITypeInfo typeinfo) 
    at System.Management.Automation.ComTypeInfo.Initialize() 
    at System.Management.Automation.ComTypeInfo.GetDispatchTypeInfo(Object comObject) 
    at System.Management.Automation.PSObject.GetMappedAdapter(Object obj, TypeTable typeTable) 
    at System.Management.Automation.PSObject.get_InternalAdapterSet() 
    at System.Management.Automation.PSObject.get_InternalTypeNames() 
    at System.Management.Automation.CmdletParameterBinderController.BindPipelineParametersPrivate(PSObject inputToOperateOn) 
    at System.Management.Automation.CmdletParameterBinderController.BindPipelineParameters(PSObject inputToOperateOn) 
    at System.Management.Automation.CommandProcessor.Read() 
    at System.Management.Automation.CommandProcessor.ProcessRecord() 
    at System.Management.Automation.CommandProcessorBase.DoExecute() 
    at System.Management.Automation.EnumerableOps.WriteEnumerableToPipe(IEnumerator enumerator, Pipe pipe, ExecutionContext co 
ntext, Boolean dispose) 
    at System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid3[T0,T1,T2](CallSite site, T0 arg0, T1 arg1, T2 arg2) 
    at System.Management.Automation.Interpreter.ActionCallInstruction`5.Invoke(Object arg0, Object arg1, Object arg2, Object a 
rg3, Object arg4) 
    at System.Management.Automation.Interpreter.CallInstruction.InvokeInstance(Object instance, Object[] args) 
    at System.Management.Automation.Interpreter.DynamicInstructionN.Run(InterpretedFrame frame) 
    at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame) 

또는 때때로 내가 얻을 (널 ??) :

You cannot call a method on a null-valued expression. 
At C:\DevHome\TFS\VCore\Scripts\BuildScripts\CreateMasterSolution.ps1:25 char:32 
+        $solution.AddFromFile($_.FullName) 
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : InvalidOperation: (:) [], RuntimeException 
    + FullyQualifiedErrorId : InvokeMethodOnNull 

또는 때때로 내가 얻을 (MethodNotFound ??) : 내가 얻을

Method invocation failed because [System.__ComObject] does not contain a method named 'SaveAs'. 
At C:\DevHome\VCore\VCore\Scripts\BuildScripts\CreateMasterSolution.ps1:27 char:32 
+        $solution.SaveAs($slnPath) 
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : InvalidOperation: (SaveAs:String) [], RuntimeException 
    + FullyQualifiedErrorId : MethodNotFound 

또는 때때로 (MethodNotFound ??) :

Method invocation failed because [System.__ComObject] does not contain a method named 'AddFromFile'. 
At C:\DevHome\TFS\VCore\Scripts\BuildScripts\CreateMasterSolution.ps1:18 char:32 
+        $solution.AddFromFile($_.FullName) 
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : InvalidOperation: (AddFromFile:String) [], RuntimeException 
    + FullyQualifiedErrorId : MethodNotFound 

또는 때때로 내가 얻을 :

Call was rejected by callee. (Exception from HRESULT: 0x80010001 (RPC_E_CALL_REJECTED)) 
At C:\DevHome\TFS\VCore\Scripts\BuildScripts\CreateMasterSolution.ps1:18 char:32 
+        $solution.AddFromFile($_.FullName) 
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : OperationStopped: (:) [], COMException 
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException 

답변

3

으로 VisualStudio와 "솔루션"에 대한 자세한 내용을 모른 채, 내가 당신에게 몇 가지 일반적인 PowerShell을 조언주지 :

  1. 을지지 않습니다 절대로을 객체에 대한 핸들을 성공적으로 관리했습니다. $dteObj을 만들었지 만 인스턴스화가 성공적이라고 가정 할 때 스팀이 켜집니다. 당신은이 설정되어 가정하지 마십시오,

    try { 
        $dteObj = New-Object -ComObject "VisualStudio.DTE.10.0"; 
        blah 
        blah 
    } 
    catch [Exception] { 
        error handling 
    } 
    
  2. 그냥 '객체 속성을 가지고 원인 :, 주위 try...catch 예컨대 포장해야합니다. 나는 NOT NULL, 예를 들면되는 솔루션 속성을 확인하는 것이 좋습니다 것 : if ($?) {} 사용하여 확인

    if ($dteObj.Solution -ne $null) { 
        do something 
    } else { 
        handle error state 
    } 
    
  3. 코드의 약간 예외가 발생하지 않는 경우 슬로우 예 :

    $solution.SaveAs($slnPath); 
    if (! $?) { 
        save failed 
    } else { 
        save ok 
    } 
    
+0

감사합니다. 나는 처음 7 줄에서 거의 문제를 경험하지 않는다. 그것은 내가 루프에 문제가있는 것 같습니다. 개체가 어떻게 든 완벽하게 인스턴스화되지 않았거나 기다려야 할 필요가 있다고 생각합니까? 뭐라고 요? – Denis