we have Octopus-2.0.
We stored the service credentials (they are per machine) in octopus user variable.
I need to create these login in SQL server as well.
For Example service login name "machine1_service1" stored as variable name. and password of that login is stored under column Variable Value in octopus.
so far i know that to any variable value from octopus we need to provide exact variable name. but in this case I actually need to get list of all these variables.
Is there a way to accomplish this?
Yes.
Octopus variables are accessible from a dictionary object that can be enumerated. If you follow a naming convention you could query the dictionary using powershell with something like the following. This would be called from within a custom step or somewhere where you can write your own powershell e.g. a PostDeploy.ps1 script in the .nuget file
Let's say the variables are defined like this
You can use this powershell to get to them and enumerate round them
# Get service accounts
$serviceAccounts = $OctopusParameters.keys | ? {$_ -like "service-*"}
write-host "Accounts found:" $serviceAccounts.count
foreach($account in $serviceAccounts)
{
write-host "Account: $account"
$password = $OctopusParameters[$account]
write-host "Password: $password"
}
Hope this helps.
Related
I'm trying to use Azure powershell to pull an SSH key and add it to a VM.
The cmdlet is
Get-AzKeyVaultKey ... -OutFile filename
I'd like to avoid actually writing the key to the disk, but I need it in a variable. Is there any way to provide a variable acting like a file or something so I can go
-OutFile $someVariablePretendingToBeFile
and use that variable please?
The variable that is returned by Get-AzKeyVaultKey is of type PsKeyVaultKey
if I get its key property, and call ToRSA() I get an RSACryptoServiceProvider
But I still don't see where to get the public key string from!
It's annoying b/c -OutFile produces exactly the public key
Thanks
Since Get-AzKeyVaultKey is not providing a way of doing (that I know of), can you get it to work with a simple :
$key=(Get-AzKeyVaultKey XXX)
To get the result in a variable ?
Let us know !
Not sure if tis would work, it is a variant of the answer above. I can't test it just now
$PublicKey = Get-AzKeyVaultKey -VaultName $vaultName -KeyName $keyName
I am trying to learn PowerShell.
$newuser = ‘ash’, ‘bob’, ‘charlie’, ‘david’
$newuser | foreach({New-LocalUser -Name $_newuser -Password “123123” -Description “Students”})
What I am trying to do is to create new local accounts with each name on the list, $newuser, with the password and Description I have.
I have googled a lot and tried other things, but no luck.
Do I need to create a list using this syntax? $newuser = #(ash, bob, charlie, david)
Also I am confused with what to put after -Name.
Tried something like -Name $newuser, -Name $__.newuser, -Name $_.($newuser), etc.
I have been looking for any method to call each object from the $newuser for creating each user account.
Any suggestion will be appreciated!!
You're close to getting where you want!
Declaring Arrays: You'll notice that when you try to use $newuser = #(ash, bob, charlie, david) in PowerShell ISE or VSCode, you'll get a syntax error. This is because you need to place quotes around each item. The way you've done it originally is no different in practice. It is simply up to how you want to define arrays. Sometimes with large arrays that expand multiple lines, it can be easier to keep track of where it ends when wrapped with #()
Understanding your variables: You can test the two array declaration methods by trying both ways, and using the GetType() method like so: $newuser.GetType()
Naming Conventions: Try to use plurals for variable names when dealing with something that contains multiple objects, like an array. So in this case, $newuser should be $newusers, and try to make your variables descriptive of what they contain. (You've done that nicely here)
$PSItem and $_: When you iterate over an array, there is a nifty PowerShell variable called $PSItem, also called by alias $_. This variable contains the current object you are iterating over. See the example below for how I might do something like this, but also keep in mind there are multiple ways to accomplish things in PowerShell most of the time.
Passwords: The cmdlet you are using here will not take a string as a password, but instead will require a SecureString. You can force the string to a SecureString in the script, but only do this as practice. The best thing to do is have a file that you call from and shred afterwards or external password manager that you call upon when needed to pass the value. PlainTexting the password is NEVER a good idea.
Method 1
$newUsers = ‘ash’, ‘bob’, ‘charlie’, 'david’
$securePassword = ConvertTo-SecureString '123123' -AsPlainText -Force
foreach ($user in $newUsers) {
New-LocalUser -Name $user -Password $securePassword -Description 'Students'
}
Method 2
$securePassword = ConvertTo-SecureString '123123' -AsPlainText -Force
‘ash’, ‘bob’, ‘charlie’, 'david’ | ForEach-Object {New-LocalUser -Name $PSItem -Password $securePassword -Description 'Students'}
This is mostly personal preference, or you'd follow the standard your team uses, but I'd say the first is what I would use in a script, as I find it a bit more readable, and the second is something I would do on the fly in a terminal.
I am using Octopus for getting my environment ready with all prerequisites. In one of the octopus step I want some varaibles values to be set dynamically for which I have used the octopus output variables.
If I want to declare the output variable in form of an array, its value not getting read in other step where I want to read that.
Below are few code snippets which I have used (Note : both the steps are having inline powershell script code written):
/*Step A code : Set output variable of step code*/
$market= "RO" /*Dynamically identify market depends on machine name */
write-host $market
Set-OctopusVariable -name "TestResult_Domain_['+%market%+']" -value
"DomainNameOfMachine"
/*Another step code*/
$market= "RO" /*Dynamically identify market depends on machine name */
write-host $market
$filterDomain = "Octopus.Action[StepA].Output.TestResult_Domain_['$market']"
I am trying to assign value to built-in release notes variable in "Run a Script" step.
$OctopusParameters["Octopus.Release.Notes"] = "Some release notes"
In the next step "Send an Email" I am using this variable in email body, but unfortunately it is empty.
<p>#{Octopus.Release.Notes}</p>
Is it possible to set Octopus Deploy system variable value from PowerShell and use it in the next step?
I am using Octopus Deploy 3.7.11.
EDIT:
I have also tried the cmdlet Set-OctopusVariable and it did not work.
Set-OctopusVariable -name "Octopus.Release.Notes" -value "Something"
I don't think it is possible to overwrite values of the built-in variables provided by Octopus Deploy. But you could define your own output variable and refer to that in the following steps. For example in your 'Run a script'-step use:
Set-OctopusVariable -name "MyReleaseNote" -value "Some text here"
Then the "Send an Email"-step can refer to this text by using the following (assuming the first step is called 'FirstStep'):
#{Octopus.Action[FirstStep].Output.MyReleaseNote}
The variable can also be used from a script in other steps, then use the syntax:
$relnote = $OctopusParameters["Octopus.Action[FirstStep].Output.MyReleaseNote"]
(If you want to save the generated releasenote perhaps you could save it as an 'artifact' in the project).
I tried this using Octoposh. Modifying an existing variable is covered in the Octoposh wiki at Modifying Variables - Edit a variable of a Project/Library variable set.
I wasn't able to get this to work because of timeouts on our network, but it looks like it should work - just not as straight-forward as I expected.
I need to create a logon script that lists the following values for each PC
(P+IPAddress) & (S+IPAddress), I managed to create the following .reg file:
HKEY_CURRENT_USER\Serial Numbers
"Printer"="P236"
"Scanner"="S236"
But I have entered these values my self, I need a script that creates a combined value (Letter + The last 3digits of the IP Address of the PC where this script runs).
I highly appreciate it if someone can help me on this.
If you want a single command, use Powershell like this:
powershell -Command "& {[system.net.dns]::gethostaddresses([system.net.dns]::gethostname()) | where { $_.addressfamily -eq 'InterNetwork' } | foreach { [microsoft.win32.registry]::setvalue('hkey_current_user\serial numbers', 'Printer', [string]::concat('P', $_.tostring().split('.')[3])) } }"
Not the most efficient and may have problems with multiple addresses (as it will always set to the last one) but it can be executed as a single command.
Should be easy enough to modify that command to add the Scanner bit too.