SOLVED - Question regarding New-PSDrive

This topic contains 6 replies, has 5 voices, and was last updated by Profile photo of Gus Hoppes Gus Hoppes 3 months, 3 weeks ago.

  • Author
    Posts
  • #21848
    Profile photo of Péter Stork
    Péter Stork
    Participant

    Hi guys,

    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
    
    Set-ExecutionPolicy Unrestricted
    #$ErrorActionPreference="SilentlyContinue"
    $strHSRoot="\\uns.t-systems.tss\itsh_homeshare\hs"
    $objHSCount=[Get-Childitem -Path $strHSRoot?|Where-Object { $_.PSIsContainer }].Count
    $strHSPersonal="00000000$[[Get-ADUser -LDAPFilter "[sAMAccountName=$env:USERNAME]" -Properties extensionAttribute11].extensionAttribute11]"
    $strHSPersonal=$strHSPersonal.substring[$strHSPersonal.length-8,8] + "_" + $env:USERNAME
    for [$i=1;$i -le $objHSCount;$i++]{If [Test-Path "$strHSRoot$i\$strHSPersonal"]{break}}
    $strHS="$strHSRoot$i\$strHSPersonal"
    If [Test-Path K:]{Remove-PSDrive K}
    If [Test-Path S:]{Remove-PSDrive S}
    Start-Sleep -s 4
    New-PSDrive –Name "K" –PSProvider "Microsoft.Powershell.core\FileSystem" –Persist –Root \\uns.t-systems.tss\itsh_dfs
    New-PSDrive –Name "S" –PSProvider "Microsoft.Powershell.core\FileSystem" –Persist –Root $strHS
    
  • #21852
    Profile photo of Don Jones
    Don Jones
    Keymaster

    I'm not able to reproduce, but just to try something, could you put -Root before -Persist, making -Persist the last parameter?

    • #21869
      Profile photo of Péter Stork
      Péter Stork
      Participant

      Hi Don!

      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?

  • #21871
    Profile photo of Tim Pringle
    Tim Pringle
    Participant

    Hi Péter,

    [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 [i]–Root \\uns.t-systems.tss\itsh_dfs[/i] 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]

  • #21875
    Profile photo of Péter Stork
    Péter Stork
    Participant

    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! 🙂

    Cheers!

  • #26774
    Profile photo of Raghu Veer
    Raghu Veer
    Participant

    Hi Peter,

    Could you provide the steps How to change it to UTF8 BOM.
    Me too having the same issue.

    Thanks
    Raghu

  • #50887
    Profile photo of Gus Hoppes
    Gus Hoppes
    Participant

    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:

    & net use z: \\server\path

You must be logged in to reply to this topic.