Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 8583

Import-PSSession, bug or bug-like feature?

$
0
0

Good Morning,

I've come across a strange error with the import-pssession cmdlet, and wanted to run it by the experts. I'm using it in a larger account creation script the import the exchange cmdlets from an exchange server to create a mailbox. In testing it all in the shell the below commands work fine:

$session=New-PSSession-ConfigurationNameMicrosoft.Exchange-ConnectionUrihttp://$exchangeserver/powershell

Import-PSSession-session$session-AllowClobber-DisableNameChecking

However, when I run them in my larger script, I get the below error:

Import-PSSession : Cannot convert null to type "System.DateTime".

At C:\scripts\importbug.ps1:8 char:5

+ Import-PSSession -session $session -AllowClobber -DisableNameChecking

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

+ CategoryInfo : MetadataError: (:) [Import-PSSession], ArgumentTransformationMetadataException

+ FullyQualifiedErrorId : RuntimeException,Microsoft.PowerShell.Commands.ImportPSSessionCommand

Now I couldn't for the life of me figure out what Import-PSSession would be doing with any dateTime values, or even what null variable I was passing to it. After deconstructing my script I narrowed it down the following minimum code to illustrate the error:

 

FunctionGet-Exchangecmdlets {

    param(

        [string]$ExchangeServer,

 

        [datetime]$date

    )

    $session=New-PSSession-ConfigurationNameMicrosoft.Exchange-ConnectionUrihttp://$exchangeserver/powershell-AllowRedirection-AuthenticationKerberos

    Import-PSSession-session$session-AllowClobber-DisableNameChecking

}

 

Get-Exchangecmdlets-ExchangeServer'exchangeserver1'  #This throws the error

 

Get-Exchangecmdlets-ExchangeServer'exchangeserver1'-Date"10/10/2013"  #This does not

 

So it seems that when import-pssession runs, it validates all the variables in its parent function.  Since $Date wasn't specified, it was null, which can't be converted to DateTime, hence the error.  This error also appeared with parameters using the [validatescript] setting, such as:

 

FunctionGet-Exchangecmdlets {

    [cmdletbinding()]

    param(

        [string]$ExchangeServer,

 

        [ValidateScript({test-path (Split-Path-Path$_-Parent)})]

        [string]$ErrorLog

    )

    $session=New-PSSession-ConfigurationNameMicrosoft.Exchange-ConnectionUrihttp://$exchangeserver/powershell-AllowRedirection-AuthenticationKerberos

    Import-PSSession-session$session-AllowClobber-DisableNameChecking

}

 

Get-Exchangecmdlets-ExchangeServer'exchangeserver1'  #This throws the error

Import-PSSession : Cannot bind argument to parameter 'Path' because it is an empty string.

At C:\scripts\importbug.ps1:10 char:5

+ Import-PSSession -session $session -AllowClobber -DisableNameChecking

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

+ CategoryInfo : InvalidData: (:) [Import-PSSession], ParameterBindingValidationException

+ FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.ImportPSSessionCommand

 

I didn’t test the other validation methods, but I suspect they’ll all throw the error.

 

Once identified this is easy enough to work around, I simply made a separate function for the import-pssession with no optional parameters and no validation.  However, since optional parameters should only be validated if a value is provided, this seems rather bug-like to me.  Nothing in the documentation for Import-PSSession jumped out at me as suggesting that variables are re-processed or anything like that.

 

Thoughts?


Viewing all articles
Browse latest Browse all 8583

Trending Articles