In my first post on Powershell/PowerCLI I had ventured the guess that many of the tasks to setup PowerShell/PowerCLI could be scripted. Today I present the script:
Set-ExecutionPolicy Bypass -force
mkdir $env:userprofile\Documents\WindowsPowerShell
mkdir "C:\PowerCLI codes"
$fixPS = @"
Set-Location "C:\PowerCLI codes"
if (`$psISE)
{
Clear-Host
Write-Host 'You can do this!'
}
"@
$fixPS | Out-File -FilePath $env:userprofile\Documents\WindowsPowerShell\profile.ps1 -Encoding ASCII
You can change the directory for your codes and the startup message, and add or remove things, but this will at least create the proper file to set your working directory, and write all lines to the profile.ps1 file.
So to use this:
1) Find powershell on your computer
2) right click, run as administrator
3) paste and execute. This should not prompt you for any questions
4) close that window
5) Use ISE or powershell like your normally would from now on
Let me know if you found this useful. I needed it since I have several jumpboxes and create new VMs, and figuring out where stuff needs to be changed takes much more time than copy pasting my setup in a script :)
Showing posts with label PowerShell. Show all posts
Showing posts with label PowerShell. Show all posts
Tuesday, February 28, 2017
Monday, February 15, 2016
Powershell and PowerCLI - getting started
Here's a small collection of links and tips for getting started with PowerShell/PowerCLI
PowerCLI main link
https://www.vmware.com/support/developer/PowerCLI/
PowerCLI Change Log (great place to see the latest PowerCLI version and release notes)
https://www.vmware.com/support/developer/PowerCLI/changelog.html
Note each release note includes a link to the software needed to run PowerCLI. It's called the compatibility matrix.
https://www.vmware.com/support/developer/PowerCLI/doc/powercli60r3-compat-matrix.html
In my case for 6.0r3 you want to
The mentioned windows OSs bring a version of Powershell by default. Find out the version of powershell running on your system with this command:
$psversiontable [enter]
For example, on my Windows 7 SP1 VM, Powershell is at version 2, so it would need to be upgraded first (with a corresponding .net upgrade). Windows 8.1 has version 3 and you would be good to go.
(Server 2016 and Windows 10 have PowerShell version 5. Yes, releases have gone in quick succession, mostly because of DSC. You can read about changes here. Keep in mind it's not officially supported in PowerCLI yet, but it will be, inevitably.)
Chris Wahl has an excellent series of posts on PowerShell here. I especially found this post useful to start setting up your environment:
http://wahlnetwork.com/2015/08/03/powershell-integrated-scripting-environment-ise/
This 15 minute vBrownBag tech talk is about that post and also about version control with Git.
https://www.youtube.com/watch?v=Pgt54U21LPo
I found this post helpful as well, even if from 2012
http://www.virten.net/2012/09/getting-started-with-powercli-5-1-and-powershell-3-0/
This is my version of the startup script Chris guides you through setting up. I place it in different locations whether it's work (so it only affects my user) or home machine.
profile.ps1 contents:
Set-ExecutionPolicy Bypass
Set-Location "E:\PowerCLI codes"
if ($psISE)
{
Clear-Host
Write-Host 'Oh it is YOU again'
}
I totally took the advice from Chris. This cracks me up since I don't use PowerCLI that often! Mad shout out to him since he was very helpful to me throughout the creation of this post :D
All nice and good in PowerShell, but by default, running the PowerCLI icon on the desktop will not obey the Set-Location, and it will complain because Set-ExecutionPolicy requires running as Administrator.
To fix this:
1) Right click the PowerCLI icon, Click Advanced, set to Run as Administrator.
2) Look at the target on the shortcut. PowerCLI runs its own initialization script, in my case:
C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1
This file is set to read-only. Make a backup, fix the permissions until you can save. In my case, for it to respect the path I want, I went to line 269 of the 6.0r3 script which has a "cd \" command. I commented it out and now my desired location in profile.ps1 works.
If you are going to use the 32-bit version of PowerCLI, you would also have to check that shortcut and do the respectful edits. I have not yet found the need to use that link though.
I'm thinking there's probably a way to have all these little customization tasks scripted so in theory you could just open a command line with administrator privileges, paste your instructions (which would include things such as create file, insert file contents, save, change permissions and file attributes, modify shortcuts, replace string) and not have to make the process of setting up the environment so manual. The ideal scenario is after pasting the instructions, you would close that cmd, double click the PowerCLI icon and you are ready to go.
I'm only thinking this is possible because PowerShell is meant to be able to do anything you can normally do on Windows GUI. I'm sure as releases of PowerCLI change things you would need to adjust the script a bit, but if you use several jumpboxes this could save you a lot of time.
PowerShell / PowerCLI feels very powerful. Also have to say having the ISE installed by default is pretty cool. Get in on this, it's looking like it will be quite fun!
PowerCLI main link
https://www.vmware.com/support/developer/PowerCLI/
PowerCLI Change Log (great place to see the latest PowerCLI version and release notes)
https://www.vmware.com/support/developer/PowerCLI/changelog.html
Note each release note includes a link to the software needed to run PowerCLI. It's called the compatibility matrix.
https://www.vmware.com/support/developer/PowerCLI/doc/powercli60r3-compat-matrix.html
In my case for 6.0r3 you want to
- run on 2008R2 sp1, 2012R2, 7 sp1, 8.1
- have PowerShell 3 or 4
- Have .Net framework 4.5 or newer
The mentioned windows OSs bring a version of Powershell by default. Find out the version of powershell running on your system with this command:
$psversiontable [enter]
For example, on my Windows 7 SP1 VM, Powershell is at version 2, so it would need to be upgraded first (with a corresponding .net upgrade). Windows 8.1 has version 3 and you would be good to go.
(Server 2016 and Windows 10 have PowerShell version 5. Yes, releases have gone in quick succession, mostly because of DSC. You can read about changes here. Keep in mind it's not officially supported in PowerCLI yet, but it will be, inevitably.)
Chris Wahl has an excellent series of posts on PowerShell here. I especially found this post useful to start setting up your environment:
http://wahlnetwork.com/2015/08/03/powershell-integrated-scripting-environment-ise/
This 15 minute vBrownBag tech talk is about that post and also about version control with Git.
https://www.youtube.com/watch?v=Pgt54U21LPo
I found this post helpful as well, even if from 2012
http://www.virten.net/2012/09/getting-started-with-powercli-5-1-and-powershell-3-0/
This is my version of the startup script Chris guides you through setting up. I place it in different locations whether it's work (so it only affects my user) or home machine.
profile.ps1 contents:
Set-ExecutionPolicy Bypass
Set-Location "E:\PowerCLI codes"
if ($psISE)
{
Clear-Host
Write-Host 'Oh it is YOU again'
}
I totally took the advice from Chris. This cracks me up since I don't use PowerCLI that often! Mad shout out to him since he was very helpful to me throughout the creation of this post :D
All nice and good in PowerShell, but by default, running the PowerCLI icon on the desktop will not obey the Set-Location, and it will complain because Set-ExecutionPolicy requires running as Administrator.
To fix this:
1) Right click the PowerCLI icon, Click Advanced, set to Run as Administrator.
2) Look at the target on the shortcut. PowerCLI runs its own initialization script, in my case:
C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1
This file is set to read-only. Make a backup, fix the permissions until you can save. In my case, for it to respect the path I want, I went to line 269 of the 6.0r3 script which has a "cd \" command. I commented it out and now my desired location in profile.ps1 works.
If you are going to use the 32-bit version of PowerCLI, you would also have to check that shortcut and do the respectful edits. I have not yet found the need to use that link though.
I'm thinking there's probably a way to have all these little customization tasks scripted so in theory you could just open a command line with administrator privileges, paste your instructions (which would include things such as create file, insert file contents, save, change permissions and file attributes, modify shortcuts, replace string) and not have to make the process of setting up the environment so manual. The ideal scenario is after pasting the instructions, you would close that cmd, double click the PowerCLI icon and you are ready to go.
I'm only thinking this is possible because PowerShell is meant to be able to do anything you can normally do on Windows GUI. I'm sure as releases of PowerCLI change things you would need to adjust the script a bit, but if you use several jumpboxes this could save you a lot of time.
PowerShell / PowerCLI feels very powerful. Also have to say having the ISE installed by default is pretty cool. Get in on this, it's looking like it will be quite fun!
Saturday, February 6, 2016
Why learn Powershell?
PowerShell is a Microsoft technology that runs on Windows and is both a command prompt and a scripting language. As I write this, it's almost 10 years old - it formally first appeared integrated as part of the operating system in Windows Server 2008. It replaces the Windows NT cmd.exe and can be installed as far as Windows XP.
Why learn Powershell?
So, while there are many ways of doing things, this does not seem to be a fad that will go away soon. In the worst case, your Windows admin skills get a boost, and in the best case, you can manage several technologies with the same base behavior. Thus, I will now make more of an effort to learn PowerShell properly, primarily for VMware scripting, but paying attention to MS and *nix applications as well.
I leave you with some good links:
Main site
https://msdn.microsoft.com/powershell
Learning Powershell
https://technet.microsoft.com/en-us/scriptcenter/dd742419.aspx
Official PowerShell blog
https://blogs.msdn.microsoft.com/powershell/
Hey Scripting Guy! Blog (long running Microsoft scripting blog)
https://blogs.technet.microsoft.com/heyscriptingguy/
Why learn Powershell?
- If you run Windows systems, this is now your shell and gateway into automation
- If you run VMware, PowerCLI is based on Powershell and is the current best way to script and automate
- Microsoft has opened their eyes that the world runs a lot of Linux. Powershell DSC supports administering Linux and has announced it will natively support SSH , and put its money where its mouth is by donating to the OpenBSD project, by itself and also as part of the Core Infrastructure Initiative.
So, while there are many ways of doing things, this does not seem to be a fad that will go away soon. In the worst case, your Windows admin skills get a boost, and in the best case, you can manage several technologies with the same base behavior. Thus, I will now make more of an effort to learn PowerShell properly, primarily for VMware scripting, but paying attention to MS and *nix applications as well.
I leave you with some good links:
Main site
https://msdn.microsoft.com/powershell
Learning Powershell
https://technet.microsoft.com/en-us/scriptcenter/dd742419.aspx
Official PowerShell blog
https://blogs.msdn.microsoft.com/powershell/
Hey Scripting Guy! Blog (long running Microsoft scripting blog)
https://blogs.technet.microsoft.com/heyscriptingguy/
Subscribe to:
Comments (Atom)