I am learning powershell while doing this (what I thought would have been) a small and simple assignment. I can't really figure out where I am going wrong so hoping someone would point me in the right direction. I am just trying to add a number of projects to a solution called "All.sln". I have the following code:
[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()
I keep getting all sorts of different errors so my thinking is that I am definitely doing something wrong here but it is not obvious to me what I am doing wrong.
I sometimes get this error (usually on the first project file I try to add):
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)
or sometimes I get (Null??):
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
or sometimes I get (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
or sometimes I get (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
or sometimes I get:
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
Without knowing more about VisualStudio and its "solutions", I'll just give you some general PowerShell advice:
Never assume you've successfully managed to get a handle to an object - you create your $dteObj
, but then steam on, assuming the instantiation was successful. You need to wrap a try...catch
around it, e.g.:
try {
$dteObj = New-Object -ComObject "VisualStudio.DTE.10.0";
blah
blah
}
catch [Exception] {
error handling
}
Just 'cause an object has an attribute, don't assume it's set. I'd recommend checking the Solution attribute isn't null, e.g.:
if ( $dteObj.Solution -ne $null ) {
do something
} else {
handle error state
}
If a bit of code doesn't cause an exception to be thrown, check it using if ($?) {}
, e.g.:
$solution.SaveAs( $slnPath );
if ( ! $? ) {
save failed
} else {
save ok
}
User contributions licensed under CC BY-SA 3.0