passing on variables

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of murad murad 2 years, 4 months ago.

  • Author
    Posts
  • #17269
    Profile photo of murad
    murad
    Participant

    Hi
    I am new to Powershell, and I need help on the following:

    I put a script where a varibale is processed and then passed on to a command within the same script, the issue is that this command fails because it does not take the variabe , but i know for sure that the varibale has the right value, can somebody please explain to me what is wrong\missing, example below

    script
    [
    param($path,$user)
    $pathd = $path.Replace($user," ")
    #$pathd= "/vol/vol1/qt2/"
    echo " $pathd"
    $ql= select-string u:\powershell\qline.txt -pattern "$pathd" | ForEach-Object {$_.Line}
    echo "this is quota line $ql"
    $q= $ql.Replace($pathd," ")
    echo "this is quota $q"
    ]

    to run the above " script /vol/vol1/qt1/username username "

    the output is

    /vol/vol1/qt1/
    this is quota line
    You cannot call a method on a null-valued expression.
    At U:\powershell\aa.ps1:8 char:1
    + $q= $ql.Replace($pathd," ")
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

    so $q fails because $ql is empty, and I can not figure out why, $pathd is correctly processed and its value is " /vol/vol1/qt1/"
    $ql works fine if I pass the variable direclty as "/vol/vol1/qt1/" and it gets the value of "600"

    qline.txt contains the lines below
    /vol/vol1/qt1/600
    /vol/vol1/qt2/300

    Dave Wyatt: Edit: Moved to PowerShell Q&A Forum.

  • #17272
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    This line:

    $pathd = $path.Replace($user," ")
    

    replaces the value of $user with a single space character inside $path. In your commented out line of

    #$pathd= "/vol/vol1/qt2/"
    

    There is no space character. That is important; the whitespace becomes part of the pattern.

  • #17279
    Profile photo of murad
    murad
    Participant

    thanks very much, i replaced that line with "$pathd = $path -replace $user" and my script works fine, many thanks again

You must be logged in to reply to this topic.