how do i get a variable from a seperate text file [duplicate] - powershell

This question already has an answer here:
Import Variables from Text File in Powershell
(1 answer)
Closed 6 years ago.
Lets say I have a text file full of variables.
I would like to create a separate file in which I can read and call these variables using powershell.
EX. Variable.txt contains $X=10
Main.ps1 retrieves the variable X from the text file, and doubles it.
Apologize is this is an amateur question, I am very new to powershell.

For your specific purpose, you could have a file, data.ps1 containing your data, like so:
$x=10
Then, a script.ps1 file, similar to the following:
. ./data.ps1
$x = $x * 2
Write-Host "Value X is $x"
Which would give you the following output when you run ./script.ps1:
Value X is 20

Related

Activate variable inside user input [duplicate]

This question already has answers here:
Expanding variables in file contents
(3 answers)
Powershell reevaluate string to array
(1 answer)
Closed 2 months ago.
Task: make PowerShell 5 to read/activate user input that contains any variable. This doesn't work on both script defined and environment variables. E.g.:
$H=4
$testParA=Read-Host "Try to input enviroment variable `$Env:windir :" #user input is $H
$testParB=Read-Host "Try to input variable `$H :" #user input is $H
Write-Host $testParA
Write-Output $testParA
Write-Host $testParB
Write-Output $testParB
Result:
> $Env:windir
> $Env:windir
> $H
> $H
Expectation:
> C:\Windows
> C:\Windows
> 4
> 4
Original task: In a text generator PowerShell script, let users to customize text, and where to place the serial counter variable "$serial". E.g., "some text $serial some other text"
No-duplication: This question has been marked as duplicate from "https://stackoverflow.com/questions/1667023/expanding-variables-in-file-contents", but not really. The answer is surely duplicated, but the question part is very different (You have to know "expand variables" to be able to find this question from searching, which is why I was unable to get answers before posting this question), and could have other methods to achieve the same or a better result. In general, on closing this question will only cause more people who never heared of "expand variables" to further create similar questions. There is probably a better method than simply closing this question.

Powershell how to replace multiple commands with output of single variable [duplicate]

This question already has answers here:
Access PSObject property indirectly with variable
(3 answers)
Set Value of Nested Object Property by Name in PowerShell
(3 answers)
Closed 2 months ago.
I am traversing an object and would like to replace several path parts with a string stored in another variable. For example:
# a is a complex ps object, the path is valid
$a.b.c
myvalue
$replace = "b.c"
$a.$($replace)
# here I expect 'myvalue', however nothing is returned, as I would like to execute the equivalent of $a.b.c
Is there some way to do this replacement?

How can I trim the process path from output? [duplicate]

This question already has answers here:
Get only filename from full path of a file
(2 answers)
Closed 5 years ago.
I'm writing a PowerShell script to output the process,
$Process=chknull $_.Properties[0].Value
$Process_new=$Process -replace '(?<! .*) ','_'
The above code outputs the result as entire path of process, Eg:
C:\Windows\System32\Notepad.exe.
Is there a way, where I can get just "Notepad.exe"
If you're getting a full path return like that as a string you can set the value into a variable and then use split on it and grab the last entry in the resulting array.
Example:
("C:\Windows\System32\Notepad.exe." -split "\\")[-1]
Remember to use two "\" as it is also an escape character in regex.
([IO.FileInfo]"C:\Windows\System32\Notepad.exe").Name

Need help understanding perl code [duplicate]

This question already has answers here:
What does the '`' character do in Perl?
(3 answers)
Closed 8 years ago.
I am a beginner in perl.
Just wanted to understand the following code.
sub get_files {
foreach my $customer (keys %customers){
lg("Getting files from ftp for customer $customer", "LOG");
my $ftp_server = $customers{$customer}{'FtpServer'};
my $ftp_user = $customers{$customer}{'FtpUser'};
my $ftp_pass = $customers{$customer}{'FtpPass'};
my $datadir = $datafiles.$customer."/";
`$get_files $ftp_server $ftp_user $ftp_pass $datadir`;
}
}
What does last line in the above subroutine tells?
It invokes the command that's in the string $get_files, passing the command the remaining strings as parameters.
Usually it's used if you want to capture the resulting output and store it in a variable. In this case where the result is being discarded it would be more usual to use system instead:
system $get_files, $ftp_server, $ftp_user, $ftp_pass, $datadir;
although if the command does then produce any output it'll appear on-screen instead of being absorbed by the back-ticks operator.

How to redirect Matlab output of a command to a file? [duplicate]

This question already has answers here:
Print A Matlab Struct to a Text File
(4 answers)
Closed 9 years ago.
I want to redirect or copy the output of a Matlab command to a file. How can I do that?
In my case, I have two large structs that I want to compare using the UNIX tool diff.
Example: I can do this in Matlab:
>> s1
s1 =
a: 32
abc: 'example'
>>
and want a file containing approx:
s1 =
a: 32
abc: 'example'
These solutions are not viable:
Copy-pase: can't automate (comfortably).
save -ascii: does not work with structs.
Have a look at the diary function. E.g.
diary my_file.txt
s1
diary off
The file my_file.txt will then contain exactly what you see on screen.
If you need to do it more fine grained there is the evalc function that will store the output to a string.
Later you can output the string into any output channel matlab offers.