Error running Powershell script from c# -
i have ps1 script runs fine when executed powershell. creates user in office365:
param( [string]$adminuser, [string]$password, [string]$adminsite, [string]$userdisplayname, [string]$userfirstname, [string]$userlastname, [string]$userprincipalname, [string]$userlicense, [string]$useroffice, [string]$userdepartment ) try { [system.reflection.assembly]::loadwithpartialname("microsoft.sharepoint.client") [system.reflection.assembly]::loadwithpartialname("microsoft.sharepoint.client.runtime") $executionpolicy = get-executionpolicy set-executionpolicy remotesigned $secpasswd = convertto-securestring $password -asplaintext -force $credential = new-object system.management.automation.pscredential($adminuser,$secpasswd) connect-msolservice -credential $credential #write-host "conected msolservice ..." -foregroundcolor green connect-sposervice -url $adminsite -credential $credential # here fail when running .net #write-host "conected sp online ..." -foregroundcolor green $user = new-msoluser -firstname $userfirstname -lastname $userlastname -userprincipalname $userprincipalname -displayname $userdisplayname -licenseassignment $userlicenseassignment -office $useroffice -department $userdepartment -usagelocation es }catch [exception] { #write-host "an exception ocurred. proccess uncompleted" -foregroundcolor red #write-host $_.exception.message -foregroundcolor red set-executionpolicy $executionpolicy return $false } set-executionpolicy $executionpolicy return $user
it works. however, have c# program executes script in way:
private collection<psobject> runpsscriptfromfile(string psscriptpath, dictionary<string, object> parameters) { if (!file.exists(psscriptpath)) { throw new filenotfoundexception("file not found.", psscriptpath); } collection<psobject> returnobjects = null; using (runspace runspace = runspacefactory.createrunspace()) { runspace.open(); runspaceinvoke runspaceinvoker = new runspaceinvoke(runspace); pipeline pipeline = runspace.createpipeline(); command cmd = new command(psscriptpath, false); if (parameters != null && parameters.count > 0) { foreach (keyvaluepair<string, object> p in parameters) { commandparameter cp = new commandparameter(p.key, p.value); cmd.parameters.add(cp); } } pipeline.commands.add(cmd); returnobjects = pipeline.invoke(); } return returnobjects; }
this program works fine others scripts, one, following error (at line i've marked in script):
the 'connect-sposervice' command found in module 'microsoft.online.sharepoint.powershell', module not loaded. more information, run 'import-module microsoft.online.sharepoint.powershell'.
i found question this, without answer: error running ps1 c# code (office 365)
i've modified c# code:
pipeline.commands.add(cmd); returnobjects = pipeline.invoke(); var error = pipeline.error.readtoend(); // new line
the "error" var contains following:
the current processor architecture x86. 'c:\program files\sharepoint online management shell\microsoft.online.sharepoint.powershell\microsoft.online.sharepoint.powershell.psd1' module requires amd64 architecture.
i've located file , i've changed line
# processor architecture (none, x86, amd64, ia64) required module processorarchitecture = 'amd64'
for one:
# processor architecture (none, x86, amd64, ia64) required module processorarchitecture = 'x86'
i don't know if it's solution, it's works. keep looking. suggestion wellcome.
Comments
Post a Comment