Tuesday, February 16, 2016

Adding PowerShell code in blog posts

For a while I was adding Powershell code in this blog, and it didn't look as neat as all the other blogs I was visiting, so I looked around, and of course I could have been beautifying my blog for a long long time:

LazyWinAdmin: Blogger - Adding PowerShell code in your blog post...

I looked at the code that LazyWinAdmin provided, but that did not work as well as the Falchion plugin from here. 

Now I can type something in the ISE, select the code and do "CTRL+Shift+C" and it then runs a Powershell script which generates the HTML code which I can paste into the Blogger site. I may eventually create a Github account and publish Powershell code through that, but this is a little bit simpler for now.

That left me with the Powershell window output, which LazyWinAdmin gave CSS code for that you can insert into the custom code for the template of your Blogger site:

CSS Code

.PoshConsole {
  color: #EEEDF0;
  background-color: #012456;
  font-family: consolas;
  font-size: 0.99em;
  padding: .25em;
  padding-top: 0.25em;
  padding-right: 0.25em;
  padding-bottom: 0.25em;
  padding-left: 0.25em;
}

Which can then be used with adding <pre class="PoshConsole">Something</pre> in the HTML editor.

One thing I did notice, is that my black background and gray text did not work so well with all of this beautification, that's why I moved to a more light template.

I noticed some other CSS style sheets in the sourcecode of the LazyWinAdmin page to make different types of styles, but I will let people hunt around for that themselves. He didn't put that in his blogpost, so I'm not sure if he wants the world to see that ;)

Tuesday, June 9, 2015

Rename interfaces based on IP range with Invoke-VMScript

Invoke-VMScript is a lifesaver if you need to do things inside a VM, and you don't want to spend hours logging on to each machine.

Here I needed to rename multiple network interface labels based on IP range:

$renamescript = @'

$adapter1 = Get-NetAdapter | Get-NetIPAddress |where IPaddress -Like "10.168.73.*"| Select interfacealias

$adapter2 = Get-NetAdapter | Get-NetIPAddress |where IPaddress -Like "192.168.72.*"| Select interfacealias

'@

Rename-NetAdapter -Name $adapter1.interfacealias -NewName "Frontend Network"

Rename-NetAdapter -Name $adapter2.interfacealias -NewName "Backend Network"


For each machine you can then do:

Invoke-VMScript -VM $hostname -GuestUser administrator -GuestPassword P4ssw0rd -ScriptText $renamescript

Combine this with the CSV file post I made before, and you can start your script, get a cup of whatever, and have Powershell do the hard work for you.

Reading in a CSV file in Powershell

CSV files make my life a whole lot easier, especially with large numbers. If you have an excel file with a hostname and an IP address, you get a csvfile like:

Server,IPAddress
server1,192.168.1.10
server2,192.168.1.11


You can import it with the following statement:

$csvfile = Import-Csv C:\Book2.csv

foreach ($value in $csvfile){

$hostname = $value.server

$IP = $value.IPAddress

Write-Host "do whatever here to $hostname with IP address: $IP"

}

Monday, May 12, 2014

Forward traffic from one interface to the other interface

Ran into this on ehow just now, while looking at doing the same for linux with netcat:

"netsh interface portproxy add v4tov4 listenport=111 listenaddress=1.2.3.4 connectport=222 connectaddress=7.8.9.10"

Very  handy if you want to push a certain kind of traffic from one network to another network (in my case I was looking at forwarding netflow traffic)

Thursday, April 17, 2014

VM Console from PowerCLI

Neat little trick within PowerCLI 5.5R1:

$VM = "your vm"

Get-VM $VM| Open-VMConsoleWindow


And now your webbrowser will open and a console will start.

Getting a list of vm's with OS'es

I wanted to do a Get-VM|select name, GuestOSFullname, but that doesn't work. So some googling gave me this little gem on the vmware forum:

New-VIProperty -Name GuestFullName -ObjectType VirtualMachine -ValueFromExtensionProperty 'Guest.GuestFullName' -Force

Now I can do a Get-VM | Select Name, GuestFullName and I get an overview of which machines have which OS.

I'm sure there are other ways of getting this too, but this was what I was looking for.

P.S., if I do Get-VM | Select Name,Guest , I do get the OS, but it is prepended with the hostname.

Tuesday, April 15, 2014

Add PowerCLI modules to Powershell console

I've recently become a fan of the Powershell ISE, especially from V3 on. To load VMware PowerCLI module and to have VMware cmdlets available in powershell console:

Add-PSSnapin VMware.VimAutomation.Core


The same can be done with the update manager:

Add-PSSnapin VMware.VumAutomation