PowerShell: Microsoft SQLi baasi kirjutamise skript

From ICO wiki
Revision as of 21:07, 4 April 2012 by Ssaveli (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search
<#
	
	.Synopsis
	Kirjutab arvutinime, loogiliste k6vaketaste arvu, nende suuruse, vaba ruumi ja kirje tegemise aja andmebaasi,
	.DESCRIPTION
        Skripti toimimiseks on vaja Microsoft SQL server. Andmbeaasi külge autentimiseks kasutatakse Windows Authenticationit
	V6imalik on anda andmebaasi serveri parameetrid (server nimi, baasi nimi ja tabeli nimi)
	Tabelis endas peavad olema väljas Masin, Ketas, Suurus, vaba ja  Aeg.
	
	
	.EXAMPLE
	C:\PS>.\skript.ps1 -Server SQLEXPRESS -Baas kettad -Table test
	
	.NOTES
	Autor: Sander Saveli 
	Versioon: 1.0 
	Muutmise kuup2ev: 02.04.2012
	
	.LINK
        https://wiki.itcollege.ee/index.php/PowerShell:_Microsoft_SQLi_baasi_kirjutamise_skript
	
	
#>

Param(	[String]$Server = "juhan-PC\SQLEXPRESS",
		[String]$Baas = "mootor",
		[String]$Tabel = "disk")


# Create SqlConnection object, define connection string, and open connection
$con = New-Object System.Data.SqlClient.SqlConnection
$con.ConnectionString = "Server=$Server;Database=$Baas;Trusted_Connection=True;"
$con.Open()

# Create SqlCommand object, define command text, and set the connection
$cmd = New-Object System.Data.SqlClient.SqlCommand
$cmd.Connection = $con


        $kettad = Get-WmiObject Win32_LogicalDisk | Select-Object SystemName, DeviceID, Size, FreeSpace
$kettad | ForEach-Object {
			$kettad = Get-WmiObject Win32_LogicalDisk | Select-Object SystemName, DeviceID, Size, FreeSpace
			$mnimi = $_.SystemName
			$knimi = $_.DeviceId
            $a = Get-Date
            $td= $a.ToShortDateString() +" " + $a.ToShortTimeString() 
			if($_.Size) {$ksuurus = $_.Size} else {$ksuurus = 0}
			if($_.FreeSpace) {$kvaba = $_.FreeSpace} else {$kvaba = 0}
			$sql = "INSERT INTO $Tabel (Masin, Ketas, Suurus, Vaba, Aeg) VALUES ('$mnimi','$knimi',$ksuurus,$kvaba, '$td')"
			$cmd.CommandText = $sql
            $RowsInserted = $cmd.ExecuteNonQuery()
            }

# Close the connection
$con.Close()