Powershell module file for importing all sessions and cache them
below code has multiple functions connect online, exchange , lync onprem, ad , quest modules. when call module script import-module "fullpath.psm1" few of them working expected , few reloading again if there session. additionally exception code never works , event id's work intermittently. can please review code , suggest if additional parameters needs considered.
function connect-o365 { try { $global:o365session= new-pssession -configurationname microsoft.exchange -connectionuri https://outlook.office365.com/powershell-liveid/ -credential $credentials -authentication basic -allowredirection -warningaction stop -erroraction stop $temp = import-module (import-pssession $o365session -allowclobber -disablenamechecking -warningaction stop) -global import-module msonline -warningaction stop -erroraction stop connect-msolservice -credential $credentials -warningaction silentlycontinue -erroraction stop } catch [system.exception] { ([exception]) if ($_.exception.innerexception) {$global:msg = $_.exception.innerexception.message} else {$global:msg = $_.exception.message} write-host "$msg exception occured while connecting o365." write-eventlog –logname application –source "office365" –entrytype "error" –eventid "900" -message "$msg exception occured while connecting o365." add-content $logfile "exception occurred $_ $msg" -passthru exit } } function connect-onpremlync { try { $global:lyncsession = new-pssession -connectionuri https://lyncpool.onpremserver.com/ocspowershell -authentication negotiatewithimplicitcredential -warningaction silentlycontinue -erroraction stop $void = import-module (import-pssession $lyncsession -allowclobber -warningaction silentlycontinue -erroraction stop -disablenamechecking) -global -disablenamechecking } catch [system.exception] { ([exception]) if ($_.exception.innerexception) {$global:msg = $_.exception.innerexception.message} else {$global:msg = $_.exception.message} write-host "$msg exception occured while connecting onprem lync." write-eventlog –logname application –source "office365" –entrytype "error" –eventid "900" -message "$msg exception occured while connecting onprem lync." add-content $logfile "exception occurred $_ $msg" -passthru exit } } function connect-onpremexchange { try { $global:onpremsession = new-pssession -configurationname microsoft.exchange -connectionuri http://exchange.onpremserver.com/powershell/ -authentication kerberos -warningaction silentlycontinue -erroraction stop $temp1 = import-module (import-pssession $onpremsession -allowclobber -warningaction silentlycontinue -erroraction stop -disablenamechecking) -global -prefix onprem -disablenamechecking } catch [system.exception] { ([exception]) if ($_.exception.innerexception) {$global:msg = $_.exception.innerexception.message} else {$global:msg = $_.exception.message} write-host "$msg exception occured while connecting onprem exchange." write-eventlog –logname application –source "office365" –entrytype "error" –eventid "900" -message "$msg exception occured while connecting onprem exchange." add-content $logfile "exception occurred $_ $msg" -passthru exit } } $global:questsnapin = get-pssnapin -name "quest.activeroles.admanagement" $global:admodule = get-module -name "activedirectory" $global:credentials = get-credential $onlinesession = get-pssession | ? { ($_.computername -eq "outlook.office365.com") -and ($_.state -eq "opened") } $lyncsessioncheck = get-pssession | ? { ($_.computername -eq "lync.onpremserver.com") -and ($_.state -eq "opened") } $onpremsessioncheck = get-pssession | ? { ($_.computername -eq "exchange.onpremserver.com") -and ($_.state -eq "opened") } $global:logfile = "c:\log\exceptions.txt" if(!$onlinesession) { connect-o365 } if(!$lyncsessioncheck) { connect-onpremlync } if(!$onpremsessioncheck) { connect-onpremexchange } if(!$questsnapin) { add-pssnapin quest.activeroles.admanagement -erroraction stop } if(!$admodule) { import-module activedirectory } ahmed ali
hi,
>> but when call module script import-module "fullpath.psm1" few of them working expected , few reloading again if there session.
please try putting module in $env:psmodulepath try again.
besides, using powershell ise or powershll console has same issue?
best regards,
andy
please remember mark replies answers if help.
if have feedback technet subscriber support, contact tnmff@microsoft.com.
Windows Server > Windows PowerShell
Comments
Post a Comment