locked
PowerShell Management Agent - Import Script RRS feed

  • Question

  • Hi ,

    I am trying to import data from Exchange using PowerShell Management Agent.

    I couldn't able to get the Import. Please find below for my Import script.


    param (
    $Username,
    $Password
    )
    $SecurePassword=$Password|ConvertTo-SecureString -Force -AsPlainText
    $credential= New-Object System.Management.Automation.PSCredential($Username,$SecurePassword)
    $session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "http://Serverxxxx.com/PowerShell/" -Credential $credential
    Import-PSSession $session

    $MailBox=Get-MailBox

    ForEach($User in $MailBox) {
        $obj=@{}
    $stats=Get-MailboxStatistics $User.Identity
    $obj.Add("ExchangeId",$User.ExchangeGuid)
    $obj.Add("AccountName",$User.SamAccountName)
    $obj.Add("CurrentUsage",$stats.TotalItemSize)
    $obj
    }

    Could anyone  let me know whats wrong with my script?


    Thanks and Regards, Siva Kumar Balaguru

    Friday, January 18, 2013 10:39 PM

Answers

  • This error is typically because you're sending an object of than a Hashtable in the pipeline. In this case, it's probably the line $stats=Get-MailboxStatistics $User.Identity because that is outputting some to the pipeline.

    As a general cure, make sure that you're ONLY sending the hashtable in the pipeline.

    I'll try to handle this is the next version of the PS MA.


    Regards, Soren Granfeldt
    blog is at http://blog.goverco.com | twitter at https://twitter.com/#!/MrGranfeldt

    Tuesday, January 22, 2013 7:12 PM

All replies

  • Hi Siva Have you tried to run the script manually in the powershell console? See if that returns anything!
    Sunday, January 20, 2013 5:13 PM
  • In Powershell console, I can able to get the results.

    When I tried to get the error log, I have found the error below:

    GetImportEntries ERROR: Unable to cast object of type 'System.Management.Automation.PSModuleInfo' to type 'System.Collections.Hashtable'.


    Thanks and Regards, Siva Kumar Balaguru


    Monday, January 21, 2013 4:58 PM
  • Here is the documentation for the powershell management agent.

    http://blog.goverco.com/p/powershell-management-agent.html

    according to what it states, the new version requires an additional parameter as well besides the username and the password.

    param (
    $Username,
    $Password,
    $OperationType = "Delta"
    )

    or $OperationType = "Full" for a full import.

    Monday, January 21, 2013 5:46 PM
  • This error is typically because you're sending an object of than a Hashtable in the pipeline. In this case, it's probably the line $stats=Get-MailboxStatistics $User.Identity because that is outputting some to the pipeline.

    As a general cure, make sure that you're ONLY sending the hashtable in the pipeline.

    I'll try to handle this is the next version of the PS MA.


    Regards, Soren Granfeldt
    blog is at http://blog.goverco.com | twitter at https://twitter.com/#!/MrGranfeldt

    Tuesday, January 22, 2013 7:12 PM
  • Did you manage to resolve this?

    Regards, Soren Granfeldt
    blog is at http://blog.goverco.com | twitter at https://twitter.com/#!/MrGranfeldt

    Thursday, January 24, 2013 9:53 PM
  • Hi Granfeldt,

    I tried 
    ForEach($User in $MailBox) {
        $obj=@{}
    $stats=Get-MailboxStatistics $User.Identity
    $obj.Add("ExchangeId",$User.ExchangeGuid)
    $obj.Add("AccountName",$User.SamAccountName)
    $obj.Add("CurrentUsage",$stats.TotalItemSize)
    $obj
    }

    to

    ForEach($User in $MailBox) {
        $obj=@{}
    $obj.Add("ExchangeId",$User.ExchangeGuid)
    $obj.Add("AccountName",$User.SamAccountName)
    $obj.Add("CurrentUsage",Get-MailboxStatistics $User.Identity |select-object TotalItemSize)
    $obj

    }

    But No luck.

    I could find this error using DbgView 

    [11536] InvokeImportEntryWorker->GetImportEntries ERROR: Unable to cast object of type 'System.Management.Automation.PSModuleInfo' to type 'System.Collections.Hashtable'.



    Thanks and Regards, Siva Kumar Balaguru

    Monday, January 28, 2013 3:25 PM
  • If you run the script in a PowerShell prompt, you could check that you don't output any other objects that the hashtable to the pipeline. Somewhere in your script, your code generates an PSModuleInfo type object.

    It is probably the Import-Session $pssession which as I recall outputs something like the PSModuleInfo. Try to see if you can supress any output from this, i..e something like

    $null = Import-PSSession $pssession

    or similar

    I'm working on a new version of the MA that will honor new features of SP1 and that will be a little more tolerant of "wrong" objects being passed in the pipeline.


    Regards, Soren Granfeldt
    blog is at http://blog.goverco.com | twitter at https://twitter.com/#!/MrGranfeldt


    Monday, January 28, 2013 8:30 PM
  • Also, could you maybe post the complete script and a log from an import run? That may be helpful.
    Monday, January 28, 2013 8:59 PM
  • So - did you manage to resolve this?

    Regards, Soren Granfeldt
    blog is at http://blog.goverco.com | twitter at https://twitter.com/#!/MrGranfeldt

    Tuesday, February 5, 2013 2:00 PM
  • Hi Soren,

    There is no objects being imported with the import script :

    param 
    (
    $Username = "",
    $Password = "",

    $OperationType="Full"

    )

    Import-Module MSOnline -Force

    $SecurePassword = ConvertTo-SecureString $Password -AsPlainText -Force
    $Creds = New-Object System.Management.Automation.PSCredential $Username, $SecurePassword

    Connect-MsolService -Credential $Creds

    $Threshold = 0

    $users = Get-MsolUser 
    $global:RunStepCustomData='0'

    if ($Users.Count -lt $Threshold)
    {
    throw "Less users than expected returned from MSOnline"
    }

    #Always pass objects as hash table in pipeline
    foreach ($User in $Users)
    {
        $obj = @{}
        $obj.Add("Id", $User.UserPrincipalName)
        $obj.Add("objectClass", "user")
        $obj.Add("IsLicensed", $User.IsLicensed)
        $obj
    }

    However when i run this on powershell console i get back my users (2 in number) The error says that "Less users than expected returned from MSOnline. Any help will be appreciated.


    HBB


    • Edited by Hira Lal Monday, February 25, 2013 8:19 PM exact script being used
    Friday, February 22, 2013 11:30 AM
  • The script looks fine as far as I can see; two things -

    1) Did you change the filter "customer.com" to match your installation?

    2) Does the username and password entered on your MA match an accountname that has permissions to lookups in O365?


    Regards, Soren Granfeldt
    blog is at http://blog.goverco.com | twitter at https://twitter.com/#!/MrGranfeldt

    Sunday, February 24, 2013 11:24 AM
  • Yes Soren,

    Infact i have used

    $Users=Get-MsolUser

    and the accountname have the permission to lookups in o365. I created a custom application in C#.net referenced your dll and was able to retreive the 2 objects happily but the same is not getting returned via MA. 

    I have a doubt about RunStepCustomData. I have set 

    $global:RunStepCustomData='0'. It's for Full import.

    The error remains the same "less users than expected from MSOnline. :(


    HBB

    Monday, February 25, 2013 8:18 PM
  • If you're only using Full Imports, you do not need to specify a value in the RunStepCustomData - you can forget about that.

    Bare in mind, that the script is run as the FIM Sync service account - and if that is not the account that has the permissions to connect to Office 365, you WILL have to connect using the $Username and $Password passed from the MA to the import script.

    My bet is that this is a permission issue. Have you tried running the script under a shell started with RunAs as the account that you have specified?

    If possible, try to publish your import script in its entirety here...


    Regards, Soren Granfeldt
    blog is at http://blog.goverco.com | facebook https://www.facebook.com/TheIdentityManagementExplorer | twitter at https://twitter.com/#!/MrGranfeldt

    Monday, February 25, 2013 9:22 PM
  • Thanks Soren,

    I am passing the credentials in Import script only and not in the connectivity tab of Powershell MA. This is how i began:-

    Created a domain called "abc.onmicrosoft.com" in O365.

    Created an Admin Account "user.one@abc.onmicrosoft.com".

    This is the account i am using in my Import powershell script.

    The fimsyncengine is running with account "xyz\usertwo" where xyz is a domain and usertwo is the service account on which sync engine runs.

    Is this the issue?do i need to create a domain "xyz" in O365 and give permission to usertwo for lookups?

    if i am passing the credentials in the script itself does it really matters?

    moreover,xyz\usertwo have permissions to read the scripts and execute in powershell.

    I bet i am going somewhere wrong. a Little light will work as a Sun :)


    HBB

    Tuesday, February 26, 2013 7:18 AM
  • Aah, could it be that you havn't typed any credentials on the properties on the PS MA? If that is the case, you'll get a blank $username and $password in your script when run from the Sync Service - essentially not having permissions in O365

    If you're passing username and password, the service account for the Sync Service only needs permissions to execute the Powershell script. That seems in order, since you're getting the error message from the script

    Try passing the right credentials through the MA..


    Regards, Soren Granfeldt
    blog is at http://blog.goverco.com | facebook https://www.facebook.com/TheIdentityManagementExplorer | twitter at https://twitter.com/#!/MrGranfeldt


    Tuesday, February 26, 2013 7:23 AM
  • Hi Soren,

    Did the same.Passed the correct credentials from the properties page , connectivity tab . No Luck :( the same result. less users.....


    HBB

    Tuesday, February 26, 2013 7:29 AM
  • Could you please post your entire import script?

    Regards, Soren Granfeldt
    blog is at http://blog.goverco.com | facebook https://www.facebook.com/TheIdentityManagementExplorer | twitter at https://twitter.com/#!/MrGranfeldt

    Tuesday, February 26, 2013 7:31 AM
  • param 
    (
    $Username = "",
    $Password = "",

    $OperationType="Full"

    )

    Import-Module MSOnline -Force

    $SecurePassword = ConvertTo-SecureString $Password -AsPlainText -Force
    $Creds = New-Object System.Management.Automation.PSCredential $Username, $SecurePassword
    $global:RunStepCustomData='0'
    Connect-MsolService -Credential $Creds

    $Threshold = 0

    $users = Get-MsolUser 

    if ($Users.Count -lt $Threshold)
    {
    throw "Less users than expected returned from MSOnline"
    }

    #Always pass objects as hash table in pipeline
    foreach ($User in $Users)
    {
        $obj = @{}
        $obj.Add("Id", $User.UserPrincipalName)
        $obj.Add("objectClass", "user")
        $obj.Add("IsLicensed", $User.IsLicensed)
        $obj
    }

    The user name is "hira.lal@hljworld.onmicrosoft.com". Please provide me your skype id or emailid so that i ca share the credentials.

    The credentials are passed from the MA properties page only.


    HBB

    Tuesday, February 26, 2013 7:38 AM
  • Hi,

    The script looks fine as far as I can see. This is quite a puzzle. Could you try to add a few lines of debugging to the script and see if that helps, i.e.

    after

    $users = Get-MsolUser 

    try adding something like (and maybe more) -

    $username | Out-File C:\Temp\PS.txt -Append

    $password | Out-File C:\Temp\PS.txt -Append

    $user.count | Out-File C:\Temp\PS.txt -Append

    I could suspect that the $Users is uninitalized and therefore there is no .Count property on the object and you'll get the Threshold error.

    (don't want your credentials :-) )


    Regards, Soren Granfeldt
    blog is at http://blog.goverco.com | facebook https://www.facebook.com/TheIdentityManagementExplorer | twitter at https://twitter.com/#!/MrGranfeldt

    Tuesday, February 26, 2013 7:46 AM
  •  The result:-

    hira.lal@hljworld.onmicrosoft.com

    Microsoft.MetadirectoryServices.ConfigParameter

    Thats it. no count for users.I changed $user.count to $Users.Count. why is $Users not getting initialized is a point of concern.


    HBB

    Tuesday, February 26, 2013 8:15 AM
  • Yes, I'm puzzled.

    So for some reason the line

    $Users = Get-MSOLUser

    doesn't work. Have you tried my previous suggestion on running the script under a RunAs session? Does that tell you anything?

    Try adding more debug info to your script, i.e. -

    $SecurePassword | Out-File C:\Temp\PS.txt -Append

    to make sure that you have the right password (converted)


    Regards, Soren Granfeldt
    blog is at http://blog.goverco.com | facebook https://www.facebook.com/TheIdentityManagementExplorer | twitter at https://twitter.com/#!/MrGranfeldt

    Tuesday, February 26, 2013 8:23 AM
  • Yes soren,

    I have tried running the script under a RunAs session. I get the perfect 2 objects on the console. There is no permission issue.

    System.Security.SecureString is the output for $securePassword.


    HBB

    Tuesday, February 26, 2013 8:44 AM
  • Am I right that you didn't get the clear text password passed earlier? Instead you got the Microsoft.MetadirectoryServices.ConfigParameter text?

    Could you try setting the password explicitely in the script just to make sure, i.e. changing the line

    $SecurePassword = ConvertTo-SecureString $Password -AsPlainText -Force

    to

    $SecurePassword = ConvertTo-SecureString "MyCloudO365Secret" -AsPlainText -Force

    Regards, Soren Granfeldt
    blog is at http://blog.goverco.com | facebook https://www.facebook.com/TheIdentityManagementExplorer | twitter at https://twitter.com/#!/MrGranfeldt

    • Proposed as answer by Hira Lal Tuesday, February 26, 2013 10:40 AM
    Tuesday, February 26, 2013 8:56 AM
  • Eureka

    You were correct the password was not getting converted correctly. But where should be the change for the password?


    HBB

    Tuesday, February 26, 2013 9:02 AM
  • Is it working now?

    The line

    $SecurePassword = ConvertTo-SecureString $Password -AsPlainText -Force

    should do the trick; that's probably what you should investigate; also make absolutely sure that the correct password is entered on the MA. I just did a test in my enviroment with this line in my script

    $Password | Out-File C:\Temp\_Ps.txt -Append

    and that gave me the cleartext password in the _Ps.txt file. So that is where you need to go troubleshoot now.


    Regards, Soren Granfeldt
    blog is at http://blog.goverco.com | facebook https://www.facebook.com/TheIdentityManagementExplorer | twitter at https://twitter.com/#!/MrGranfeldt

    • Proposed as answer by Hira Lal Wednesday, March 6, 2013 8:43 AM
    Tuesday, February 26, 2013 9:07 AM
  • Yes its working now.Thanks for your overwhelming support Soren. Will investigate as to why am i getting different output with same command.

    HBB

    Tuesday, February 26, 2013 9:17 AM
  • No problem, glad to help; always happy to see that this MA is really being used - seems like its becoming the de facto for managing O365 licenses these days - and I'm working on a new version be released soon.

    Drop a review comment on my blog (http://blog.goverco.com/p/powershell-management-agent.html) and/or a thumbs up on Facebook (https://www.facebook.com/TheIdentityManagementExplorer) and I'm happy :-)

    Enjoy the rest of the troubleshooting...


    Regards, Soren Granfeldt
    blog is at http://blog.goverco.com | facebook https://www.facebook.com/TheIdentityManagementExplorer | twitter at https://twitter.com/#!/MrGranfeldt

    Tuesday, February 26, 2013 9:22 AM