Can i use existing variables in powershell read-host

前端 未结 2 1539
梦谈多话
梦谈多话 2021-01-07 12:18

i am writing a simple script in powershell using "read-host -prompt" so that i can input some text that will then be used to create some environment variables.

2条回答
  •  既然无缘
    2021-01-07 12:46

    You cannot do this straight :

    ####[int]$myVar = 10           Read too fast the question :)
    #$myVar = Get-ItemProperty ...
    #or
    #myVar = Get-Content ...
    $tempVar = Read-Host "Enter the value ($myVar is default)"
    if ($tempVar)
    {
       $myVar = $tempVar
    }
    $myVar
    # Store $myVar in the registry or in the file for next run
    

    My bad, I answered without a good reading :) As @postanote said, If you need to keep a track of the value from each run, you can store it in a file (IMHO, I would avoid the profile.ps1) or in a personal registry key. Then read the file or the registry value to initialize your variable.

提交回复
热议问题