Return value of environment variable with powershell - powershell

I have the name of an environment variable in a variable and I want to get the value. How do I do that? I've tried:
PS C:\Users\Joe> $v="USERDOMAIN"
PS C:\Users\Joe> "$env:$v"
At line:1 char:2
+ "$env:$v"
+ ~~~~~
Variable reference is not valid. ':' was not followed by a valid variable name character. Consider using ${} to
delimit the name.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : InvalidVariableReferenceWithDrive
PS C:\Users\Joe> "$env:$($v)"
At line:1 char:2
+ "$env:$($v)"
+ ~~~~~
Variable reference is not valid. ':' was not followed by a valid variable name character. Consider using ${} to
delimit the name.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : InvalidVariableReferenceWithDrive

Two lines
$v = "Path"
(get-item env:$v).Value
One line
iex ('$env:' + $x)

To complement Vladimir's helpful answer with a solution that makes direct use of the .NET framework:
$v="USERDOMAIN"
[Environment]::GetEnvironmentVariable($v)

Related

Cannot bind argument to parameter 'Name' because it is an empty string

I am trying to install Sitecore 9 and I am receiving the following error:
Install-SitecoreConfiguration : Cannot bind argument to parameter 'Name' because it is an empty string.
At line:30 char:1
+ Install-SitecoreConfiguration #solrParams
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Install-SitecoreConfiguration
[TIME] 00:00:00
Invoke-ManageServiceTask : Cannot bind argument to parameter 'Name' because it is an empty string.
At C:\Program Files\WindowsPowerShell\Modules\SitecoreInstallFramework\2.1.0\Public\Install-SitecoreConfiguration.ps1:641 char:47
+ & $entry.Task.Command #paramSet | Out-Default
+ ~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Invoke-ManageServiceTask], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Invoke-ManageServiceTask
I had another few errors prior which I resolved. This one I am scratching my head at. I traced it back to the Install-SitecoreConfiguration file where the $entry is however I cannot understand what I am looking for as this is fully new to me. I deleted the certificates etc previously as that was causing another error.
Any additional questions are welcomed that I am provide more information to, right now I am unsure what more could be useful. I have attached picture as well.

How to set environment variable with dot in name using powershell?

I want to set environment variable in powershell that has dot in name.
This line throws an error:
$env:Test.Env.Var1 = "test111"
The property 'Var1' cannot be found on this object. Verify that the property exists and can be set.
At line:1 char:1
+ $env:Test.Env.Var1 = "test111"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound
Is it possible to set "Test.Env.Var1" variable without using [Environment]::SetEnvironmentVariable method?
This can be done by
${env:test.value} = 'value'
Note the colon after env. This works in both powershell and pwsh
Cf. https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_variables?view=powershell-7.2#variable-names-that-include-special-characters
It can be done by
${env:Test.Env.Var1} = 'value'
source
Thanks to #4c74356b41 who provided answer in comment.

how to execute an exe with variable value for path and arguments

I'm trying to execute in my powershell script the command below :
D:\Apps\Documentum\product\7.3\bin\idql.exe -udmadmin -pPassword dctm04 -RC:\temp\documentum\session_list.txt -w20 > C:\temp\documentum\session_logstash.txt
In my powershell script I do that:
$DOCBASE="dctm04"
$USER_DOCBASE="dmadmin"
$USER_PWD="Password01"
$IDQL_PATH="D:\Apps\Documentum\product\7.3\bin"
$QRY_SESSIONS="C:\temp\documentum\session_list.txt"
$QRY_LOG_SESSIONS="C:\temp\documentum\session_logstash.txt"
$IDQL_PATH\idql.exe -u$USER_DOCBASE -p$USER_PWD $DOCBASE -R$QRY_SESSIONS -w20 > $QRY_LOG_SESSIONS
But it doesn't work properly, I receive the error below :
At C:\temp\documentum\Generate.ps1:49 char:13
+ $IDQL_PATH\idql.exe -u$USER_DOCBASE -p$USER_PWD $DOCBASE -R$QRY_SESSIONS -w20 ...
+ ~~~~~~~~~
Unexpected token '\idql.exe' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParseException
+ FullyQualifiedErrorId : UnexpectedToken
I think, i don't use variable properly on my command.
Please note my powershell version is :
PS C:\temp\documentum> $PSVersionTable.PSVersion
Major Minor Build Revision
----- ----- ----- --------
4 0 -1 -1
could you give me the solution in order to solve my problem
The reason is that combining a string to executable name makes no sense to Powershell's parsing rules. Use the call operator & or Invoke-Item. Like so,
$ssms="C:\Program Files (x86)\Microsoft SQL Server\140\Tools\Binn\ManagementStudio"
PS C:\> $ssms\ssms.exe
At line:1 char:6
+ $ssms\ssms.exe
+ ~~~~~~~~~
Unexpected token '\ssms.exe' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
C:\>& $ssms\ssms.exe
# Launches SSMS succesfully
C:\>Invoke-Item $ssms\ssms.exe
# Launches SSMS succesfully
There's nice a document about running executables.

Converting time stamp in powershell

I get the below time stamp from API response how do i convert this to human readable text in power shell, i tried below but it is throwing error.
PS C:\Users\foobar\ddd> [datetime]::ParseExact('20100804T104413+0100','
yyyyMMdd'T'HHmmsszzz',[System.Globalization.CultureInfo]::InvariantCulture)
At line:1 char:57
+ [datetime]::ParseExact('20100804T104413+0100','yyyyMMdd'T'HHmmsszzz', ...
+ ~
Missing ')' in method call.
At line:1 char:57
+ ... me]::ParseExact('20100804T104413+0100','yyyyMMdd'T'HHmmsszzz',[System ...
+ ~~~~~~~~~~~~
Unexpected token 'T'HHmmsszzz'' in expression or statement.
At line:1 char:69
+ ... e]::ParseExact('20100804T104413+0100','yyyyMMdd'T'HHmmsszzz',[System. ...
+ ~
Missing argument in parameter list.
At line:1 char:122
+ ... dd'T'HHmmsszzz',[System.Globalization.CultureInfo]::InvariantCulture)
+ ~
Unexpected token ')' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingEndParenthesisInMethodCall
20170125T153341-050020170125T153344-0500
Your only problem seems to be the errant (lack of) quoting of T in your code; removing it seems to work fine:
[datetime]::ParseExact('20100804T104413+0100','yyyyMMddTHHmmsszzz', $null)
Also, since you're providing a format string in which all characters are specified individually and numerically, you needn't specify a culture (passing $null, which defaults to the current culture, will do).

Powershell: Adding element to hashtable failed

PS C:\Users\Hind> $b=#{}
PS C:\Users\Hind> $b+={k="a";v="b"}
A hash table can only be added to another hash table.
At line:1 char:1
+ $b+={k="a";v="b"}
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : AddHashTableToNonHashTable
Why did it fail? How can I add one element to a hashtable successfully?
Correction, this fails because you are missing the # character in front of #{k="a";b="b"}
PS C:\Users\Hind> $b=#{}
PS C:\Users\Hind> $b+=#{k="a";v="b"}
#{} is declaring a new hash table. {} is a script block. They are not the same.
Initialising the hashtable should be with round bracket instead of curly brackets
$b=#()
$b+=#{k="a";v="b"}