Posts tagged ‘Powershell’

Free PowerShell book

If you’re just learning PowerShell or you’re already a top scriptwizard “Mastering Powershell” might prove to be a useful resource.

Besides the usual scripting basics like variables, functions, pipes and so on the later chapters show some usage of the scripting language for some more concrete problems like XML manipulation or user account management. Just give it a try, it’s free!

Prezentare Windows Powershell

Avand in vedere “succesul” prezentarii de Powershell de saptamana trecuta am decis sa public prezentarea si codul demo. Have fun!

Windows Powershell
Windows Powershell Demo Code

Windows Power Shell

Microsoft Academic Tour and Innovation Briefing

Joi 7 Mai ora 14.00 in sala EC105 din Facultatea de Automatica si Calculatoare are loc Microsoft Academic Tour. Hot Topics: Windows 7 RC1 si Internet Explorer 8.
Joi 14 Mai ora 14.00 in sala EC105 din Facultatea de Automatica si Calculatoare are loc Microsoft Innovation Briefing. Hot Topics: Windows Azure si Windows Powershell.

Prezentarea de Powershell e tinuta de subsemnatul. Daca te simti cu bashul pe caciula vino sa vezi ca se poate si altfel!

Pentru inregistrare :
http://studentclub.ro/evenimente/DetaliiEveniment.aspx?ID=655
si
http://studentclub.ro/evenimente/DetaliiEveniment.aspx?ID=656

PS: mai mult ca sigur ca pe 7 mai o sa vedem windows 7 RC1 in limba romana! Tocmai m-am inrolat in programul de beta testing!

PowerShell Server

Meseriasii de la /n (aia care dadeau stickere moca) au un produs tare fain. Ii zice powershell server si jucaria iti transforma masina windows din o statie unde poti face chestii doar via GUI in una bucata statie accesibila remote via ssh. SSH vine de la Secure shell. Ei bine daca secure e clar ce ne da, shell aici e inlocuit cu PowerShell. In felul asta se poate controla o masina windows doar cu putty! (yes Powershel rocks! )

Desigur exista un mic downside … pretul

http://www.nsoftware.com/powershell/server/

Powershell 2 CTP3

Ocupati cu cumparaturile de sezon si cu facutul de sarmale si carnati putini au observat ca echipa de produs de la Powershell s-a tinut de cuvant si a lansat la apa o versiune noua de powershell 2 : CTP3 (ctp=community technology preview).

Facilitatile noi  sunt destul de consistente:

  • imbunatariri majore in ceea ce priveste remotingul, peste 60 de cmdlets noi legate de : adaugarea,stergerea,mutarea unui computer, manevrarea logurilor,etc,
  • avem un editor grafic putin imbunatatit si redenumit (in CTP2 ii spuneam Graphical Powershell acum il refeream ca ISE – integrated script editor) si acum cu suport pentru debuging (da se poate face debugining si fara echo!)
  • API-ul de chemare a powershellului in o bucata de cod c# a fost putin schimbat si s-ar putea ca unii dezvoltatori sa strambe din nas

Imbunatatirile si adaugirile sunt mult mai multe si o sa ma luminez si eu pe masura ce se updateaza pagina de pe scriptcenter. Pana atunci download and enjoy!

PS: daca vreti functionalitatile de remote (care de fapt reprezinta cireasa CTP-ului asta e nevoie de o componenta suplimentara: WinRM 2.0 CTP3)

Ca tot veni vorba de powershell am dat recent peste un laborator online unde se poate experimenta cu powershell fara sa fie nevoie sa fie instalat pe pc-ul personal: aici

La multi ani si un an nou mai prosper si cu cat mai multe bucurii! Santate included!

Quickie: Get free disk space with Powershell and wmi

Quick and dirty

gc work.txt |foreach{
$val=(gwmi -computername $_ -Query "SELECT FreeSpace FROM Win32_LogicalDisk WHERE DeviceID = 'C:'").FreeSpace / 1Gb;
echo "$_ $val"
}

Quckie: Group Policy Refresh on Demand

Tooluri necesare:

1) Powershell

2) Scula asta. Exista si un cmdlet care face direct chestia asta dar nu merge in powershell 2.0. Pana cand apare ne descurcam cu exeul.

3) Numele workstationurilor in format csv

$comp = Import-Csv .\workstations.csv |select computername|select computername
foreach ($i in $comp) {.\Rgprefresh.exe /m:$comp}

PowerShell WakeOnLan v0.2

Scriptul de ieri l-am extins si i-am adaugat functii de remote shutdown si restart. Ca bonus am adaugat si epic fail si putina validare.

#wakeonlan $computer
function WakeOnLan($computer)
{
	$select=$select |where-object {$_.computername -eq $computer} |Select-Object mac
	if ($select.mac -eq $null)
	{
		echo "workstation not found.epic fail. use all to wake'em all"
	}
	else
	{
		$select.mac  -match "(..)(..)(..)(..)(..)(..)" | out-null
		$mac= [byte[]]($matches[1..6] |% {[int]"0x$_"})
		$UDPclient = new-Object System.Net.Sockets.UdpClient
		$UDPclient.Connect(([System.Net.IPAddress]::Broadcast),4000)
		$packet = [byte[]](,0xFF * 102)
		6..101 |% { $packet[$_] = $mac[($_%6)]}
		$UDPclient.Send($packet, $packet.Length)
		echo "workstation $computer is booting up..."
	}
}

#wakeonlan all the computers
function WakeOnLanAll
{
	$computers=$select | Select-Object computername
	foreach ($computer in $computers)
	{
		$target = $computer.computername
		WakeOnLan($target)
		#delay to be powergrid friendly
		Start-Sleep -seconds 5
	}
}
#shutdown $computer
function ShutDown($computer)
{
if ($computer.ToLower() -eq "all")
	{
	$select=$select|Select-Object computername
	foreach ($computername in $select)
		{
			$target=$computername.computername
			get-wmiobject win32_operatingsystem -computer $target | foreach {$_.shutdown()}
		}
	}
else {
		$select=$select |where-object {$_.computername -eq $computer} |Select-Object computername
		if ($select.computername -eq $null)
		{
			echo "workstation $computer not found.epic fail. use all to kill'em all"
		}
		else
		{
			get-wmiobject win32_operatingsystem -computer $computer | foreach {$_.Shutdown()}
		}
	}
}
####reboot $computer
function Reboot($computer)
{
#reboot all
if ($computer.ToLower() -eq "all")
	{
	$select=$select|Select-Object computername
	foreach ($computername in $select)
		{
			$target=$computername.computername
			get-wmiobject win32_operatingsystem -computer $target | foreach {$_.reboot()}
		}
	}
else {
	$select=$select |where-object {$_.computername -eq $computer} |Select-Object computername
	#check input
	if ($select.computername -eq $null)
	{
		echo "workstation $computer not found.epic fail. use all to kill'em all"
	}
	#reboot
	else
		{
			get-wmiobject win32_operatingsystem -computer $computer | foreach {$_.reboot()}
			#delay to be powergrid friendly
			Start-Sleep -seconds 5
		}
	}
}

###################
$option=read-host "Enter option"
$select=Import-Csv workstations.csv
switch ($option)
{
	"wol" {
			$computer=read-host "Enter Workstation to wake..."
			if ($computer -eq "all")
			{
				WakeOnLanAll
			}
	else {
			WakeOnLan($computer)
			ping -4 -n 25 $computer
		}
	}
	"reboot" {
			$computer=read-host "Enter Workstation to reboot..."
			Reboot($computer)
	}
	"shutdown" {
			$computer=read-host "Enter Workstation to kill..."
			Shutdown($computer)
	}
	default {echo "error!options are : wol, reboot, shutdown"}
}

PowerShell Wake on Lan v0.1

Wake on lan e o functie disponibila pe orice placa de baza mai noua de 1900 toamna ce te lasa sa pornesti de la distanta un calculator. Principiul de functionare e cam asa: trimiti pe retea un pachet “special” ce contine MAC-ul statiei ce ce vrei sa o pornesti. Daca e configurata corect si e sub tensiune statia porneste si putem sa o administram de la distanta.
Fabricarea magic packetului e luata de la powershellguy

Pentru a rula scriptul e nevoie de un fisier csv ce contine doua informatii : nume statie si mac
exemplu

computername,mac
workstation01,001e37FF3e1b
workstation02,001e37FF3e8c

Scriptul cere de la user numele statiei si o porneste. Enjoy!

$computer=read-host "Enter Workstation to wake..."

$select=Import-Csv workstations.csv |where-object {$_.computername -eq $computer} |Select-Object mac
if ($select.mac -eq $null)
{
	echo "workstation not found.fail"
}
else
{

$select.mac  -match "(..)(..)(..)(..)(..)(..)" | out-null
$mac= [byte[]]($matches[1..6] |% {[int]"0x$_"})  

$UDPclient = new-Object System.Net.Sockets.UdpClient
$UDPclient.Connect(([System.Net.IPAddress]::Broadcast),4000)
$packet = [byte[]](,0xFF * 102)
6..101 |% { $packet[$_] = $mac[($_%6)]}
$UDPclient.Send($packet, $packet.Length)
echo "workstation $computer is booting up..."
ping -n 25 $computer
}