ParameterBindingException when pipelining an Outlook.Folder object to custom cmdlet
hello all-
i've written simple c# powershell cmdlet (get-store) returns outlook object model store object. cmdlet i've written reads above store object pipeline , creates email folder (get-folder)
i parameterbindingexception when piping returned store object get-store get-folder.
if create store object manually on ps command line, via new-object, pipe get-folder, works.
it appears errant store object __comobject type, while store object works outlook.storeclass type. know how can make work?
here's transcript of ps session:
---------------------------------------------------------------
ps c:\ps\mapi\bin\x64\debug> import-module .\mapi.dll
ps c:\ps\mapi\bin\x64\debug> $app = new-object -com outlook.application
ps c:\ps\mapi\bin\x64\debug> $stores = $app.session.stores
ps c:\ps\mapi\bin\x64\debug> $storeok = $stores[1]
ps c:\ps\mapi\bin\x64\debug> $storeok.gettype()
ispublic isserial name basetype
-------- -------- ---- --------
true false storeclass system.__comobject
ps c:\ps\mapi\bin\x64\debug> $storeok | get-folder -path inbox\works -createifnotexist
application : microsoft.office.interop.outlook.applicationclass
class : 2
session : microsoft.office.interop.outlook.namespaceclass
parent : system.__comobject
defaultitemtype : 0
defaultmessageclass : ipm.note
description :
entryid : 0000000029542a42175b114d983d2c3446907a490100b870629719727b4ba82a6c06a31c2912004c5fb6db8f0000
folders : system.__comobject
items : system.__comobject
name : works
storeid : 0000000038a1bb1005e5101aa1bb08002b2a56c20000454d534d44422e444c4c00000000000000001b55fa20aa6611
cd9bc800aa002fc45a0c00000048514d41494c5356523031002f6f3d4964656e746943727970742f6f753d45786368
616e67652041646d696e6973747261746976652047726f7570202846594449424f484632335350444c54292f636e3d
526563697069656e74732f636e3d6c657300
unreaditemcount : 0
userpermissions : system.__comobject
webviewon : false
webviewurl :
webviewallownavigation : true
addressbookname :
showasoutlookab : false
folderpath : \\les@voltage.com\inbox\works
inappfoldersyncobject : false
currentview : system.__comobject
customviewsonly : false
views : system.__comobject
mapiobject : system.__comobject
fullfolderpath : \\les@voltage.com\inbox\works
issharepointfolder : false
showitemcount : 1
store : system.__comobject
propertyaccessor : system.__comobject
userdefinedproperties : system.__comobject
ps c:\ps\mapi\bin\x64\debug> $storenotok = get-store -storename les@voltage.com
ps c:\ps\mapi\bin\x64\debug> $storenotok.gettype()
ispublic isserial name basetype
-------- -------- ---- --------
true false __comobject system.marshalbyrefobject
ps c:\ps\mapi\bin\x64\debug> $storenotok | get-folder -path inbox\doesntwork -createifnotexist
get-folder : input object cannot bound parameters command either because command not
take pipeline input or input , properties not match of parameters take pipeline input.
at line:1 char:15
+ $storenotok | get-folder -path inbox\doesntwork -createifnotexist
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ categoryinfo : invalidargument: (system.__comobject:psobject) [get-folder], parameterbindingexception
+ fullyqualifiederrorid : inputobjectnotbound,mapi.get_outlookfolder
here's code:
-------------------------------------------------------
namespace mapi
{
[cmdlet(verbscommon.get, "store")]
public class get_store : pscmdlet
{
protected override void processrecord()
{
base.processrecord();
this.writeobject(_getstore());
this.writedebug("get-app::processrecord");
}
public application _getapplication()
{
application app = new application();
return app;
}
[parameter(position = 0, valuefrompipeline = false)]
[validatenotnullorempty]
public string storename
{
get { return _storename; }
set { _storename = value.tolower(); }
}
string _storename;
public store _getstore()
{
application app = null;
try
{
if (null == _storename)
throw new argumentexception("missing argument 'storename'");
app = _getapplication();
foreach (store store in app.session.stores)
{
if (store.displayname.tolower() == _storename)
return store;
if (null != store)
marshal.releasecomobject(store);
}
return null;
}
finally
{
if (null != app)
marshal.releasecomobject(app);
}
}
}
[cmdlet(verbscommon.get, "folder")]
public class get_outlookfolder : pscmdlet
{
[parameter(position = 0, valuefrompipeline = true, valuefrompipelinebypropertyname = false)]
[validatenotnullorempty]
public microsoft.office.interop.outlook.store store
{
get
{
return _store;
}
set
{
_store = value;
}
}
store _store;
[parameter(position = 0,valuefrompipeline = false)]
public string path
{
get { return _folderpath; }
set { _folderpath = value; }
}
string _folderpath;
[parameter(position = 1, valuefrompipeline = false)]
public switchparameter createifnotexist
{
get { return _createifnotexist; }
set { _createifnotexist = value; }
}
bool _createifnotexist=false;
protected override void processrecord()
{
this.writedebug("get-folder::processrecord");
this.writeobject(_createfolder());
}
private void _createoropenfolder(string path, mapifolder parent, ref mapifolder ret)
{
if (null == path || "" == path)
{
ret = parent;
return;
}
string [] toks = path.split('\\');
if (null == toks)
throw new argumentexception("folder path invalid: " + _folderpath);
string foldername = toks[0];
mapifolder targetfolder = searchchildfolders(parent, foldername);
if (null != targetfolder)
{
path = path.remove(0, foldername.length);
path = path.trimstart('\\');
_createoropenfolder(path, targetfolder, ref ret);
}
else
{
// start creating
if (_createifnotexist)
{
foreach (string newfoldername in toks)
{
//if (null != targetfolder)
// marshal.releasecomobject(targetfolder);
targetfolder = parent.folders.add(newfoldername);
//marshal.releasecomobject(parent);
parent = targetfolder;
}
// return last 1 on path
ret = targetfolder;
}
else
throw new itemnotfoundexception("folder '" + foldername + "' not on path '" + _folderpath + "' , createifnotexist argument not passed.");
}
}
mapifolder searchchildfolders(mapifolder parent, string name)
{
foreach (mapifolder f in parent.folders)
if (f.name.tolower() == name)
return f;
else
marshal.releasecomobject(f);
return null;
}
// return requested folder.
public mapifolder _createfolder()
{
if (null == _folderpath)
throw new argumentnullexception("_folderpath");
if (null == _store)
throw new argumentnullexception("_store");
mapifolder ret = null;
_createoropenfolder(_folderpath,
_store.getrootfolder(),
ref ret);
return ret;
}
}
}
hi lesthaler,
for error got in powershell, please confirm parameter in cmdlet get-folder accepts kind of pipeline input type, error indicates wrong pipeline type.
more detailed information troubleshoot error, can check article:
if need suggestions of c# script, please go c# forum more effective support:
i hope helps.
Windows Server > Windows PowerShell
Comments
Post a Comment