none
Power Shell parameter to get computername instead of drive name RRS feed

  • Question

  • Hi,

    I am trying to derive a folder path using

    $path = Split-Path -parent $MyInvocation.MyCommand.Definition

    but this gives folder path with drive name for example E:\test\folder\  but i want to get \\100-test-342-t\test\folder instead.

    $drive1 = (get-location).Drive.Name +":"

    so i am using $replacedrive="\\"+ ($path -replace[regex]::Escape($drive1),$env:COMPUTERNAME).

    this works if i use it as a stand alone. But i have issue using it inside whole script since it errors out saying "string length too long".

    Can anyone please provide any better way of getting computer name in folder path.

    Thanks,

    SriDan
    Wednesday, February 3, 2016 8:00 PM

Answers

  • Hi,

    Here's one method:

    $folderPath = Join-Path -Path ("\\$($env:COMPUTERNAME)") -ChildPath (Split-Path -NoQualifier -Path (Split-Path -Parent $MyInvocation.MyCommand.Definition))
    
    $folderPath


    • Marked as answer by SriDan Wednesday, February 3, 2016 8:39 PM
    Wednesday, February 3, 2016 8:13 PM

All replies

  • Hi,

    Here's one method:

    $folderPath = Join-Path -Path ("\\$($env:COMPUTERNAME)") -ChildPath (Split-Path -NoQualifier -Path (Split-Path -Parent $MyInvocation.MyCommand.Definition))
    
    $folderPath


    • Marked as answer by SriDan Wednesday, February 3, 2016 8:39 PM
    Wednesday, February 3, 2016 8:13 PM
  • Thanks.

    This works.

    • Marked as answer by SriDan Wednesday, February 3, 2016 8:39 PM
    • Unmarked as answer by SriDan Wednesday, February 3, 2016 8:39 PM
    Wednesday, February 3, 2016 8:39 PM
  • Cheers, you're welcome.

    Wednesday, February 3, 2016 8:41 PM