Posteado por: powershellscript | Junio 18, 2008

Borrar todos los archivos .txt de una carpeta y todos sus subdirectorios

Get-ChildItem -include “*.txt” -recur remove-item

otro método:

Get-ChildItem -rec -filter *.txt remove-item

Posteado por: powershellscript | Junio 18, 2008

Borrar todos los archivos que tengan una antiguedad superior a 10 días

$now = get-date
get-childitem . -recurse where-object {($now – $_.LastWriteTime).Days -lt 10} remove-item

Posteado por: powershellscript | Junio 18, 2008

Generar el hash SHA1 de un archivo

$fileName = “C:\File1.txt”
$provider = new-object -TypeName System.Security.Cryptography.SHA1CryptoServiceProvider
$fileContent = get-content $fileName
$fileContentBytes = [System.Text.Encoding]::UTF8.GetBytes($fileContent)
$hashBytes = $provider.ComputeHash($fileContentBytes)
$hashBytesEncoded = [System.Convert]::ToBase64String($hashBytes)
write-output $hashBytesEncoded

Posteado por: powershellscript | Junio 18, 2008

Leer registros de una Tabla de SQL Server

$connectionString = “Server=(local);Database=AdventureWorks;Integrated Security=SSPI;”
$selectStatement = “select FirstName, LastName from Person.Contact;”
$connection = new-object System.Data.SqlClient.SqlConnection($connectionString)
$connection.Open()
$command = new-object System.Data.SqlClient.SqlCommand($selectStatement, $connection)
$reader = $command.ExecuteReader()
while ($reader.Read())
{
$firstName = $reader.GetString(0)
$lastName = $reader.GetString(1)
write-output “$firstName $lastName”
}
$connection.Close()

Posteado por: powershellscript | Junio 18, 2008

Conexión con un servidor SQL Server y ejecución de instrucciones SQL

$Table = new-object System.Data.DataTable
$sqlConn = new-object System.Data.SqlClient.SqlConnection(“Data Source=Server\sqlexpress;Initial Catalog=dbProducts;Integrated Security=True”)
$adapter = new-object System.Data.SqlClient.SqlDataAdapter(“Select * from Products”,$sqlConn)
$adapter.Fill($Table)
write-output $table

Posteado por: powershellscript | Junio 18, 2008

Listar los servicios en ejecución

Get-Service where {$_.Status -eq “Running”}

Posteado por: powershellscript | Junio 18, 2008

Listar usuarios de una OU

$Dom = “LDAP://OU=Office,DC=powershellscripts,DC=domain,DC=com
$Root = New-Object DirectoryServices.DirectoryEntry $Dom

$selector = New-Object DirectoryServices.DirectorySearcher
$selector.SearchRoot = $root

$adobj= $selector.findall() where {$_.properties.objectcategory -like “CN=Person*”}
foreach ($person in $adobj)
{
$prop=$person.properties
Write-host “First name: $($prop.givenname) Surname: $($prop.sn) User: $($prop.cn)”
}
Write-host “There are $($adobj.count) users in the $($root.name) domain”

Posteado por: powershellscript | Junio 18, 2008

Crear una OU

$Domain= New-Object DirectoryServices.DirectoryEntry “LDAP://DC=powershellscripts,DC=domain,DC=com
$OU = $Domain.Create(“organizationalUnit”, “ou=PowerShell”)
$OU.SetInfo()

Posteado por: powershellscript | Junio 18, 2008

Crear usuarios en una OU

$OU= New-Object DirectoryServices.DirectoryEntry “LDAP://OU=SampleMustExists,DC=powershellscripts,DC=domain,DC=com
$User=$OU.Create(“user”,”CN=John”)
$User.put(“profilepath”,”\\server\share“)
$User.SetInfo()

Posteado por: powershellscript | Agosto 14, 2007

Inicio de este Blog

Hoy comienza un nuevo Blog sobre PowerShell. En los próximos días iremos agregando ejemplos para realizar todo tipo de tareas en nuestros equipos. Desde las más simples a las más complejas.

Categorías