Setting variables in PowerShell itself is trivial, merely assign (= operator) to the variable:
$VarName = 42
But likely the question is looking for a way to set Environment variables which the (VSTS) application can 'see' and 'use' when it is called from PowerShell.
To assign to any session Environment variable prefix the name with "ENV" so it looks like this:
$Env:VarName = 4201
This will remain for the current PowerShell session or life of the console -- other sessions in other windows or run later will not see or be affected by such settings.
If you wish to have a persistent Environment variable then you must set that in the registry, either for the User (HKey_Local_User) or Computer (Hkey_Local_Machine).
These will get the environment settings from the registry (maybe be different that currently set in your process):
[Environment]::GetEnvironmentVariable('path',[EnvironmentVariableTarget]::Machine)
[Environment]::GetEnvironmentVariable('path',[EnvironmentVariableTarget]::User)
And the Set commands are similar but include the new value (e.g., variable 'Tools' is set to 'C:\':
[Environment]::SetEnvironmentVariable('Tools', 'C:\', [EnvironmentVariableTarget]::Machine)
[Environment]::SetEnvironmentVariable('Tools', 'C:\', [EnvironmentVariableTarget]::User)
There is also a "location" enumeration for "Process" (instead of User or Machine) but it is simpler to set this using the PowerShell $Env:VariableName shown above.
[Environment]::SetEnvironmentVariable('Tools', 'C:\', [EnvironmentVariableTarget]::Process)
Answer from HerbM on Stack OverflowSetting variables in PowerShell itself is trivial, merely assign (= operator) to the variable:
$VarName = 42
But likely the question is looking for a way to set Environment variables which the (VSTS) application can 'see' and 'use' when it is called from PowerShell.
To assign to any session Environment variable prefix the name with "ENV" so it looks like this:
$Env:VarName = 4201
This will remain for the current PowerShell session or life of the console -- other sessions in other windows or run later will not see or be affected by such settings.
If you wish to have a persistent Environment variable then you must set that in the registry, either for the User (HKey_Local_User) or Computer (Hkey_Local_Machine).
These will get the environment settings from the registry (maybe be different that currently set in your process):
[Environment]::GetEnvironmentVariable('path',[EnvironmentVariableTarget]::Machine)
[Environment]::GetEnvironmentVariable('path',[EnvironmentVariableTarget]::User)
And the Set commands are similar but include the new value (e.g., variable 'Tools' is set to 'C:\':
[Environment]::SetEnvironmentVariable('Tools', 'C:\', [EnvironmentVariableTarget]::Machine)
[Environment]::SetEnvironmentVariable('Tools', 'C:\', [EnvironmentVariableTarget]::User)
There is also a "location" enumeration for "Process" (instead of User or Machine) but it is simpler to set this using the PowerShell $Env:VariableName shown above.
[Environment]::SetEnvironmentVariable('Tools', 'C:\', [EnvironmentVariableTarget]::Process)
You need to use Write-Host with a special string format:
Write-Host "##vso[task.setvariable variable=YourVSTSVariableName]$yourPowershellVariable"
Setting local variables in Powershell/CMD
azure devops - How do you set a variables in powershell - Stack Overflow
In Powershell, how to set variable values within a function and have that value available in the parent scope? - Stack Overflow
I want to define a variable but not call it right away
Videos
Trying to get a quick script that would determine if a specific system process is running.
I am running into an issue where I cannot set a local variable.
C:\WINDOWS\system32>var1 = 777
'var1' is not recognized as an internal or external command, operable program or batch file.
This seems to be an issue with more than one of our endpoints. Is there a way to fix this issue using the terminal?
Setting variables in PowerShell itself is trivial, merely assign (= operator) to the variable:
$VarName = 42
But likely the question is looking for a way to set Environment variables which the (VSTS) application can 'see' and 'use' when it is called from PowerShell.
To assign to any session Environment variable prefix the name with "ENV" so it looks like this:
$Env:VarName = 4201
This will remain for the current PowerShell session or life of the console -- other sessions in other windows or run later will not see or be affected by such settings.
If you wish to have a persistent Environment variable then you must set that in the registry, either for the User (HKey_Local_User) or Computer (Hkey_Local_Machine).
These will get the environment settings from the registry (maybe be different that currently set in your process):
[Environment]::GetEnvironmentVariable('path',[EnvironmentVariableTarget]::Machine)
[Environment]::GetEnvironmentVariable('path',[EnvironmentVariableTarget]::User)
And the Set commands are similar but include the new value (e.g., variable 'Tools' is set to 'C:\':
[Environment]::SetEnvironmentVariable('Tools', 'C:\', [EnvironmentVariableTarget]::Machine)
[Environment]::SetEnvironmentVariable('Tools', 'C:\', [EnvironmentVariableTarget]::User)
There is also a "location" enumeration for "Process" (instead of User or Machine) but it is simpler to set this using the PowerShell $Env:VariableName shown above.
[Environment]::SetEnvironmentVariable('Tools', 'C:\', [EnvironmentVariableTarget]::Process)
You need to use Write-Host with a special string format:
Write-Host "##vso[task.setvariable variable=YourVSTSVariableName]$yourPowershellVariable"
When defining variables in PowerShell, single quotes (') mean you want the literal version of the string. Use double-quotes (") if you want to allow variable expansion:
PS C:\> $a = "hello"
PS C:\> $a
hello
PS C:\> $b = "$a world!"
PS C:\> $b
hello world!
More info:
- Single Quotes vs. Double Quotes in PowerShell: What's the Difference?
- Referencing Variables and Variable Values
Edit after comments:
For your example where you're pulling the line from a file, that's a little trickier since it's pulling the line as a literal string.
The easiest way (IMO) would be to use the Replace method; something like:
$selHost = (get-content c:\scripts\hosts.txt)[0]
$a = ((get-content c:\scripts\config.txt)[1]).replace('$selhost', $selHost)
When you read a string that contains a variable name out of a file and into a variable, you are going to need something other than double quotes to cause string expansion. There is a tool that's relevant here. It's called ExpandString. Take a look at this sample code:
$selhost = 'spr-it-minion'
$b = '$selhost is offline!'
$c = $ExecutionContext.InvokeCommand.ExpandString($b)
$c
What's going on here is that first I have given $selhost and $b literal values, similar to the ones you would read out of the files you are using. Of course, $b isn't right, because the reference to $selhost isn't resolved, as outlined in the accepted answer. But $c gets the value produced by expanding $selhost, which you can use in your output.
I'll leave applying this to your case as a coding exercise.
The variables are created in your function's local-scope. Those variables are deleted when your function is done.
Global: The scope that is in effect when Windows PowerShell starts. Variables and functions that are present when Windows PowerShell starts have been created in the global scope. This includes automatic variables and preference variables. This also includes the variables, aliases, and functions that are in your Windows PowerShell profiles. Local: The current scope. The local scope can be the global scope or any other scope. Script: The scope that is created while a script file runs. Only the commands in the script run in the script scope. To the commands in a script, the script scope is the local scope.
Source: about_Scopes
If you need the variables to be available for the script, then write them to the script scope.
$BackupFile = $null
$TaskSequenceID = $null
$OSDComputerName = $null
$capturedWimPath = $null
Function Set-OsToBuild
{
switch ($OsToBuild)
{
"Win7x64"
{
$script:BackupFile = "Win7x64-SP1.wim"
$script:TaskSequenceID = "WIN7X64BC"
$script:OSDComputerName = "Ref-Win7x64"
$script:capturedWimPath = "$($PathToMdtShare)\Captures\$BackupFile"
}
}
}
If you would like to keep the values for whole sessions (until you close the powershell-process), then you should use the global scope.
$global:BackupFile = $null
$global:TaskSequenceID = $null
$global:OSDComputerName = $null
$global:capturedWimPath = $null
Function Set-OsToBuild
{
switch ($OsToBuild)
{
"Win7x64"
{
$global:BackupFile = "Win7x64-SP1.wim"
$global:TaskSequenceID = "WIN7X64BC"
$global:OSDComputerName = "Ref-Win7x64"
$global:capturedWimPath = "$($PathToMdtShare)\Captures\$BackupFile"
}
}
}
The powershell about_scope help document is what you want to read for this.
Specifically this section:
Windows PowerShell Scopes
Scopes in Windows PowerShell have both names and numbers. The named scopes specify an absolute scope. The numbers are relative and reflect the relationship between scopes. Global: The scope that is in effect when Windows PowerShell starts. Variables and functions that are present when Windows PowerShell starts have been created in the global scope. This includes automatic variables and preference variables. This also includes the variables, aliases, and functions that are in your Windows PowerShell profiles. Local: The current scope. The local scope can be the global scope or any other scope. Script: The scope that is created while a script file runs. Only the commands in the script run in the script scope. To the commands in a script, the script scope is the local scope. Private: Items in private scope cannot be seen outside of the current scope. You can use private scope to create a private version of an item with the same name in another scope. Numbered Scopes: You can refer to scopes by name or by a number that describes the relative position of one scope to another. Scope 0 represents the current, or local, scope. Scope 1 indicates the immediate parent scope. Scope 2 indicates the parent of the parent scope, and so on. Numbered scopes are useful if you have created many recursive scopes.
So depending on your exact needs you could use any one of the following I believe.
$global:BackupFile = "Win7x64-SP1.wim"$script:BackupFile = "Win7x64-SP1.wim"$1:BackupFile = "Win7x64-SP1.wim"
Hopefully this isnt a silly question.
Basically I'm trying to write this script to search the directory for a folder, move it to an archive, create a new folder based on deployment number, and copy over files from various locations. The problem I'm running into is I've defined my variables at the start and it looks like theyre being called when theyre being set. Is it bad practice to set variables like that at the start? I can shift the order around but I wanted to check on this.
[CmdletBinding()]
param
(
$deploymentNumber = ""
)
#Variables defined
$deploymentNumber = Read-Host "Enter Deployment Number"
$f = New-Item -path ".\z_PRE_Deployment_$($deploymentNumber)" -ItemType Directory -Force
$oldBackup = Test-path '.\z_PRE_Deployment*'
$copy1 = copy-item -path "C:\Users\michael\Documents\PA Flow Backups\*" -destination $f.FullName -recurse
#Check in folder for previous backups and move them to an archive folder before proceeding
if (Test-path '.\z_PRE_Deployment*') {
# write-verbose "Searching for previous deployment folder..."
# gci '.\z_PRE_Deployment*' | Move-Item -destination '.\z_Archive'
# write-verbose "Moving previous deployment folder to archive..."
"true"
} else {
# write-verbose "Previous deployment backup not found..."
# write-verbose "Creating new folder for deployment $deploymentNumber..."
"false"
}
# $f
# write-verbose "Creating new folder for deployment '$deploymentNumber'..."
# write-verbose "Folder created successfully..."
# write-verbose "Copying backup files..."
# $copy1Hello,
Please anyone help me where to use set-variable or new-variable. And when I use [cmdletbinding()] and after that I use set-variable then it is giving unexpected statement.
Thanks