The script below works, if I use the PS command
New-PSDrive –Name “K” –PSProvider “Microsoft.Powershell.core\FileSystem” –Persist –Root \uns.t-systems.tss\itsh_dfs
in a PowerShell window, but if I run the script itself, I get the error below. Where might be the problem?
New-PSDrive : A positional parameter cannot be found that accepts argument '\\uns.t-systems.tss\itsh_dfs'.
At C:\Utility\remap_CFSDFS.ps1:13 char:1
+ New-PSDrive â?"Name "K" â?"PSProvider "Microsoft.Powershell.core\FileSystem"
â?" ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~
+ CategoryInfo : InvalidArgument: [:] [New-PSDrive], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.NewPSDriveCommand
New-PSDrive : A positional parameter cannot be found that accepts argument '\\uns.t-systems.tss\itsh_homeshare\hs10196163_pstork'.
At C:\Utility\remap_CFSDFS.ps1:14 char:1
+ New-PSDrive â?"Name "S" â?"PSProvider "Microsoft.Powershell.core\FileSystem"
â?" ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~
+ CategoryInfo : InvalidArgument: [:] [New-PSDrive], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.NewPSDriveCommand
I’ve tried rearranging all the parameters, with and without quotation marks, “Microsoft.Powershell.core\FileSystem”|“FileSystem”|FileSystem, etc.
Nothing seems to work, but as I said, if I copy the line with errors to the PS console window, it executes without any problems and maps the drive (with nearly any syntax I’ve tried so far)
Now I’m using the net use commands to do the same
net use k: /p:yes \\uns.t-systems.tss\itsh_dfs
net use s: /p:yes $strHS
, which is a workaround to say the least, but it works flawlessly.
Any other hints on what I could check to make New-PSDrive happen?
[ul]I guess one of the first things you can do is to check the value of your actual variables being used. Use Get-Variable at the end of your script to make sure you the required variables are being set correctly.[/ul]
[ul]Enclosing the value for the -Root parameter in single quotes [not double] is also not a bad idea.[/ul]
[ul]You could also try putting the -Root parameter as the very first parameter in the command, and see if you get a different response.[/ul]
[ul]Another thing to try in the script is to completely remove the –Root \uns.t-systems.tss\itsh_dfs part. Reason being, the Root parameter is mandatory with New-PSDrive. So if the remainder of the command is okay, you should be prompted at the console to enter a value for -Root. If not, then you’ve got problems before the -root parameter, and you can investigate further.[/ul]
Well, I seem to have found the issue, and it’s quite funny.
My script was encoded in UTF8, nonBOM.
Changing it to UTF8 BOM solved all problems.
Thanks for your support though.
Following those up made me learn some important factors about quotes at least!
I’ve seen this happen with older versions of powershell, particularly on older windows 7 machines. It works without problem on a new windows 10 pc. The only workaround I’ve found was using the net use commands. You can still use them through powershell. I’ve used a simple call operator in my script to allow commands through the shell. Like so: