passing on variables

Welcome Forums General PowerShell Q&A passing on variables

This topic contains 2 replies, has 2 voices, and was last updated by

4 years, 8 months ago.

  • Author
  • #17269

    Points: 0
    Rank: Member

    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

    $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

    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

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

  • #17272

    Points: 0
    Rank: Member

    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

    Points: 0
    Rank: Member

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

The topic ‘passing on variables’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort