Announcement

Collapse
No announcement yet.

PSDrives, UNCs, expanding variables!

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • PSDrives, UNCs, expanding variables!

    Hi Guys!

    What I'm trying to achieve seems very simple. I'm trying to use a PSDrive to plop the contents of a variable into a remote file on a server (obviously this will expand out to many servers once I have it tested). The file path is expanded from the Server Name which is in the variable $SName - and the filename is "servername.ini". A credential for a known Domain Admin user has been placed in $A.

    I created and used the drive using the following:

    Code:
    New-PSDrive -Name U -Path "\\$SName\C$" -Credential $A
    
    Set-Location U:
    $IniFile | Set-Content -Path "$SName.ini"
    The issue I have is that in the "Set-Content" command, the path of "U" is expanded three times so that I get the following error message:

    Code:
    Set-Content : Could not find a part of the path 
    '\\servername\C$\servername\C$\servername\C$\servername.ini'.
    Can anyone tell me what I'm doing wrong in creating and/or referencing this PSDrive?

    Sorry for being a n00b... TIA.

    p.s. I have also tried the obvious 'set-Content -Path "U:\$SName.ini" ' instead of using "Set-Location".
    Last edited by Stonelaughter; 7th May 2013, 16:28.


    Tom
    For my own and your protection, I do not provide support by private message under any circumstances. All such messages will be deleted and ignored.

    Anything you say will be misquoted and used against you

  • #2
    Re: PSDrives, UNCs, expanding variables!

    Is -Path a valid parameter?, I think it should be -Root.
    If you run the commands in a Windows powershell (v3) window, do see the working path changing to U:\ ? (U: with a backslash!)
    Code:
    New-PSDrive -Name U -PsProvider FileSystem -Root '\\$SName\C$' -Credential $A
    
    Set-Location 'U:\'
    
    dir
    If 'U:\' is the working directory then continue with this next part of the script:

    Code:
    $cmd = "(get-content '$IniFile') | Set-Content -Path '$SName.ini'"
    invoke-expression $cmd
    /Rems
    Last edited by Rems; 7th May 2013, 19:01.

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment


    • #3
      Re: PSDrives, UNCs, expanding variables!

      Sorry of course you're right it's "Root" - I just mistranscribed it. Thanks very much for the tips!
      Last edited by Stonelaughter; 8th May 2013, 10:22.


      Tom
      For my own and your protection, I do not provide support by private message under any circumstances. All such messages will be deleted and ignored.

      Anything you say will be misquoted and used against you

      Comment


      • #4
        Re: PSDrives, UNCs, expanding variables!

        OK that didn't work because invoking expression tried to invoke the contents of the INI File!!

        Here's the entire script as it is now, incorporating your quotes and such where you suggested...

        Can anyone help any further? In the "Set-Content" command, it's still trying to write to a path where the server name and C$ are expanded three times; but if I do a "get-location" I just get "U:\" and if I get-content $FN I just get "servername.ini".

        I'm beginning to wonder if this is a bug in PS3.0...?

        Code:
        $ServList = import-csv -Path C:\Stage\TestList.csv -Header "ServerName"
        
        $OriginalIniFile = get-content -Path C:\Stage\64ini.txt
        
        $A = Get-Credential -Credential dom\user
        
        ForEach ($csvline in $ServList)
            {
                $IniFile = $OriginalIniFile
                $SName = $csvline.ServerName.Substring(0,13)
        
                New-PSDrive -Name U -PSProvider FileSystem -Root \\$SName\C$ -Credential $A
                
                $iniFile = $iniFile.ToUpper().Replace("HPUSS",$SName.ToUpper())
        
                Set-Location 'U:\'
        
                $FN = "$Sname.ini"
                $IniFile | Set-Content -Path $FN
        
                Set-Location 'C:\'
                Remove-PSDrive U
        
            }


        Tom
        For my own and your protection, I do not provide support by private message under any circumstances. All such messages will be deleted and ignored.

        Anything you say will be misquoted and used against you

        Comment


        • #5
          Re: PSDrives, UNCs, expanding variables!

          OK the following works: trying to expand "$SName" in the -Destination parameter of "Copy-Item" did NOT work (no idea why but it used "$SName" literally as the path instead of expanding it). That's why I've used "Set-Location".

          I now believe there to be a bug in the "Set-Content" commandlet which doesn't expand PSDrive locations properly...

          Thanks for anyone who was headscratching with me over this.

          Code:
          $ServList = import-csv -Path C:\Stage\TestList.csv -Header "ServerName"
          
          $OriginalIniFile = get-content -Path C:\Stage\64ini.txt
          
          $A = Get-Credential -Credential dom\user
          
          ForEach ($csvline in $ServList)
              {
                  $IniFile = $OriginalIniFile
                  $SName = $csvline.ServerName.Substring(0,13)
          
                  $iniFile = $iniFile.ToUpper().Replace("HPUSS",$SName.ToUpper())
          
                  Set-Location 'C:\Stage'
          
                  $FN = "$Sname.ini"
                  $IniFile | Set-Content -Path $FN
          
                  Set-Location \\$SName\C$
                  Copy-Item -Path C:\Stage\$FN 
                  Set-Location C:\Stage 
                  
              }


          Tom
          For my own and your protection, I do not provide support by private message under any circumstances. All such messages will be deleted and ignored.

          Anything you say will be misquoted and used against you

          Comment


          • #6
            Re: PSDrives, UNCs, expanding variables!

            Originally posted by Stonelaughter View Post
            OK the following works: trying to expand "$SName" in the -Destination parameter of "Copy-Item" did NOT work (no idea why but it used "$SName" literally as the path instead of expanding it). That's why I've used "Set-Location".

            I now believe there to be a bug in the "Set-Content" commandlet which doesn't expand PSDrive locations properly...

            Thanks for anyone who was headscratching with me over this.

            Code:
            $ServList = import-csv -Path C:\Stage\TestList.csv -Header "ServerName"
            
            $OriginalIniFile = get-content -Path C:\Stage\64ini.txt
            
            $A = Get-Credential -Credential dom\user
            
            ForEach ($csvline in $ServList)
                {
                    $IniFile = $OriginalIniFile
                    $SName = $csvline.ServerName.Substring(0,13)
            
                    $iniFile = $iniFile.ToUpper().Replace("HPUSS",$SName.ToUpper())
            
                    Set-Location 'C:\Stage'
            
                    $FN = "$Sname.ini"
                    $IniFile | Set-Content -Path $FN
            
                    Set-Location \\$SName\C$
                    Copy-Item -Path C:\Stage\$FN 
                    Set-Location C:\Stage 
                    
                }
            At home and tired so I may be way off track here but don't you need to actually get the contents of your text file before piping it into the required ini

            Code:
            Get-Content $IniFile | Set-Content -Path $FN

            Comment


            • #7
              Re: PSDrives, UNCs, expanding variables!

              Originally posted by Stonelaughter View Post
              I now believe there to be a bug in the "Set-Content" commandlet which doesn't expand PSDrive locations properly...
              I think so too.
              As a workaround, instead of mapping a drive (with PSDrive) maybe you can use:
              $U = Join-Path "\\$SName\D$" "" -Credential $A


              (I am not sure what was the purpose of using .Substring(0,13) in your script, presumably something with a naming convention. I have changed it a bit in the sample below, so I was able to test the script here.)
              Code:
              $ServList = import-csv -Path 'C:\Stage\TestList.csv' -Header "ServerName" | Where-Object {$_.ServerName -ne ''}
              
              $OriginalIniFile = get-content -Path 'C:\Stage\64ini.txt'
              
              $A = Get-Credential -Credential 'domain\user'
              
              $ServList | % {
              
                   $SName = $_.ServerName.Trim()
                   $SName = $SName.substring(0, [System.Math]::Min(99, $SName.Length))
              
                   $IniFile = ("$OriginalIniFile").ToUpper()
                   $IniFile = ("$IniFile").Replace("HPUSS",$SName.ToUpper())
              
                   $U = Join-Path "\\$SName\D$" "" -Credential $A
              
                   Set-Location $U -ErrorAction SilentlyContinue
                   $IniFile | Set-Content "$Sname.ini" -ErrorAction SilentlyContinue 
              
                   Set-Location C:\
              
                 }
              /Rems
              Last edited by Rems; 8th May 2013, 20:16.

              This posting is provided "AS IS" with no warranties, and confers no rights.

              __________________

              ** Remember to give credit where credit's due **
              and leave Reputation Points for meaningful posts

              Comment


              • #8
                Re: PSDrives, UNCs, expanding variables!

                The substring is to get rid of the name qualification; the server name in the test environment was 13 characters long when you omit the DNS Suffix. In the live script I've used:

                Code:
                 $SName = $csvline.ServerName.Split(".") | select -First 1
                which splits the server name up to the first dot.

                So it worked in my test environment but not in my live... back to the head-scratching!!!
                Last edited by Stonelaughter; 9th May 2013, 11:24.


                Tom
                For my own and your protection, I do not provide support by private message under any circumstances. All such messages will be deleted and ignored.

                Anything you say will be misquoted and used against you

                Comment


                • #9
                  Re: PSDrives, UNCs, expanding variables!

                  NEW TOP TIP

                  When you're mapping a drive or otherwise referencing a hidden share (C$) in Powershell, BACKTICK THE DOLLAR!!!

                  e.g.

                  Code:
                   
                  
                  New-PSDrive -Name U -PSProvider Filesystem -Root \\$SName\C`$ -Credential $A
                  This will fix the "expanding three times" issue described above.


                  Tom
                  For my own and your protection, I do not provide support by private message under any circumstances. All such messages will be deleted and ignored.

                  Anything you say will be misquoted and used against you

                  Comment


                  • #10
                    Re: PSDrives, UNCs, expanding variables!

                    Originally posted by Stonelaughter View Post
                    NEW TOP TIP

                    cheers

                    The dollar sign is a special character to powershell, it is used for variables and Powershell will try to expand. If you want to use the special character as a literal you need to escape it in some cases with a back-quote sign.
                    Or, it is also possible to wrap the string (in this case the unc path to the hidden share) in single-quote signs (not! double-quote signs). Straight single-quote signs work in much the same way as straight double-quote signs execept that; 1)the embedding of double quotes in the encapsulated string is allowed; 2)variable names are not expanded.

                    Thanks Tom!

                    /Rems
                    Last edited by Rems; 9th May 2013, 19:17.

                    This posting is provided "AS IS" with no warranties, and confers no rights.

                    __________________

                    ** Remember to give credit where credit's due **
                    and leave Reputation Points for meaningful posts

                    Comment

                    Working...
                    X