none
What's up with 'Get-psdrive [driveletter] | Select Root' RRS feed

  • Question

  • Hey Guys,

    I am trying to create a script that would map the letter G to our DFS share.

    Because of some error handling i want to know wether it exists and wether it is already mappen to the DFS share or to something else.

    i am using this for it :

    if ((get-psdrive).Name -contains "G" -and (Get-PSDrive "G").Root -ne "\\ companyname.tld \ data") {

    write-host "already connected to something else"

    }

    This should work right? (but it doensn't)

    PS C:\Users\****> get-psdrive g

    Name           Used (GB)     Free (GB) Provider      Root                                               CurrentLocation
    ----           ---------     --------- --------      ----                                               ---------------
    G                   2,11         77,89 FileSystem    \\ companyname.tld \ data


    PS C:\Users\****> get-psdrive g | select Root

    Root
    ----
    G:\

    Do you get it?

    PS: this is my first post and not my native language, dont mind the space's in the UNC path, i got an error while trying to post this message with links : Body text cannot contain images or links until we are able to verify your account.

    • Edited by Sten Lootens Sunday, January 11, 2015 2:36 AM typo's (Corrected) and there was somecompany information included (removed!)
    Sunday, January 11, 2015 2:32 AM

Answers

  • Update:

    As you can see i found another var/prop DisplayRoot with gives me what i want.

    I Stil think thath it is strange thath when i output a object it gives result A and when i output object.property thath gives result B:

    PS C:\Users\Sten\Desktop> Get-Member -InputObject $tmp
    
    
       TypeName: System.Management.Automation.PSDriveInfo
    
    Name            MemberType     Definition                                                                                            
    ----            ----------     ----------                                                                                            
    CompareTo       Method         int CompareTo(System.Management.Automation.PSDriveInfo drive), int CompareTo(System.Object obj), in...
    Equals          Method         bool Equals(System.Management.Automation.PSDriveInfo drive), bool Equals(System.Object obj)           
    GetHashCode     Method         int GetHashCode()                                                                                     
    GetType         Method         type GetType()                                                                                        
    ToString        Method         string ToString()                                                                                     
    Credential      Property       pscredential Credential {get;}                                                                        
    CurrentLocation Property       string CurrentLocation {get;set;}                                                                     
    Description     Property       string Description {get;set;}                                                                         
    DisplayRoot     Property       string DisplayRoot {get;}                                                                             
    Name            Property       string Name {get;}                                                                                    
    Provider        Property       System.Management.Automation.ProviderInfo Provider {get;}                                             
    Root            Property       string Root {get;}                                                                                    
    Free            ScriptProperty System.Object Free {get=## Ensure that this is a FileSystem drive...                                  
    Used            ScriptProperty System.Object Used {get=## Ensure that this is a FileSystem drive...                                  
    
    PS C:\Users\Sten\Desktop> $tmp.DisplayRoot
    \\ company.tld \ data

    Could some one verify wether this is a Issue or me being stupid?

    • Marked as answer by Sten Lootens Sunday, January 11, 2015 3:12 AM
    Sunday, January 11, 2015 3:09 AM

All replies

  • You're not using the -contains comparison operator correctly. Read Get-Help about_Comparison_Operators. Change it to -eq (equals) and try again. Also, read The Big Book of PowerShell Gotchas on PowerShell.org.


    Sunday, January 11, 2015 2:36 AM
  • Thans for the reply, i will look into that.

    i quickly tried (without succes):

    PS C:\Users\****> if ((get-psdrive).Name -eq "G" -and (Get-PSDrive "G").Root -ne "\\ company.name \ data") { write-host "error" }

    I think the problems is thath get-psdrive [driveletter] gives an output with multiple variables in wich root contains the unc path, when i am trying to acces only the var/property 'Root' it would give me the drive letter instead? (look at the value of root)

    PS C:\Users\****> get-psdrive g
    
    Name           Used (GB)     Free (GB) Provider      Root                                               CurrentLocation
    ----           ---------     --------- --------      ----                                               ---------------
    G                   2,11         77,89 FileSystem    \\ companyname.tld \ data
    
    
    PS C:\Users\****> $tmp = get-psdrive g
    PS C:\Users\****> $tmp.root
    G:\

    • Edited by Sten Lootens Sunday, January 11, 2015 2:53 AM forget to mention thath -eq didnt work
    Sunday, January 11, 2015 2:50 AM
  • Update:

    As you can see i found another var/prop DisplayRoot with gives me what i want.

    I Stil think thath it is strange thath when i output a object it gives result A and when i output object.property thath gives result B:

    PS C:\Users\Sten\Desktop> Get-Member -InputObject $tmp
    
    
       TypeName: System.Management.Automation.PSDriveInfo
    
    Name            MemberType     Definition                                                                                            
    ----            ----------     ----------                                                                                            
    CompareTo       Method         int CompareTo(System.Management.Automation.PSDriveInfo drive), int CompareTo(System.Object obj), in...
    Equals          Method         bool Equals(System.Management.Automation.PSDriveInfo drive), bool Equals(System.Object obj)           
    GetHashCode     Method         int GetHashCode()                                                                                     
    GetType         Method         type GetType()                                                                                        
    ToString        Method         string ToString()                                                                                     
    Credential      Property       pscredential Credential {get;}                                                                        
    CurrentLocation Property       string CurrentLocation {get;set;}                                                                     
    Description     Property       string Description {get;set;}                                                                         
    DisplayRoot     Property       string DisplayRoot {get;}                                                                             
    Name            Property       string Name {get;}                                                                                    
    Provider        Property       System.Management.Automation.ProviderInfo Provider {get;}                                             
    Root            Property       string Root {get;}                                                                                    
    Free            ScriptProperty System.Object Free {get=## Ensure that this is a FileSystem drive...                                  
    Used            ScriptProperty System.Object Used {get=## Ensure that this is a FileSystem drive...                                  
    
    PS C:\Users\Sten\Desktop> $tmp.DisplayRoot
    \\ company.tld \ data

    Could some one verify wether this is a Issue or me being stupid?

    • Marked as answer by Sten Lootens Sunday, January 11, 2015 3:12 AM
    Sunday, January 11, 2015 3:09 AM
  • I think you need

    (Get-PSDrive -PSProvider filesystem).DisplayRoot

    Friday, August 9, 2019 9:04 AM