WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: DGOODALE on June 11, 2018, 04:37:26 PM

Title: Call powershell cmdlet
Post by: DGOODALE on June 11, 2018, 04:37:26 PM
Ok this is driving me crazy and taking far to long to do and should be simple.  I can run the cmdlet in a ps console and it works but I can't get it called from WB.  It is supposed to create an exchange mbx in an hybrid environment.  I have tried almost every combination of quotes I can think of to get this to work.  Exchsession.ps1 makes that credentialed connection to Exch. and that functions fine.  I used this as a starter http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsupt.web+WinBatch/How~To+Run~PowerShell~Command.txt (http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsupt.web+WinBatch/How~To+Run~PowerShell~Command.txt)  What am I doing wrong?

   samaccountname="bilbba"
   FullName="Bilbo Baggins"
   UPN="Bilbo.Baggins@vsp.com"
   pwd="asdfasdf"
   first="Bilbo"
   last="Baggins"
   displaynm="Bilbo Baggins"
   alias="bilbba"
   orgunit="domain.com/users"
;RunWait("powershell.exe", 'c:\temp\newcsr\exchsession.ps1')

   pwrshl_cmd =  "New-RemoteMailbox -UserPrincipalName '%upn%' -Alias '%samaccountname%' -Name '%fullname%' -FirstName '%first%' -LastName '%last%' -DisplayName '%displaynm%' -OnPremisesOrganizationalUnit '%orgunit%' -Password (ConvertTo-SecureString '%pwd%' -AsPlainText -Force) -ResetPasswordOnNextLogon $true"
   RunWait("powershell.exe", '-command':pwrshl_cmd)
Title: Re: Call powershell cmdlet
Post by: td on June 11, 2018, 05:16:48 PM
One possible issue is the lack of a space after the '-command' switch, i.e., '-command '.
Title: Re: Call powershell cmdlet
Post by: DGOODALE on June 11, 2018, 06:37:03 PM
Ok, this probably isn't as straight forward as I thought it would be...
The full story... I have to create user accounts in bulk using an import spec file.  Each user has a line with all the attributes needed to create the account / mbx.
I have to establish a session with 0365, easy enough:
    $UserCredential = Get-Credential
    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://someserver.domain.com/PowerShell/ -Authentication Kerberos -Credential $UserCredential
    Import-PSSession $Session  -AllowClobber

However I have to continue to use that session for subsequent calls to New-RemoteMailbox which is where I believe this falls short as I use the new-remotemailbox command as a call in a loop that processes all users in the file.
Title: Re: Call powershell cmdlet
Post by: stanl on June 12, 2018, 02:39:48 AM
Not sure if this helps but the WB CLR can be used to connect to exchange.  The link below illustrates this with a simple email. Possibly it could be modified to process the loop for remote mailbox entries.


http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/getarticle.web+WinBatch/dotNet/EWS/Mail+Simple~EWS~Email~Message.txt

Another avenue might be the WB ADSI Extender.