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
Related
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.
What I am trying to do is download 2 images from URL's and open them after download. Here's what I have:
#echo off
set files='https://cdn.suwalls.com/wallpapers/cars/mclaren-f1-gtr-42852-400x250.jpg','http://www.dubmagazine.com/home/media/k2/galleries/9012/GTR_0006_EM-2014-12-21_04_GTR_007.jpg'
powershell "(%files%)|foreach{$fileName='%TEMP%'+(Split-Path -Path $_ -Leaf);(new-object System.Net.WebClient).DownloadFile($_,$fileName);Invoke-Item $fileName;}"
Im getting 'Cannot find drive' A drive with the name 'https' cannot be found.
It's the Split-path command that is having problems but cant seem to find a solution.
You could get away with basic string manipulation but, if the option is available, I would opt for using anything else that is data aware. In your case you could use the [uri] type accelerator to help with these. I would also just opt for pure PowerShell instead of splitting between batch and PS.
$urls = 'https://cdn.suwalls.com/wallpapers/cars/mclaren-f1-gtr-42852-400x250.jpg',
'http://www.dubmagazine.com/home/media/k2/galleries/9012/GTR_0006_EM-2014-12-21_04_GTR_007.jpg'
$urls | ForEach-Object{
$uri = [uri]$_
Invoke-WebRequest $_ -OutFile ([io.path]::combine($env:TEMP,$uri.Segments[-1]))
}
Segments will get you the last portion of the url which is a proper file name in your case. Combine() will build the target destination path for you. Feel free to add you invoke item logic of course.
This also lacks error handling if the url cannot be accessed or what not. So be aware of that possibility. The code above was meant to be brief to give direction.
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.
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.
I tried but failed and need an expert help. In a .vimrc I have the following:
set tags=/sandbox/myNameIsSam/tags
This works just fine. Inside gvim, I can load the tags file and everything is awesome. However, I would like for each user to have their own project tags file. How and why is not important here. The below setting doesn't seem to do what I need. Can someone help?
let projectTagFile='/sandbox/'.$USERNAME.'/tags'
set tags=projectTagFile
you should get username by $USER not $USERNAME
What is important is that in this particular case, you cannot use set to set this option because it only allows you to set it to a single literal value. However, with let, you can use Vimscript to obtain the value using a function, for example, or in your case, through concatenation (read more about about options here). Try this instead:
let &tags='/sandbox/'.$USER.'/tags'
To check if the tags was set correctly, you could execute:
:set tags?
Or, for the fully-qualified path:
:echo &tags
I've always done something like
let tagfile = '/sandbox/' . $USERNAME . '/tags'
execute 'set tags=' . tagfile
but I now like #Kent's solution better. Doing it like that had never crossed my mind.