PowerShell: Microsoft SQLi baasi kirjutamise skript: Difference between revisions

From ICO wiki
Jump to navigationJump to search
Ssaveli (talk | contribs)
No edit summary
Ssaveli (talk | contribs)
No edit summary
 
Line 12: Line 12:
.EXAMPLE
.EXAMPLE
C:\PS>.\skript.ps1 -Server jaan-PC\SQLEXPRESS -Baas kettad -Table test
C:\PS>.\skript.ps1 -Server SQLEXPRESS -Baas kettad -Table test
.NOTES
.NOTES

Latest revision as of 21:07, 4 April 2012

<#
	
	.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()