Added python/read_write_file.sh
This commit is contained in:
parent
9f918ead05
commit
483b01e2e1
@ -32,7 +32,7 @@ Read a file from a website into the table
|
||||
download-CsvToSQL.ps1 -sqServer "SQL01" -database "migrate-data" -table "export" -url "http://localhost/data.csv" -file "c:\temp\file.csv"
|
||||
|
||||
.LINK
|
||||
https://github.com/beakerflo/EnterMI
|
||||
https://www.entermi.nl
|
||||
#>
|
||||
[CmdletBinding()]
|
||||
param(
|
||||
|
109
PowerShell/MSSQLServer/export-SQLToExcel.ps1
Normal file
109
PowerShell/MSSQLServer/export-SQLToExcel.ps1
Normal file
@ -0,0 +1,109 @@
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Retrieves information from a SQL server database, save it to excel and mail it.
|
||||
.DESCRIPTION
|
||||
This export-SQLToExcel.ps1 script uses the SQL Server Powershell module to connect to a SQL Server and export data to a excel file.
|
||||
Written by Floris van Enter | EnterMI
|
||||
.PARAMETER sqlServer
|
||||
a single computer name or an array of computer names. You mayalso provide IP addresses.
|
||||
.PARAMETER database
|
||||
The database to get the information.
|
||||
.PARAMETER schema
|
||||
The schema to associate the objects with.
|
||||
This is an optional parameter; if it is not included, 'dbo' schema will be used.
|
||||
.PARAMETER table
|
||||
The table or view to get the data from. If it does not exist, the table will be created.
|
||||
This is an optional parameter; if it is not included, 'export' table will be used.
|
||||
.PARAMETER file
|
||||
The location & name of the excel file to export to.
|
||||
.PARAMETER template
|
||||
The location & name of a template excel file to use.
|
||||
This is an optional parameter; if it is not included, a new excel file will be created
|
||||
.PARAMETER startRow
|
||||
The writing starts at this row, default is 1
|
||||
.PARAMETER startColumn
|
||||
The writing starts at this column, default is 1
|
||||
.PARAMETER showExcel
|
||||
When you want to show excel during operations use this parameter and set it to $Trues
|
||||
.PARAMETER mailAddress
|
||||
If you to mail the sheet, fill in an email address
|
||||
This is an optional parameter; if it is not included, no e-mail will be sent
|
||||
.PARAMETER mailContents
|
||||
If you to mail the sheet, fill in an email body
|
||||
This is an optional parameter; if it is not included, an empty mail will be sent
|
||||
.EXAMPLE
|
||||
Read data from the SQL Server, save it in a file and mail it to floris@entermi.nl
|
||||
download-CsvToSQL.ps1 -sqServer "SQL01" -database "migrate-data" -table "export" -url "http://localhost/data.csv" -file "c:\temp\file.csv"
|
||||
.LINK
|
||||
https://github.com/beakerflo/EnterMI
|
||||
#>
|
||||
[CmdletBinding()]
|
||||
param(
|
||||
[string]$sqlServer,
|
||||
[string]$database,
|
||||
[string]$schema = 'dbo',
|
||||
[string]$table = 'export',
|
||||
[string]$file,
|
||||
[string]$template = $Null,
|
||||
[int]$startRow = 1,
|
||||
[int]$startColumn = 1,
|
||||
[string]$mailAddress = $Null,
|
||||
[string]$mailContents = $Null,
|
||||
[boolean]$showExcel = $False
|
||||
)
|
||||
|
||||
# create the file from template which has all necessairy formatting
|
||||
Copy-Item -Path $xlTemplate -Destination $xlFile -Force
|
||||
|
||||
# start excel instance with file
|
||||
$xl = New-Object -ComObject "Excel.Application"
|
||||
$wb = $xl.Workbooks.Open($xlFile)
|
||||
$ws = $wb.Sheets.Item(1)
|
||||
#$xl.Visible = $True # makes excel visible in development-status
|
||||
|
||||
# activate SQL mode and query
|
||||
Import-Module "sqlps" -DisableNameChecking
|
||||
|
||||
$sqlQuery = "SELECT [Kolom1],[Kolom2],[Kolom3],[Kolom4] FROM [inventarisaties].[dbo].[view] ORDER BY [Kolom1] ASC"
|
||||
$data = Invoke-SQLcmd $sqlQuery -ServerInstance $sqlServer
|
||||
|
||||
# walk through the results and fill the excel file
|
||||
$row = $rowStart
|
||||
foreach ($line in $data) {
|
||||
$col = $colStart
|
||||
$cells = $ws.Cells
|
||||
$cells.item($row,$col) = $line.Kolom1
|
||||
$col++
|
||||
$cells.item($row,$col) = $line.Kolom2
|
||||
$col++
|
||||
$cells.item($row,$col) = $line.Kolom3
|
||||
$col++
|
||||
$cells.item($row,$col) = $line.Kolom4
|
||||
$row++
|
||||
}
|
||||
|
||||
# close excel instances
|
||||
$wb.Save()
|
||||
$wb.Close()
|
||||
$xl.Quit()
|
||||
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($xl)
|
||||
|
||||
# Start Outlook
|
||||
Start-Process Outlook
|
||||
$o = New-Object -com Outlook.Application
|
||||
$mail = $o.CreateItem(0)
|
||||
|
||||
# Send an e-mail
|
||||
$mail.subject = “Hierbij de laatste lijst“
|
||||
$mail.body = (Get-Content $mail | out-string)
|
||||
$mail.To = $rcpt
|
||||
$mail.Bcc = $rcptCC
|
||||
$mail.Attachments.Add($xlFile)
|
||||
$mail.Send()
|
||||
|
||||
# Cleanup
|
||||
Start-Sleep -s 16 # give time to send mails, before quitting Outlook
|
||||
Move-Item ($folder + "Servicedesk-2*.xlsx") $folderA -force
|
||||
$o.Quit() # quits everything concerning Outlook
|
||||
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($o)
|
||||
Start-Process Outlook # only on live-workstation
|
@ -6,12 +6,15 @@ function Get-MACAddress {
|
||||
Retrieves MAC-Address
|
||||
.DESCRIPTION
|
||||
Retrieves MAC-Address from each IP enabled networkdevice in a computer
|
||||
Written by Floris van Enter | EnterMI
|
||||
.PARAMETER ComputerName
|
||||
The name of the computer to query.
|
||||
.EXAMPLE
|
||||
.\Get-MacAddress -ComputerName 'desktop1'
|
||||
.EXAMPLE
|
||||
.\Get-MacAddress -ComputerName 'server1','server2','desktop1'
|
||||
.LINK
|
||||
https://www.entermi.nl
|
||||
#>
|
||||
[CmdletBinding()]
|
||||
Param(
|
||||
|
41
Python/read_write_file.sh
Normal file
41
Python/read_write_file.sh
Normal file
@ -0,0 +1,41 @@
|
||||
# Read file, edit/cleanup data, write file
|
||||
# Tested in Python 2.7
|
||||
# I used this script to convert data to be imported in SQL Server
|
||||
# The CSV files are read, line by line and relevant data is
|
||||
# converted and written to another txt file.
|
||||
#
|
||||
# You can contact me by e-mail at floris@entermi.nl.
|
||||
#
|
||||
# Last updated 14 December, 2017.
|
||||
#
|
||||
# Floris van Enter
|
||||
# http://entermi.nl
|
||||
|
||||
readFile = open('./source.csv','r')
|
||||
writeFile = open('./destination.txt','w')
|
||||
|
||||
# Read every line and do something with it
|
||||
line = readFile.readline()
|
||||
while line:
|
||||
# CSV means comma seperated. Fill in here a way to split on specific character
|
||||
contents = line.split(',')
|
||||
|
||||
# read the 7th and 9th column in the line and cleanup the data
|
||||
# removed all quotes ' & "
|
||||
# removed all brackets () and []
|
||||
# strip() to remove whitespaces in front and at the end
|
||||
# Start the string from position 4, skip the first three characters with [3:]
|
||||
|
||||
name = contents[7].replace('"','').replace("'","").replace('[','').replace(']','').strip()[3:] + " (" + type + ")"
|
||||
desc = contents[9].replace('"','').replace("'","").strip()
|
||||
|
||||
query = 'DB::statement("INSERT INTO `table` (`name`, `description`, `created_at`, `updated_at`) VALUES '
|
||||
query += "('" + name + "', '" + desc + "', now(), now())"
|
||||
query += '");'
|
||||
|
||||
# write query and read next line
|
||||
writeFile.write(query + "\n")
|
||||
line = readFile.readline()
|
||||
|
||||
readFile.close()
|
||||
writeFile.close()
|
@ -1,6 +1,15 @@
|
||||
# Copy files to remote location.
|
||||
# Tested in Python 2.7
|
||||
# Make sure you can connect to your remote folder with certificate.
|
||||
# This way you don't need to enter your credentials and you
|
||||
# can schedule this script to run when you like
|
||||
#
|
||||
# You can contact me by e-mail at floris@entermi.nl.
|
||||
#
|
||||
# Last updated 1 December, 2017.
|
||||
#
|
||||
# Floris van Enter
|
||||
# http://entermi.nl
|
||||
|
||||
scp -r /home/pi/scripts/wordpressUpdate/source/* user@server.remote.nl:/var/www/site1/
|
||||
scp -r /home/pi/scripts/wordpressUpdate/source/* user@server.remote.nl:/var/www/site2/
|
||||
|
42
TSQL/Backup_all_Databases_in_instance.sql
Normal file
42
TSQL/Backup_all_Databases_in_instance.sql
Normal file
@ -0,0 +1,42 @@
|
||||
/*
|
||||
|
||||
SQL Server Backup script - SQL Server 2008, SQL Server 2008 R2, SQL Server 2012, SQL Server 2014, and SQL Server 2016
|
||||
|
||||
You can contact me by e-mail at floris@entermi.nl.
|
||||
|
||||
Last updated 1 December, 2017.
|
||||
|
||||
Floris van Enter
|
||||
http://entermi.nl
|
||||
|
||||
*/
|
||||
|
||||
DECLARE @name VARCHAR(50) -- database name
|
||||
DECLARE @path VARCHAR(256) -- path for backup files
|
||||
DECLARE @fileName VARCHAR(256) -- filename for backup
|
||||
DECLARE @fileDate VARCHAR(20) -- used for file name
|
||||
|
||||
-- specify database backup directory
|
||||
SET @path = 'C:\Program Files\Microsoft SQL Server\MSSQL13.TEST\MSSQL\Backup\'
|
||||
|
||||
-- specify filename format
|
||||
SELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),112)
|
||||
|
||||
DECLARE db_cursor CURSOR READ_ONLY FOR
|
||||
SELECT name
|
||||
FROM master.dbo.sysdatabases
|
||||
WHERE name NOT IN ('tempdb')
|
||||
|
||||
OPEN db_cursor
|
||||
FETCH NEXT FROM db_cursor INTO @name
|
||||
|
||||
WHILE @@FETCH_STATUS = 0
|
||||
BEGIN
|
||||
SET @fileName = @path + @name + '_' + @fileDate + '.BAK'
|
||||
BACKUP DATABASE @name TO DISK = @fileName
|
||||
|
||||
FETCH NEXT FROM db_cursor INTO @name
|
||||
END
|
||||
|
||||
CLOSE db_cursor
|
||||
DEALLOCATE db_cursor
|
@ -1,3 +1,16 @@
|
||||
/*
|
||||
|
||||
SQL Server Database overview - SQL Server 2008, SQL Server 2008 R2, SQL Server 2012, SQL Server 2014, and SQL Server 2016
|
||||
|
||||
You can contact me by e-mail at floris@entermi.nl.
|
||||
|
||||
Last updated 1 December, 2017.
|
||||
|
||||
Floris van Enter
|
||||
http://entermi.nl
|
||||
|
||||
*/
|
||||
|
||||
USE master;
|
||||
GO
|
||||
|
||||
|
@ -1,3 +1,16 @@
|
||||
/*
|
||||
|
||||
SQL Server Instance information script - SQL Server 2008, SQL Server 2008 R2, SQL Server 2012, SQL Server 2014, and SQL Server 2016
|
||||
|
||||
You can contact me by e-mail at floris@entermi.nl.
|
||||
|
||||
Last updated 1 December, 2017.
|
||||
|
||||
Floris van Enter
|
||||
http://entermi.nl
|
||||
|
||||
*/
|
||||
|
||||
SELECT
|
||||
SERVERPROPERTY('MachineName') AS ComputerName,
|
||||
SERVERPROPERTY('ServerName') AS InstanceName,
|
||||
|
Loading…
x
Reference in New Issue
Block a user