I'm trying to concatenate a variable reference inside another string but it keeps showing wrong values.
Basically I intend to update variable $arq value acording to variable $version, without having to reset $arq.
I'm using [ref] but not sure it's the best way to do so.
I've tryed the following so far.
$downloadSource = "\\domain.or.ip\folder"
$version = "0.0.0"
$arq = "file_name_$([ref]$version)`_filename_continuation.zip"
function UpdateVersion {
(Get-ChildItem -name "$downloadSource\file_name*").Split('_')[2]
}
$version = UpdateVersion
echo $version
echo $arq
Variable $version is being updated correctly.
But $arq is receiving the wrong value:
"file_name_System.Management.Automation.PSReference`1[System.String]_filename_continuation.zip"
I tried changing $arq as follows
$arq = "file_name_" + $(($versaoEsperada).value) + "`_filename_continuation.zip"
But the same wrong value is shown.
Would you guys help me, please?
Thanks
Related
I'm a beginner at Powershell and am struggling to understand some syntax from some code I found on Github. I've read the docs on Powershell assignment, and on switch statements, and can't understand what is going on with the = $Yes and = $No in this code snippet:
Switch ($Prompt3) {
Yes {
Stop-EdgePDF
Write-Output "Edge will no longer take over as the default PDF viewer."; = $Yes
}
No {
= $No
}
}
I haven't been able to find any references to this kind of syntax, and it doesn't seem to do anything in the script. So why is it there?
UPDATE: This issue has been resolved.
Looks to me like the variable name that was getting the assignment was deleted in a change back in August.
$PublishSettings = $Yes
Was changed to:
= $Yes
And:
$PublishSettings = $No
Was changed to:
= $No
Looks like poor search and replace.
I've created an issue for the problem at GitHub.
There are many characters that are valid in a function (or variable) name; this includes the = symbol. What you're observing is a function or alias.
Examples:
# standard function
function =
{
return $args
}
# accessing the function: drive
${Function:=} = {
return $args
}
# defining a new alias
New-Alias -Name = -Value Get-Variable
# using the Alias attribute
function Test-Thing
{
[Alias('=')]
param()
return $args
}
I'm trying to import an xml file and store as a variable for the remainder of a powershell session. The import is obviously successful but the variable content does not persist outside of the function.
Function auth
{
$cred = import-clixml -Path c:\temp\cred.xml
}
try this:
Function auth
{
$global:cred = "test"
}
auth
$global:cred
You can use globals as Esperento57 suggests or you can do this
function auth
{
return 'test'
}
$cred = auth
More succinct:
function auth
{
'test'
}
$cred = auth
You need to declare the variable outside the scope of the function first and then inside the function explicitly tell the variable to update using the script:var method.
Here's the example is taken from https://www.kongsli.net/2013/04/25/powershell-gotchas-refer-to-variables-outside-a-function/ to which credit is given.
The thing is that we have to explicitly tell Powershell to update the variable in the parent scope instead of creating a new variable in the current scope.
$x = 1
function changeit {
"Changing `$x. Was: $x"
$script:x = 2
"New value: $x"
}
"`$x has value $x"
changeit
"`$x has value $x"
If you need to do this but with a number of functions and variables, you can place them all into a script and then dotsource the script.
Imagine a script like this:
#MyDevFunctions.ps1
$myImportantVar = "somevar"
$myOtherVar = "ABC123"
Function Get-MyCoolValue(){$myImportantVar}
Write-Host "Finished Loading MyDevFunctions"
If you wanted to run this, and then also persist the values of the variables and also the functions themselves, from your parent script you simply invoke it like so:
PS > . .\MyDevFunctions.ps1
"Finished Loading MyDevFunctions"
PS > $myOtherVar
ABC123
PS> Get-MyCoolValue
someVar
I have the below code snippet:
$Reg2 = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $Machine)
$RegKey2= $Reg.OpenSubKey("SOFTWARE\\TrendMicro\\PC-cillinNTCorp\\CurrentVersion\\Misc.")
$CheckAVVer = $RegKey2.GetValue("ProgramVer")
Whats happening is $CheckAVVer doesn't seem to grab the value. If remove the $CheckAVVer, it works and displays the value.
Please help.
In the second line, change:
$Reg.OpenSubKey("SOFT...
To:
$Reg2.OpenSubKey("SOFT...
And see if that fixes it for you.
$Reg2 = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $Machine)
$RegKey2= $Reg2.OpenSubKey("SOFTWARE\\TrendMicro\\PC-cillinNTCorp\\CurrentVersion\\Misc.")
$CheckAVVer = $RegKey2.GetValue("ProgramVer")
Write-host $CheckAVVer
I'm quite confused with some variable scoping behavior I'm experiencing. Take the following sample module:
$script:intTemplate = 1
[xml]$script:xmlTemplate = #"
<test>
<element>
</element>
</test>
"#
function getvar {
$myint = $script:intTemplate
$myint++
Write-output "Myint is $myint while intTemplate is $intTemplate"
$myxml = $script:xmlTemplate
$e = $myxml.CreateElement("MyChildElement")
$myxml.SelectSingleNode("/test").AppendChild($e) |Out-Null
$myxml.Innerxml.tostring()
$script:xmltemplate.Innerxml.tostring()
}
Now importing that module are running getvar generates:
PS C:\Windows\system32> getvar
Myint is 2 while intTemplate is 1
<test><element></element><MyChildElement /></test>
<test><element></element><MyChildElement /></test>
Further runs of getvar continue to add more and more child elements to $xmlTemplate while $intTemplate remains the same. I don't understand why $myXml is not always starting off with the simple test/element structure and, more to the point, how the script local $xmlTemplate variable appears to be getting changed while intTemplate does not.
Any assistance on figuring out what is going on here would be much appreciated.
David
As far as I understand scope has nothing to do with you problem, when you write $myInt=$script:intTemplate you manipulate a value, so you copy the value. When you write $myxml = $script:xmlTemplate you manipulate a reference, so copying a reference, you work with the same underlying object. Just try $script:intTemplate += 1.
I try to start a function of an script out of another script.
I want to save the return into a variable but this doesn't work.
script1.ps1:
function test
{
return "hallo"
}
script2.ps1:
./script1.ps1; $p=test
or
$p = ./script1.ps1; test
It seems that $p is null, but I don't know what's wrong.
Can anybody please help me?
thx
Try this:
. ./script1.ps1; $p=test
Why: you have to load the function into current scope (that's the period at the beginning – the dot source operator).
If you use ';', then completely new statement begins. So from you example $p = ./script.ps1; test, you assign output from script.ps1 to $p and then run the function.