Using subexpression with & in powershell - powershell

I am completely new to powershell.
I have a requirement to have a set of commands as a subexpression $() because I want the output of the command to be sent to Out-Host and without $() the if loops create an issue.
Now there might also be a possibility that the command has filenames with spaces and to handle that we append & before the file name in command.
Bacially, $(& file_name) | Out-Host fails saying & here is invalid.
How to go about using $() with &

Works ok for me. You'll need to supply a counterexample. In this case you couldn't pipe from "if" without $() or &{}.
$( If (1 -eq 1) { & echo hi } ) | out-host > file
hi
# no output until it's finished
$( If (1 -eq 1) { & 'c:\program files\internet explorer\iediagcmd' } ) |
out-host > file

Related

Powershell: ConvertFrom-Json doesn't get the variable assigned due to forward and backward compatibility

I am trying to load a variables into powershell from variables.json having the following
{
"psfp": "C:/San\/SV65\/ps",
"vmfp": "'C:/San\/SV65\/hyper-packer\/hyper-packer\/output-centos8-9\/Virtual Machines'",
"psmp": "C:/San\/SV65\/ps",
"vmname": ""
}
Trying to import with
$jvariables=Get-Content -Raw -Path '.\variables-ps.json' | ConvertFrom-Json
Output on powershell
PS C:\San\SV65\ps> Write-host $jvariables.psfp
C:/San/SV65/ps
PS C:\San\SV65\ps> Write-host $jvariables.vmfp
'C:/San/SV65/hyper-packer/hyper-packer/output-centos8-9/Virtual Machines'
PS C:\San\SV65\ps> Write-host $jvariables.psmp
C:/San/SV65/ps
These forward slash not incompatible on powershell for a windows path! ConvertFrom-Json doesn't accept backslash on the variables tried with '' as well
Hence could not load my modules on these path
any other way to achieve the above case?
Want to use json as it easy for the end user to update rather a txt files
Please share
you can loop through the object properties and change the path separator like this:
$jvariables.psobject.properties | where {$_.membertype -eq "NoteProperty"} |
foreach {$data.($_.name) = $_.value.replace("/","\")}
or a simpler approach would be to escape backslash in config file, like this :
{
"psfp": "C:\\San\\SV65\\ps",
"vmfp": "'C:\\San\\SV65\\hyper-packer\\hyper-packer\\output-centos8-9\\Virtual Machines'",
"psmp": "C:\\San\\SV65\\ps",
"vmname": ""
}

Invoke-Expression cannot execute multi-line functions

I was trying to run a PowerShell script saved in a text document by using the following:
cat myScript.txt | Invoke-Expression
It works when each command is on its own line but throws an error saying I am missing syntax:
Invoke-Expression: At line:1 char:14
+ function foo {
+ ~
Missing closing '}' in statement block or type definition.
The code in myScript.txt is as follows:
function foo {
param([int]$a, [int]$b)
echo "$a + $b"
}
foo 1 2
Is there a way to run this script without changing the text file to merge all functions to one line?
You can also use out-string to convert output into string.
cat myScript.txt | out-string | Invoke-Expression

How to add a text in a file with an argument in Powershell

I am trying to add the text passed as an argument in powershell to config file. Below is the sample of the code. What I am expecting that below command will read the config file & search for parameter1 & when it find the parameter it will add the value (passed as an argument) after "="
(gc config.params) -replace "Parameter1 =", "$&` $1" | sc inifile.params
So the output supposed to be like:
Parameter1 = hostname
when the following command will be executed:
powershell.exe Untitled1.ps1 hostname
Please suggest.
$1 is not how arguments are passed to PowerShell scripts; they get an array $args or you specify parameter names. And the array does not have the script path as the first element.
So, for your code:
(gc config.params) -replace "Parameter1 =", "$&` $($args[0])" | sc inifile.params
or
param($text)
(gc config.params) -replace "Parameter1 =", "$&` $text" | sc inifile.params

select-string not taking variable in powershell

I have stored a MAC address as a string in a variable:
$macaddress= "1234567"
I'm trying to store the output from a command in another variable:
$testing = { arp -a; | select-string $macaddress }
Write-Host $testing
If the command is executed in PowerShell, I do not see the value of $macaddress. It displays as a '$macaddress' in the screen instead of its value "1234567".
Please help me set the value of $macaddress correctly.
The problem is not how you define variable $macaddress, but in how you try to capture command output.
Remove the enclosing { ... } and the ;:
$testing = arp -a | select-string $macaddress
As for what you tried:
{ ... } creates a script block, which is a piece of source code (loosely speaking) for later execution (e.g., with operators . or &).
If you pass a script block to Write-Host - whose use you should generally avoid, by the way - it is converted to a string, and the string representation is the literal contents of the script block between { and } - that's why you saw $macaddress appear (unexpanded) in your output.
; terminates a command, and it is only necessary if you place multiple commands on a single line.
A pipeline is still considered a single command, even though it is composed of multiple sub-commands; do not attempt to use ; in a pipeline - you'll break it (and, in fact, even your script-block-creating command would break).
Try it this way:
$macAddress = "00-01-02-03-04"
arp -a | Select-String $macAddress
If you want to extract the IP address related to the MAC address, you can do this:
$macAddress = "00-01-02-03-04"
arp -a | Select-String ('\W*((?:[0-9]{1,3}\.){3}(?:[0-9]{1,3}))\W+(' +
$macAddress + ')') | ForEach-Object { $_.Matches[0].Groups[1].Value }

Piping from a variable instead of file in Powershell

Is ther any way in Powershell to pipe in from an virable instead of a file?
There are commands that I need to pipe into another command, right now that is done by first creating a file with the additional commands, and then piping that file into the original command. Code looks somehting like this now:
$val = "*some command*" + "`r`n" + "*some command*" + "`r`n" + "*some command*"
New-Item -name Commands.txt -type "file" -value $val
$command = #'
db2cmd.exe /C '*custom db2 command* < \Commands.txt > \Output.xml'
'#
Invoke-Expression -Command:$command
So instead of creating that file, can I somehow just pipe in $val insatead of Commands.txt?
Try this
$val = #("*some command*1","*some command2*","*some command3*")
$val | % { db2cmd.exe /C $_ > \Output.xml }
You should be able to pipe in from $val provided you use Write-Output or its shorthand echo, but it may also be worth trying passing the commands directly on the command line. Try this (and if it doesn't work I can delete the answer):
PS C:\> filter db2cmd() { $_ | db2cmd.exe ($args -replace '(\\*)"','$1$1\"') }
PS C:\> $val = #"
>> *custom db2 command*
>> *some command*
>> *some command*
>> *some command*
>> "#
>>
PS C:\> db2cmd /C $val > \Output.xml
What happens here is that Windows executables receive their command line from a single string. If you run them from cmd.exe you cannot pass newlines in the argument string, but Powershell doesn't have that restriction so with many programs you can actually pass multiple lines as a single argument. I don't know db2cmd.exe so it might not work here.
The strange bit of string replacement is to handle any double quotes in the arguments: Powershell doesn't quote them and the quoting rules expected by most exe files are a bit bizarre.
The only limitation here would be that $val must not exceed about 32,600 characters and cannot contain nulls. Any other restrictions (such as whether non-ascii unicode characters work) would depend on the application.
Failing that:
echo $val | db2cmd.exe /C '*custom db2 command*' > \Output.xml
may work, or you can use it in combination with the filter I defined at the top:
echo $val | db2cmd /C '*custom db2 command*' > \Output.xml