5

I'm new to Powershell scripting and I'm working on a user management Powershell script, but I have run into a strange error.

The following code works when I run it from the shell, but not when it is run from a script:

$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionURI http://servername/Powershell/ -Authentication Kerberos -Credential $UserCredential -AllowRedirection
Import-PSSession $Session

When I run it in a script with a Param() block on the first line, it fails with the following error:

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

I can get the Import-PSSession to work if I remove my Param() block, but I'm not sure how to accept command-line arguments for my script otherwise. How can I keep the Param() block (or more generally, accept command-line arguments) and still be able to import the PS session?

I'm using Powershell v2 on a Windows 2008 R2 server trying to connect to a 2010 Exchange server.

Update:

I have a script named ManageUser.ps1 that I run from a Powershell prompt like

.\ManageUser.ps1 -disableUser -username someuser

The script starts like this:

Param(
    [switch]$disableUser,
    [string]$username
)
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionURI http://servername/Powershell/ -Authentication Kerberos -Credential $UserCredential -AllowRedirection
Import-PSSession $Session
#more stuff past here...

It is failing on the Import-PSSession command. But, if I remove the whole Param(...), the session import works. I hope this is enough for you to understand it!

Logan
  • 1,128
  • 12
  • 21
  • You want people to troubleshoot your code, without showing your code? How are you writing the `param()` block? How are you calling the script? – TessellatingHeckler Dec 03 '16 at 01:21
  • @TessellatingHeckler Sorry, I thought I had given enough info. I updated my question now. Please let me know if there's something else that would be helpful to know! Thanks. – Logan Dec 03 '16 at 01:55
  • Just wondering if you tried adding the -AllowClobber option to the Import-PSSession? Perhaps there's some default parameter setting happening in the background by Powershell? – SamuelWarren Aug 22 '17 at 18:55
  • @SamuelWarren It's been a while since I worked with this script, but I'm pretty sure I did try adding -AllowClobber. Thanks for the suggestion. – Logan Aug 24 '17 at 17:48

1 Answers1

2

Your script works fine on it's own and also when called from within another script:

& 'C:\Scripts\ManageUser.ps1' -disableUser -username "foo"
Get-Mailbox -resultsize 5 | ConvertTo-Csv -NoTypeInformation | Out-File C:\Scripts\mytest.csv

Subsequent runs will get the import error/warning due to not having -allowclobber as mentioned by @SamuelWarren...

In your case (at least when initially writing the question long ago) the error was due to another variable that you haven't mentioned here. It's likely you fixed that after the first run, and then subsequent tests were showing the AllowClobber error.

Also worth noting (for anybody who comes across this in the future) to check the path of the file you are calling. Just because you try to use a relative path .\myfile.ps1 doesn't mean that PowerShell is currently looking in the right directory.

Check: $psscriptroot

Or Split-Path -Path $($global:MyInvocation.MyCommand.Path)

Hicsy
  • 406
  • 7
  • 17